Dyanmically change column data

Dyanmically change column data

kirakatoukirakatou Posts: 4Questions: 1Answers: 0
$(document).ready(function() {
    $('#example').DataTable( {
        "ajax": "data/objects.txt",
        "columns": [
            // change this data
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "extn" },
            { "data": "start_date" },
            { "data": "salary" }
        ]
    } );
} );

is there a way to dynamically change column data ?

Replies

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Yes. You can use row().data() or cell().data().

    If this doesn't help then please provide more details of how you want to change the data.

    Kevin

  • kirakatoukirakatou Posts: 4Questions: 1Answers: 0
    edited August 2020

    its change value of column.
    what i mean is change the column name that used to call api.
    everytime datatable try to call api its got request like this:

    draw: 2
    columns[0][data]: position
    columns[0][name]: position
    columns[0][searchable]: false
    columns[0][orderable]: false
    

    i want to change that columns[0][data] dynamically cause its affect the order column in backend
    its something like this

    $(document).ready(function() {
        $('#example').DataTable( {
            "ajax": "data/objects.txt",
            "columns": [
                // change this data
                { "data": $('#filter') == 'name' ? "name" : 'others' },
                { "data": "position" },
                { "data": "office" },
                { "data": "extn" },
                { "data": "start_date" },
                { "data": "salary" }
            ]
        } );
    } );
    
  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    No, there is not. a feature in Datatables to dynamically change the column names. The columns.data is used to define the object property for the column. You can change this by using destroy() and reinitializing the columns with the desired object properties but I'm guessing this isn't what you want.

    You will probably need to change something in your server script to not affect the order. If you want help with this please post your script and details of the problem. Someone may be able to offer suggestions.

    Kevin

This discussion has been closed.