How to show JSON without 'data' key in DataTable?

How to show JSON without 'data' key in DataTable?

dunkubokdunkubok Posts: 1Questions: 1Answers: 0

I have the following Ajax request JSON-format:

 Array:
   Object:
     Object (key = 'fields'):
       Values to be shown
   Object:
     Object (key = 'fields'):
       Values to be shown
   Object:
     Object (key = 'fields'):
       Values to be shown

How can I add this data format to DataTable? I think I should somehow get rid of the key values in 'columns'.

Any idea?

$('#grid').DataTable({
    "ajax": demand,
    "columns": [
    {"fields": model"},
    {"fields": "product"},
    {"fields": "type"},
    {"fields": "build"},
    {"fields": "sdnad"},
    {"fields": "rcnad"},
    {"fields": "sdeud"},
    {"fields": "rceud"},
    {"fields": "smsd"},
    {"fields": "smrc"},
    {"fields": "notes"},
    ],
    initComplete: function () {
        this.api().columns().every(function () {
            var column = this
            var select = $('<select><option value=""></option></select>')
            .appendTo($(column.footer()).empty())
            .on('change', function () {
                var val = $.fn.dataTable.util.escapeRegex(
                    $(this).val()
                    )

                column
                .search(val ? '^' + val + '$' : '', true, false)
                .draw()
            })

            column.data().unique().sort().each(function (d, j) {
                select.append('<option value="' + d + '">' + d + '</option>')
            })
        })
    }
})

Answers

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10
    edited January 2018

    I think DataTables relies on the JSON being formatted that specific way, but more experienced users may chime in with some other options.

    Here's what I do:

    1) Include this at the top of the target page for the AJAX request:

    <?php
    
    // DataTables PHP library
    include "../../Datatables/Editor/php/DataTables.php";
    
    use
     DataTables\Database,
     DataTables\Database\Query,
     DataTables\Database\Result;
    

    2) Construct sql statement as $sql

    and run:
    $records = $db->sql( $sql )->fetchAll(); to get the results.

    3) Modify $records if necessary

    4) Return to the requesting page in the proper format:

    $data['data'] = array_merge( $records );
    
    echo json_encode( $data );
    
  • kthorngrenkthorngren Posts: 20,628Questions: 26Answers: 4,831

    This page discusses the data structures supported by Datatables:
    https://datatables.net/manual/data/

    Datatables doesn't have a fields object for columns. you would use columns.data. For example:

        "columns": [
        {"data": "model"},
        {"data": "product"},
        {"data": "type"},
    ....
        {"data": "notes"},
        ],
    

    I'm not clear what your JSON format is but, if using objects, it will need to look similar to this:

    {
      "data": [
        {
          "model": "1",
          "product": "xyz",
          "type": "abc",
    ....
          "notes": "1234"
        },
    ....
      ]
    }
    

    Here is an ajax with objects example:
    https://datatables.net/examples/ajax/objects.html

    Take a look at the "Ajax" tab.

    Or if you want to use arrays you can use this example:
    https://datatables.net/examples/ajax/simple.html

    Kevin

  • allanallan Posts: 62,296Questions: 1Answers: 10,215 Site admin

    Can you show us an actual example of the data please? I'm sore certain, but from your description you might be able to use columns.data's ability to use nested data.

    Allan

This discussion has been closed.