2013-11-15 37 views
2

我有一个带有ajax调用的数据表。我形成这样的表格:用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(); 
    } 
}); 

正如你所看到的,我发送2个参数给我的PHP动作。
现在我想重新加载表,当我点击一个按钮。所以我想要做的是另一个Ajax调用并发送其他参数(quizid将是相同的,但questionid将不同)。

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

我做了的jsfiddle:http://jsfiddle.net/8TwS7/

回答

4

,你应该能够通过使用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)); 
      }); 
     },  
    }); 

你应该能够然后调用点击功能重新绘制你的表没有问题使用fnDraw API调用我们在数据表初始化上创建的变量

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

     oTable.fnDraw(); 
    }); 
+0

然后我只是得到这个错误:Uncaught TypeError:Can not read pr operty'oFeatures'为null – nielsv

+0

你将不得不通过jsfiddle发布你的整个源代码,以便排除故障......某些东西没有重击并且没有查看所有的源代码我只能猜测 –

+0

好的,我已经制作了一个jsfiddle。它不工作,链接不正确,但你可以看到我的代码是怎样的... http://jsfiddle.net/8TwS7/ – nielsv