使用选定的复选框进行分页。复选框仅适用于当前分页页面。 jQuery 数据表
我正在使用 jquery 数据表插件。
我只有一个直接的 html 表格布局。
<table class="display" id="contactsTable">
<thead>
<tr>
<th>Id</th>
<th>Email</th>
<th>Name</th>
<th>Phone</th>
<th>City</th>
<th>State</th>
<th>Arrival</th>
<th>Departure</th>
<th>Inserted</th>
<th>Check</th>
</tr>
</thead>
<tbody>
<tr>
<td>301</td>
<td>email address</td>
<td>Test</td>
<td></td>
<td></td>
<td></td>
<td>July 14 2011</td>
<td>July 23 2011</td>
<td>April 12 2011 07:05</td>
<td><input type="checkbox" name="selected[]" value="301" class="chkbox"/></td>
</tr>
<tr>
<td>300</td>
<td>email</td>
<td>Test</td>
<td></td>
<td></td>
<td></td>
<td>September 02 2011</td>
<td>September 10 2011</td>
<td>April 11 2011 12:01</td>
<td><input type="checkbox" name="selected[]" value="300" class="chkbox"/></td>
</tr>
这是我的提交代码(只是临时的)。
<input id="submitButton" type="submit" value="Submit" onclick="test()" />
我的 javascript 将复选框映射到数组。
function test() {
var values = $('input:checkbox:checked.chkbox').map(function () {
return this.value;
}).get();
console.log(values);
}
这是数据表代码
$(document).ready(function() {
var selected;
var selectedOutput = '#selectedOutput';
var template = 'selectedTemplate';
var submitButton = '#submitButton'
var dTable = $('#contactsTable').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bPaginate": true,
"bLengthChange": true,
"bFilter": true,
"bSort": false,
"bInfo": true,
"bAutoWidth": false,
"bProcessing": true,
"aoColumns": [
{"bVisible": false }, //keep the id invisble
null,
null,
null,
null,
null,
null,
null,
null,
null
]
});
问题是,我无法在页面上进行复选框选择。该表有多个页面。如果我单击“提交”,它只会提交我所在的当前页面的复选框数组。
我希望这已经足够清楚了。我不确定发生了什么事。感谢您的任何帮助。
I'm using the jquery datatables plugin.
I have just a straight html table layout for it.
<table class="display" id="contactsTable">
<thead>
<tr>
<th>Id</th>
<th>Email</th>
<th>Name</th>
<th>Phone</th>
<th>City</th>
<th>State</th>
<th>Arrival</th>
<th>Departure</th>
<th>Inserted</th>
<th>Check</th>
</tr>
</thead>
<tbody>
<tr>
<td>301</td>
<td>email address</td>
<td>Test</td>
<td></td>
<td></td>
<td></td>
<td>July 14 2011</td>
<td>July 23 2011</td>
<td>April 12 2011 07:05</td>
<td><input type="checkbox" name="selected[]" value="301" class="chkbox"/></td>
</tr>
<tr>
<td>300</td>
<td>email</td>
<td>Test</td>
<td></td>
<td></td>
<td></td>
<td>September 02 2011</td>
<td>September 10 2011</td>
<td>April 11 2011 12:01</td>
<td><input type="checkbox" name="selected[]" value="300" class="chkbox"/></td>
</tr>
Here is my code for submit (just temp).
<input id="submitButton" type="submit" value="Submit" onclick="test()" />
And my javascript to map the checkboxes to an array.
function test() {
var values = $('input:checkbox:checked.chkbox').map(function () {
return this.value;
}).get();
console.log(values);
}
Here is the datatables code
$(document).ready(function() {
var selected;
var selectedOutput = '#selectedOutput';
var template = 'selectedTemplate';
var submitButton = '#submitButton'
var dTable = $('#contactsTable').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bPaginate": true,
"bLengthChange": true,
"bFilter": true,
"bSort": false,
"bInfo": true,
"bAutoWidth": false,
"bProcessing": true,
"aoColumns": [
{"bVisible": false }, //keep the id invisble
null,
null,
null,
null,
null,
null,
null,
null,
null
]
});
The problem is, I can't do a checkbox selection across a page. The table has mutiple pages to it. If I click submit, it only submits the array of checkboxes for the current page I am on.
I hope this is clear enough. I'm not sure what is happening. Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你可能会看看 这个,它可能会解决你的问题。
这个例子可能对您有帮助,因为它似乎正是您的情况(这个例子使用 fnGetNodes(),而在另一个链接中解释了如果第一个函数不起作用,如何使用 fnGetHiddenNodes() ): http://datatables.net/examples/api/form.html
i think you might have a look at this, it might solve your problem.
This example might be of help to you as it seems to be exactly your case (this one uses fnGetNodes(), while in the other link it is explained how to use fnGetHiddenNodes() if the first function doesn't work ): http://datatables.net/examples/api/form.html
原因
出于性能原因,jQuery DataTables 从 DOM 中删除不可见的行,这就是为什么当您提交表单时仅提交可见的复选框。
解决方案
您可能需要将那些被选中且 DOM 中不存在的
转换为
提交表单时。
例如,要提交包含所有复选框值的表单:
如果您通过 Ajax 提交表单,那就更简单了。
例如,要通过 Ajax 提交包含所有复选框值的表单:
DEMO
请参阅 jQuery DataTables:如何提交所有页面表单数据以获取更多信息和演示。
CAUSE
jQuery DataTables removes non-visible rows from DOM for performance reasons, that is why when you submit the form only visible checkboxes get submitted.
SOLUTION
You may need to turn those
<input type="checkbox">
that are checked and don't exist in DOM into<input type="hidden">
upon form submission.For example, to submit form with values of all checkboxes:
If you're submitting the form via Ajax, it's even simpler.
For example, to submit form via Ajax with values of all checkboxes:
DEMO
See jQuery DataTables: How to submit all pages form data for more information and demonstration.
这应该可以为您解决问题
This should solve the issue for you