获取数据表中选定行的第一列值
Get selected rows first column value in datatable
$('.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);
});
这是 select 多行的代码。单击按钮时,我需要获取 selected 行第一列值并存储在数组 dataArr[] 中。请帮我解决这个问题。
试试这个:
$('#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);
});
更新
您也可以使用 dataTables
的一些原生功能,如下所示:
$('#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);
});
Guruprasad 代码(完美),带有一些方便的小更新。
var table= $('#YourTableName').DataTable();
$('#button').click(function () {
var arr = [];
$.each(table.rows('.selected').data(), function() {
arr.push(this["ColomnName"]);
});
});
$('.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);
});
这是 select 多行的代码。单击按钮时,我需要获取 selected 行第一列值并存储在数组 dataArr[] 中。请帮我解决这个问题。
试试这个:
$('#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);
});
更新
您也可以使用 dataTables
的一些原生功能,如下所示:
$('#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);
});
Guruprasad 代码(完美),带有一些方便的小更新。
var table= $('#YourTableName').DataTable();
$('#button').click(function () {
var arr = [];
$.each(table.rows('.selected').data(), function() {
arr.push(this["ColomnName"]);
});
});