Using columns().data() api with ajax sourced JSON data in jquery datatables

Using columns().data() api with ajax sourced JSON data in jquery datatables

kunalpatel00750kunalpatel00750 Posts: 2Questions: 1Answers: 0

I want to get unique values from a column of datatables using columns().data() api and store it in an array.

Datatables code:

table = $('#example').DataTable({
    processing: true,
    ajax: {
        dataSrc: "",
        url: "api.php?type=1&dmid="+dmid+"&mindate="+min_date+"&maxdate="+max_date,
        type: "GET"
    },
    columns: [
        { data: "id" },
        { data: "receipt_num" },
        .
        .
        .
        { data: "amount" }
    ],
    columnDefs: [
        { "visible": false, "targets": 2 }
    ],
    dom: 'Bflrtip',
    scrollX: true,
    colReorder: true
});

example:

table.columns(2).data().unique().sort().each(function(value, index) {
    console.log(value, index);
});

The above example works when i dont use ajax sourced data but when i use ajax there is no output in console.

As the column i want values from is hidden i am unable to use js/jquery to get column data that is why i was trying to use columns() api.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598
    Answer ✓

    Hi @kunalpatel00750 ,

    It might be because the data is still loading - the Ajax data is loaded asynchronously. Try adding those lines inside initComplete, so it will be called once the data is fully loaded and the table initialised.

    Cheers,

    Colin

  • kunalpatel00750kunalpatel00750 Posts: 2Questions: 1Answers: 0

    @colin Damn thanks man it worked :)

This discussion has been closed.