dtjwov4984 2013-11-15 14:27
浏览 90
已采纳

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

I have a datatable with ajax calls. 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.
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).

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

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

  • 写回答

1条回答 默认 最新

  • dongpao5127 2013-11-15 18:52
    关注

    you should be able to accomplish this by using fnServerData instead of 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() + "
    " + 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

    $('#somelement').on('click', function(){
    
            oTable.fnDraw();
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32c8t6工程,使用hal库
  • ¥100 有偿求易语言word文档取doc和docx页数方法或模块
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决