2013-11-15 34 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