columns().data() returns empty arrays

columns().data() returns empty arrays

ruggggerrugggger Posts: 23Questions: 6Answers: 1

I am trying to use table.columns().data() to get an array containing my table but I am getting empty arrays.

The following code

    console.log('table ',t);
    console.log('table.columns().data() ', t.columns().data());
    console.log('table.column(0).data() ', t.column(0).data());

outputs the arrays to the console. They are all empty although the table is displayed on the page and contains several rows.

Link : http://app.meafood.org.il/%D7%9C%D7%A7%D7%95%D7%97%D7%95%D7%AA/

Thank you,
Yaron

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,831Questions: 1Answers: 10,518 Site admin

    You are using Ajax - which means that the data is fetched asynchronously. When the columns().data() code runs, the data hasn't been loaded yet!

    Use initComplete to know when the data has been loaded.

    Allan

  • ruggggerrugggger Posts: 23Questions: 6Answers: 1

    Oops, sorry for that.

    Here I updated the code. When clicking on the 'ADD' button I want to get the sorted array of the 'ID' column. I get it but it is sorted as a string, although it is all integers, and I try to force it to 'type': num as well.

            "columnDefs": [
                { "targets": [3],
                   "type": "num" }
              ],
                 
    

    Clicking on ADD will print the data to the console

         console.log('sort() ', t.column(3).data().sort());
        console.log('sort().reverse() ', t.column(3).data().sort().reverse()); 
    

    "1","10","11","12","13","14","15","16","17","18","19","2","20" and so on..

    http://app.meafood.org.il/%D7%9C%D7%A7%D7%95%D7%97%D7%95%D7%AA/

    Thanks,
    Yaron

  • allanallan Posts: 63,831Questions: 1Answers: 10,518 Site admin

    In the data source it is a string:

    id: "1"

    Which is why it is string sorting. The type has no effect on what sort() does - that is only used when the table itself does the sorting.

    Ideally you would change your data source to have integers.

    Alternatively you can specify a sorting function for sort() just like you can with Array.prototype.sort().

    Allan

  • ruggggerrugggger Posts: 23Questions: 6Answers: 1

    I don't understand why the data source is a string...

    In the SQL table the column is defined as INT(10) Unsigned Auto Increment.
    The field contains a number in all its rows.

    What would make DataTables view it as a string?

    Yaron

  • allanallan Posts: 63,831Questions: 1Answers: 10,518 Site admin

    DataTables isn't making it a string - it is a string in the JSON data source that is being given to DataTables:

    {"data":[{"DT_RowId":"row_1","id":"1","name":"NAME1","email":...

    Note: "id":"1".

    I don't know why the clientsapi.php script is giving it back as a string, but I'd suggest you need to cast the value in that script if you want it to be an integer.

    Allan

  • ruggggerrugggger Posts: 23Questions: 6Answers: 1

    The clientsapi.php is as follows:

    use DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
    
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'mea_clients' )
        ->fields(
            Field::inst( 'id' )->set(false),
            Field::inst( 'name' ),
            Field::inst( 'email' ),
            Field::inst( 'telephone' ),
            Field::inst( 'deleted' )
        
        )
        ->process( $_POST )
        ->json();
    

    How can I set 'id' as a numeric field type ?
    Is it done with getFormatter ? Couldn't find the info..

    Thanks

    Yaron

  • allanallan Posts: 63,831Questions: 1Answers: 10,518 Site admin
    Answer ✓

    Yes, you could use:

    ->getFormatter( $val, $data, $opts ) {
      return intval( $val );
    } )
    

    Allan

This discussion has been closed.