gpt4 book ai didi

jquery - 使用附加参数更新服务器处理的 DataTables 源

转载 作者:行者123 更新时间:2023-12-03 22:31:48 27 4
gpt4 key购买 nike

当单击提交输入按钮时,我尝试将其他参数(选定复选框的列表)传递到服务器处理的 DataTables 表#my_table:

screenshot

这可能意味着我必须将 my_table.sAjaxSource 设置为后端脚本加上编译的复选框列表,然后调用 my_table.fnDraw() ?

我准备了一个非常简单的测试用例 - test.php:

<?php
error_log('QUERY_STRING: ' . $_SERVER['QUERY_STRING']);
?>

index.html:

<html>
<head>
<style type="text/css" title="currentStyle">
@import "/css/demo_table_jui.css";
@import "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/redmond/jquery-ui.css";
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>;
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>;
<script type="text/javascript" src="/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(function() {
my_table = $('#my_table').dataTable( {
bJQueryUI: true,
bServerSide: true,
bProcessing: true,
sAjaxSource: '/test.php'
} );
});

var my_table;

function redrawTable() {
var str = '';
var boxes = new Array();

//loop through all checkboxes
$(':checkbox').each(function() {
if ($(this).is(':checked')) {
boxes.push($(this).attr('name') + '=' + $(this).val());
}
});

str = '/test.php?' + boxes.join('&');
// TODO: set my_table.sAjaxSource to str
my_table.fnDraw();
}
</script>
</head>
<body>

<p>Select fruit:</p>
<p><label><input type="checkbox" name="fruits" value="apple">apple</label></p>
<p><label><input type="checkbox" name="fruits" value="banana">banana</label></p>
<p><label><input type="checkbox" name="fruits" value="pear">pear</label></p>

<p>Select candy:</p>
<p><label><input type="checkbox" name="candy" value="toffee">toffee</label></p>
<p><label><input type="checkbox" name="candy" value="fudge">fudge</label></p>

<p><input type="button" onclick="redrawTable();" value="Submit"></p>

<table class="display" id="my_table">

<thead>
<tr>
<th>Column_1</th>
<th>Column_2</th>
<th>Column_3</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

</body>
</html>

请告诉我如何实现这一点(将自定义参数传递给 DataTables AJAX 源脚本)。

更新:这段代码似乎对我来说效果很好,谢谢尼古拉

<html>
<head>
<style type="text/css" title="currentStyle">
@import "/css/demo_table_jui.css";
@import "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/redmond/jquery-ui.css";
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript" src="/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">

var my_table;

$(function() {
my_table = $('#my_table').dataTable( {
bJQueryUI: true,
bServerSide: true,
bProcessing: true,
sAjaxSource: '/test.php',
fnServerParams: function ( aoData ) {
$(':checkbox').each(function() {
if ($(this).is(':checked')) {
aoData.push( { name: $(this).attr('name'), value: $(this).val() } );
}
});
}
});
});

</script>
</head>
<body>

<p>Select fruit:</p>
<p><label><input type="checkbox" name="fruits" value="apple">apple</label></p>
<p><label><input type="checkbox" name="fruits" value="banana">banana</label></p>
<p><label><input type="checkbox" name="fruits" value="pear">pear</label></p>

<p>Select candy:</p>
<p><label><input type="checkbox" name="candy" value="toffee">toffee</label></p>
<p><label><input type="checkbox" name="candy" value="fudge">fudge</label></p>

<p><input type="button" onclick="my_table.fnDraw();" value="Submit"></p>

<table class="display" id="my_table">

<thead>
<tr>
<th>Column_1</th>
<th>Column_2</th>
<th>Column_3</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

</body>
</html>

在 error_log 中我看到:

QUERY_STRING: 
sEcho=2&
iColumns=3&
sColumns=&
iDisplayStart=0&
iDisplayLength=10&
mDataProp_0=0&
mDataProp_1=1&
mDataProp_2=2&
sSearch=&
bRegex=false&
sSearch_0=&
bRegex_0=false&
bSearchable_0=true&
sSearch_1=&
bRegex_1=false&
bSearchable_1=true&
sSearch_2=&
bRegex_2=false&
bSearchable_2=true&
iSortingCols=1&
iSortCol_0=0&
sSortDir_0=asc&
bSortable_0=true&
bSortable_1=true&
bSortable_2=true&
fruits=apple&
fruits=banana&
candy=toffee&
candy=fudge&
_=1317666289823

最佳答案

this 可以看出例如,您应该使用 fnServerParams:

"fnServerParams": function ( aoData ) {
aoData.push( { "name": "more_data", "value": "my_value" } );
}

其中 aoData 是要发送到服务器的对象数组

关于jquery - 使用附加参数更新服务器处理的 DataTables 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7629218/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com