Initialize a single-column table with a plain array

Initialize a single-column table with a plain array

UdiDUdiD Posts: 16Questions: 7Answers: 1

I'd like to initialize a single-column table with a plain array.
Doing the following results in each row having the first letter of each array item (Of course, that's not what I need):

var dataArr = ["James", "Joe", "John"]
var table = $('#exampleTable').DataTable({
    data: dataArr,
  columns: [{title: "Name"}]
});

Now, doing the following works, but it would require me to wrap each item in an array of its own, which is undesirable:

var dataArr = [["James"], ["Joe"], ["John"]]
var table = $('#exampleTable').DataTable({
    data: dataArr,
  columns: [{title: "Name"}]
});

Is there some initialization option I can use to make the plain array work?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 62,241Questions: 1Answers: 10,211 Site admin
    Answer ✓

    Yes, with DataTables the data array has to be an array of rows objects/arrays. Using a single data point / column is relatively unusual with a DataTable, which is why there is no option for that. However, you could readily do it using $.map:

    data: $.map( dataArr, function ( d ) {
      return [ d ];
    } )
    

    Allan

  • UdiDUdiD Posts: 16Questions: 7Answers: 1

    Thanks, allan!

This discussion has been closed.