how can I trigger jquery datatables fnServerData to update a table via AJAX when I click a button?

前端 未结 7 952
陌清茗
陌清茗 2020-12-28 17:15

I\'m using the datatables plugin with server-side data and am updating the table using AJAX.

My dataTables setup looks like this:

tblOrders = parame         


        
相关标签:
7条回答
  • 2020-12-28 17:49

    In the initialization use:

    "fnServerData": function ( sSource, aoData, fnCallback ) {
                        //* Add some extra data to the sender *
                        newData = aoData;
                        newData.push({ "name": "key", "value": $('#value').val() });
    
                        $.getJSON( sSource, newData, function (json) {
                            //* Do whatever additional processing you want on the callback, then tell DataTables *
                            fnCallback(json);
                        } );
                    },
    

    And then just use:

    $("#table_id").dataTable().fnDraw();
    

    The important thing in the fnServerData is:

        newData = aoData;
        newData.push({ "name": "key", "value": $('#value').val() });
    

    if you push directly to aoData, the change is permanent the first time and when you draw the table again the fnDraw don't work the way you want. So, use a copy of aoData to push data to the ajax.

    0 讨论(0)
提交回复
热议问题