在datatable中获取所选行的第一列值 [英] Get selected rows first column value in datatable

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

问题描述

on('click','tr',function(){
var id = this.id;

  $ var index = $ .inArray(id,selected); 
if(index === -1)
{
selected.push(id);
} else
{
selected.splice(index,1);
}
$(this).toggleClass('selected');
});
$('#btn')。click(function(){
var dataArr = [];
var rowCount = table.rows('。selected' ;
alert(rowCount);
});

这里是选择多行的代码。点击按钮,我需要获取所选行的第一列值并存储在数组dataArr []中。请帮我解决这个问题。

解决方案

尝试这样:



演示

  $('#btn')。click(function(){
var dataArr = [];
$ .each($(#example tr.selected),function(){// get每个tr已经选择了类
dataArr.push($(this).find('td')。eq(0).text()); //找到它的第一个td并推值
//dataArr.push($(this).find('td:first')。text());你也可以使用这个
});
console.log(dataArr);
});






更新 p>

您可以使用 dataTables 的一些本地功能,如下所示:



$ $ $ $ $ $'$''''''''''''''''''''''''''' ');
var rowData = table.rows(rows).data();
$ .each($(rowData),function(key,value){
dataArr.push [name]); //name是第一列的值
});
console.log(dataArr);
});

UPDATED DEMO


$('.example tbody').on('click', 'tr', function (){
    var id = this.id;
    var index = $.inArray(id, selected);
    if (index === -1) 
    {
    selected.push(id);
    } else 
    {
    selected.splice(index, 1);
    }
    $(this).toggleClass('selected');
});
$('#btn').click(function (){
    var dataArr = [];
    var rowCount = table.rows('.selected').data().length;
    alert(rowCount);
});

Here is the code for select multiple rows. On clicking the button I need to get the selected rows first column value and store in array dataArr[]. Please help me to fix this.

解决方案

Try this:

DEMO

$('#btn').click(function (){
    var dataArr = [];
    $.each($("#example tr.selected"),function(){ //get each tr which has selected class
        dataArr.push($(this).find('td').eq(0).text()); //find its first td and push the value
        //dataArr.push($(this).find('td:first').text()); You can use this too
    });
    console.log(dataArr);
});


UPDATE

You can get using some native functions of dataTables too as below:

$('#btn').click(function (){
    var dataArr = [];
    var rows = $('tr.selected');
    var rowData = table.rows(rows).data();
    $.each($(rowData),function(key,value){
        dataArr.push(value["name"]); //"name" being the value of your first column.
    });
    console.log(dataArr);
});

UPDATED DEMO

这篇关于在datatable中获取所选行的第一列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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