Unwanted comma in XHR response ending up in table's cells

♀尐吖头ヾ 提交于 2021-01-29 13:34:09

问题


I have an XHR response that looks something like this:

[
    [
        "e33222",
        "1730-06-27",
        "Lewis Morris",
        [
            "Edward Wynne; ",
            "William Bulkeley"
        ]
    ], [...]
]

It is used to populate a table with

var table = $('#mainTable').DataTable()
table.rows.add(result)
table.draw();

It all works great, except for an annoying comma added at the beginning of every subsequent sub-item (or, better, at the end of every first sub-item which has a following one, see "Edward Wynne; ", "William Bulkeley").

The end result is that in the cell corresponding to that data I get:

William Morris; ,Richard Morris

Is there any way of getting:

William Morris; Richard Morris

?


回答1:


So loop over the data before you set it in the datatable and format the data so it is not an array.

var results = [
    [
        "e33222",
        "1730-06-27",
        "Lewis Morris",
        [
            "Edward Wynne; ",
            "William Bulkeley"
        ]
    ],
    [
        "22222",
        "2222-06-27",
        "foooo Mooooo",
        [
            "foo bar; ",
            "foo baz"
        ]
    ],    
]

results.forEach(function (record) {
  record[3] = record[3].join('')
})
console.log(results);


来源:https://stackoverflow.com/questions/60873549/unwanted-comma-in-xhr-response-ending-up-in-tables-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!