选择Jquery Datatables中的所有行 [英] Select All rows in Jquery Datatables

查看:163
本文介绍了选择Jquery Datatables中的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,我可以在其中选择多个行单击,但是如何在一个点击按钮上选择所有行,同时所有行都突出显示选择(可以给出当前页面的选择示例)和所有页面)。我已经写了一个代码来获取多个选定的值。



复选框也可以做,但是如何获取选定的值。



以下是单次和多次选择的代码。

  var oTable = $(#示例)。dataTable(); 

$('#example tbody')。on('click','tr',function(){
$(this).toggleClass('selected');

});

在按钮提交上获取所选值的代码

  var row = oTable.rows('。selected')。 

var jsonArr =[;

if(row!= null&& row.length){

for(var i = 0; i< row.length; i ++){
var row1 = row [i]; //这将给我一行详细信息
// row1 [0]将给我列详细信息
jsonArr = jsonArr +{\ID\:+ row1 [0] + },

}
jsonArr = jsonArr +];


解决方案

可能会帮助你的是TableTools扩展名。有一个示例,带有select_all和select_none按钮,这些按钮适用于所有页面。 / p>

一个默认限制是select_all将忽略当前的过滤,但是使用下面的代码很容易解决。为fnSelectAll提供true参数将启用过滤器感知选择。

  tableTools:{
sRowSelect:'multi' ,
aButtons:[
{
sExtends:'select_all',
sButtonText:'Select All',
fnClick:function(nButton,oConfig,oFlash){
TableTools.fnGetInstance('example')。fnSelectAll(true);
}
}
]
}


I have a datatables in which I can select multiple row onclick but how can I select all the row on a click of button and at the same time all rows are highlighted with selection (Can you please give example of selection for current page and all the pages). I have written a code to get multiple selected value.

Checkbox will also do but then how to get selected value.

Below is code for single and multiple selection.

 var oTable = $("#example").dataTable();

    $('#example tbody').on('click', 'tr', function() {
                        $(this).toggleClass('selected');

    });

Code to get selected value on button submit.

var row = oTable.rows('.selected').data();

var jsonArr = "[";

        if(row != null && row.length){

            for (var i = 0; i < row.length; i++) {
                var row1 = row[i]; // this will give me one row details
                        // row1[0] will give me column details
                        jsonArr = jsonArr + "{\"ID\":" + row1[0] + "},";

                }   
             jsonArr = jsonArr + "]";

解决方案

What will probably help you is TableTools extension. There is an example with select_all and select_none buttons, and those work for all pages.

One default limitation is that select_all will ignore current filtering, but that is easy to solve using the code below. Providing "true" argument to fnSelectAll enables the filter-aware selection.

tableTools: {
    sRowSelect: 'multi',
    aButtons: [
        {
            sExtends: 'select_all',
            sButtonText: 'Select All',
            fnClick: function (nButton, oConfig, oFlash) {
                TableTools.fnGetInstance('example').fnSelectAll(true);
            }
        }
    ]
}

这篇关于选择Jquery Datatables中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆