javascript JQuery DataTables 使用分页时如何从表中获取选定的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14321353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:18:09  来源:igfitidea点击:

JQuery DataTables How to get selected rows from table when we using paging?

javascriptjquerydatatables

提问by SashaMo

For example I selected (checked) 2rows from second page than go to first page and select 3rows. I want get information from 5selected rows when I stay at first page.

例如,我从第二页选择(选中)2行而不是转到第一页并选择3行。当我停留在第一页时,我想从5 个选定的行中获取信息。

$('tr.row_selected') - not working

$('tr.row_selected') - 不工作

Thanks.

谢谢。

Upd.

更新。

I created handler somthing like this:

我创建了这样的处理程序:

$('#example').find('tr td.sel-checkbox').live("click", function () { /*code here*/ });

$('#example').find('tr td.sel-checkbox').live("click", function () { /*code here*/ });

But right now when click event is hadle the row from table is hidding. I think it may be sorting or grouping operation of DataTables. Any idea what I must do with this?

但是现在当点击事件发生时,表格中的行正在隐藏。我认为可能是DataTables的排序或分组操作。知道我必须用这个做什么吗?

回答by allanx2000

When a checkbox gets selected, store the row information you want in a global object as a Key-Value pair

当复选框被选中时,将所需的行信息作为键值对存储在全局对象中

I don't remember specifically how i did it before but the syntax was something like

我不记得我以前是怎么做的,但语法有点像

$('input[type=checkbox]').click(function()
{
    var row = $(this).parent(); //this or something like it, you want the TR element, it's just a matter of how far up you need to go
    var columns = row.children(); //these are the td elements

    var id = columns[0].val(); //since these are TDs, you may need to go down another element to get to the actual value

    if (!this.checked) //becomes checked (not sure may be the other way around, don't remember when this event will get fired)
    {
        var val1 = columns[1].val();
        var val2 = columns[2].val();

        myCheckValues[id] =[val1,val2]; //Add the data to your global object which should be declared on document ready
    }
    else delete myCheckValues[id];
});

When you submit, get the selected rows from your object:

提交时,从对象中获取选定的行:

for (var i = 0; i < myCheckValues.length; i++)
...

Sorry, haven't done JS in a long time so code as is might not work but you get the idea.

抱歉,很长时间没有使用 JS,因此代码可能无法正常工作,但您明白了。

回答by SashaMo

$('#example').find('tr td.sel-checkbox').live("click", function () {
            var data = oTable.fnGetData(this);
            // get key and data value from data object
            var isSelected = $(this).hasClass('row_selected');
            if(isSelected) {
                myCheckValues[key] = value;
                checkedCount++;
            } else {
                delete myCheckValues[key];
                checkedCount--;
            }
        });

..... On submit

..... 提交时

if(checkedCount > 0) {
            for(var ArrVal in myCheckValues) {
                var values = myCheckValues[ArrVal];   // manipulate with checked rows values data        
            }
        }