单击时使用ajax调用重新加载数据表数据

时间:2022-06-05 12:47:47

I have a datatable with ajax calls. I form the table like this:

我有一个带有ajax调用的数据表。我形成这样的表格:

var _initTable = function() {
    $('#datatables tr').not(':first').on('click', function() {
        var dateandtime = $(this).find(':nth-child(3)').text();
        window.location.href = '/results/detail/dateandtime/' + dateandtime;
    });
};

$('#datatables').dataTable({
     bProcessing  : true,
     sProcessing  : true,
     bServerSide  : true,
     sAjaxSource  : '/results/load-results',
     fnServerParams: function ( aoData ) {
         aoData.push( {"name": "quizid", "value": quizid },{ "name": "questionid", "value": questionid } );
     },
     aoColumnDefs : [{'bSortable' : false, 'aTargets' : ['no-sort']}], // make the actions column unsortable
     sPaginationType : 'full_numbers',
     fnDrawCallback  : function(oSettings) {
         _initTable();
     }
});

As you can see I send 2 parameters to my php action.
Now I would like to reload the table when I click on a button. So what I want to do is make another ajax call and send other parameters (quizid will be the same, but the questionid will be different).

如你所见,我向我的php动作发送2个参数。现在我想在点击按钮时重新加载表格。所以我想做的是进行另一个ajax调用并发送其他参数(quizid将是相同的,但是questionid将是不同的)。

I know you have something like this oTable.fnReloadAjax(newUrl); but what should I paste at the newUrl parameter??

我知道你有这样的东西oTable.fnReloadAjax(newUrl);但是我应该在newUrl参数上粘贴什么?

I've made a jsfiddle: http://jsfiddle.net/8TwS7/

我做了一个jsfiddle:http://jsfiddle.net/8TwS7/

1 个解决方案

#1


4  

you should be able to accomplish this by using fnServerData instead of fnServerParams

您应该能够通过使用fnServerData而不是fnServerParams来实现此目的

var oTable = $('#datatables').dataTable({
     bProcessing  : true,
     sProcessing  : true,
     bServerSide  : true,
     sAjaxSource  : '/results/load-results',
"fnServerData": function ( sSource, aoData, fnCallback ) {
            /* Add some extra data to the sender */
            aoData.push( { "name": "quizid", "value": quizid } );
            aoData.push( { "name": "question_id", "value": question_id } );
            $.getJSON( sSource, aoData, function () { 
                /* Do whatever additional processing you want on the callback, then tell DataTables */              
            }).done(function(json){
                fnCallback(json);
                        }).fail(function(xhr, err){ 
                var responseTitle= $(xhr.responseText).filter('title').get(0);
                alert($(responseTitle).text() + "\n" + formatErrorMessage(xhr, err) ); 
            });
        },      
    });

you should be able to then call a click function to redraw your table no problem using the fnDraw API call on the variable we created on datatable initialization

你应该能够在我们在数据表初始化上创建的变量上使用fnDraw API调用来调用click函数重绘表没问题

$('#somelement').on('click', function(){

        oTable.fnDraw();
    });

#1


4  

you should be able to accomplish this by using fnServerData instead of fnServerParams

您应该能够通过使用fnServerData而不是fnServerParams来实现此目的

var oTable = $('#datatables').dataTable({
     bProcessing  : true,
     sProcessing  : true,
     bServerSide  : true,
     sAjaxSource  : '/results/load-results',
"fnServerData": function ( sSource, aoData, fnCallback ) {
            /* Add some extra data to the sender */
            aoData.push( { "name": "quizid", "value": quizid } );
            aoData.push( { "name": "question_id", "value": question_id } );
            $.getJSON( sSource, aoData, function () { 
                /* Do whatever additional processing you want on the callback, then tell DataTables */              
            }).done(function(json){
                fnCallback(json);
                        }).fail(function(xhr, err){ 
                var responseTitle= $(xhr.responseText).filter('title').get(0);
                alert($(responseTitle).text() + "\n" + formatErrorMessage(xhr, err) ); 
            });
        },      
    });

you should be able to then call a click function to redraw your table no problem using the fnDraw API call on the variable we created on datatable initialization

你应该能够在我们在数据表初始化上创建的变量上使用fnDraw API调用来调用click函数重绘表没问题

$('#somelement').on('click', function(){

        oTable.fnDraw();
    });