问题
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