我有一个表格可以过滤掉表格中的数据。但是我对如何将数据传递给DataTables以及如何在任何选择或输入更改上刷新表格提出了一些问题。
以下是代码的一部分:
function renderDataTable(selector) {
var out = [];
var tables = jQuery(selector);
var sorting;
for ( var i=0, iLen=tables.length ; i<iLen ; i++ ){
var defaultCol = jQuery('th', tables[i]).index(jQuery(".dataTable-defaultSort",tables[i]));
if(defaultCol >= 0){
sorting = [ defaultCol, 'desc' ];
}else{
sorting = [12,'desc'];
}
var oTable2 = jQuery(tables[i]).dataTable({
"sDom": 'T<"clearfix">lfrt<"clearfix">ip',
"aaSorting": [ sorting ],
"bStateSave": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "list.php",
"iDisplayLength": 20,
"aLengthMenu": [[20, 50, 100], [20, 50, 100]],
"sPaginationType": "full_numbers",
});
out.push( oTable2 );
}
return out;
}
$(document).ready(function() {
renderDataTable("#main_table");
$("select#myVar").change(function () {
var myVar = $(this).val();
// push data to table and refresh?
});
});
有人可以帮帮我吗?提前完成。
答案 0 :(得分:4)
好的,找到了答案,我在这里发布,所以任何需要相同的人都可以使用它。 我发现我正在寻找的东西可以这样做:
<script type="text/javascript" charset="utf-8" src="jquery.dataTables.fnReloadAjax.js"></script>
<script type="text/javascript" charset="utf-8">
function renderDataTable(selector) {
var out = [];
var tables = jQuery(selector);
var sorting;
for ( var i=0, iLen=tables.length ; i<iLen ; i++ ){
var defaultCol = jQuery('th', tables[i]).index(jQuery(".dataTable-defaultSort",tables[i]));
if(defaultCol >= 0){
sorting = [ defaultCol, 'desc' ];
}else{
sorting = [12,'desc'];
}
var data1 = $("#data1").val();
var data2 = $("#data2").val();
var oTable2 = jQuery(tables[i]).dataTable({
"sDom": 'T<"clearfix">lfrt<"clearfix">ip',
"aaSorting": [ sorting ],
"bStateSave": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "<?=LOC_BASE?>gmoulds/moldes_estatisticas/moldes_listagem_01_busca.php",
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name": "data1", "value": $("#data1").val() } );
aoData.push( { "name": "data2", "value": $("#data2").val() } );
$.getJSON( sSource, aoData, function (json) {
fnCallback(json);
} );
},
"iDisplayLength": 20,
"aLengthMenu": [[20, 50, 100], [20, 50, 100]],
"sPaginationType": "full_numbers",
"sScrollX": "100%",
"bScrollCollapse": true
});
out.push( oTable2 );
$("#data1, #data2").keyup(function(e) {
oTable2.fnReloadAjax();
});
}
return out;
}
$(document).ready(function() {
renderDataTable("#main_table_estatistica");
});
</script>
函数fnReloadAjax()不包含在jquery.dataTables.min.js中,但可以在http://www.datatables.net/plug-ins/api#fnGetHiddenNodes
找到它感谢ShadowScripter指出了一些方向。
答案 1 :(得分:0)
function renderDataTable(selector) {
var out = [];
var tables = jQuery(selector);
var sorting;
for ( var i=0, iLen=tables.length ; i<iLen ; i++ ){
var defaultCol = jQuery('th', tables[i]).index(jQuery(".dataTable-defaultSort",tables[i]));
if(defaultCol >= 0){
sorting = [ defaultCol, 'desc' ];
}else{
sorting = [12,'desc'];
}
var data1 = $("#data1").val();
var data2 = $("#data2").val();
var oTable2 = jQuery(tables[i]).dataTable({
"sDom": 'T<"clearfix">lfrt<"clearfix">ip',
"aaSorting": [ sorting ],
"bStateSave": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "<?=LOC_BASE?>gmoulds/moldes_estatisticas/moldes_listagem_01_busca.php",
"fnServerParams": function (aoData) {
var searcharray = ({name: "search", value: 'aaa'});
aoData['searchdata'] = searcharray;
},
"iDisplayLength": 20,
"aLengthMenu": [[20, 50, 100], [20, 50, 100]],
"sPaginationType": "full_numbers",
"sScrollX": "100%",
"bScrollCollapse": true
});
out.push( oTable2 );
$("#data1, #data2").keyup(function(e) {
oTable2.fnReloadAjax();
});
}
return out;
}
$(document).ready(function() {
renderDataTable("#main_table_estatistica");
});