简体   繁体   English

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

[英]Reload datatable data with ajax call on click

I have a datatable with ajax calls. 我有一个带有ajax调用的数据表。 I form the table like this: 我形成这样的表:

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. 如您所见,我向我的php操作发送了2个参数。
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). 所以我想做的是再次调用ajax并发送其他参数(quizid将相同,而questionid将不同)。

I know you have something like this oTable.fnReloadAjax(newUrl); 我知道你有这样的东西oTable.fnReloadAjax(newUrl); but what should I paste at the newUrl parameter?? 但是我应该在newUrl参数上粘贴什么?

I've made a jsfiddle: http://jsfiddle.net/8TwS7/ 我做了一个jsfiddle: http : //jsfiddle.net/8TwS7/

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 您应该能够调用click函数来重绘表,对我们在数据表初始化时创建的变量使用fnDraw API调用没问题

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

        oTable.fnDraw();
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM