0

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

?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
HBMCS
  • 686
  • 5
  • 25
  • Does this answer your question? [Commas and $ in datatable columns](https://stackoverflow.com/questions/13280817/commas-and-in-datatable-columns) – Anurag Srivastava Mar 26 '20 at 18:11
  • You can process the data so it is the way you want before you add it. – epascarello Mar 26 '20 at 18:18
  • The "render" : function would need to be called when the table is initialised. My need is to format the data after a XHR call, which clears and repopulates the table. Can I call the 'render' after the draw() function? – HBMCS Mar 26 '20 at 18:31
  • @epascarello This is a JSON respnse, and the comma is outside the actual data coming from the XHR, so to speak (i.e. outside the inverted commas). How can I prevent JSON not to have commas between fields? – HBMCS Mar 26 '20 at 18:34
  • Because it is doing toString() on an array in your datatable, so process it so it is not an array – epascarello Mar 26 '20 at 18:34

1 Answers1

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);
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thanks, that makes sense, and it works in your snippet. Nevertheless I get 'TypeError: record[3].join is not a function' when I run it in my webiste. I should add that not all the records have sub-items, many record only have one item. could this be an issue? – HBMCS Mar 26 '20 at 18:45
  • I do not know what your data is so hard to know, just went off what you shown us. – epascarello Mar 26 '20 at 18:51