Get selected rows first column value in datatable

前端 未结 2 668
夕颜
夕颜 2021-01-18 03:42
$(\'.example tbody\').on(\'click\', \'tr\', function (){
    var id = this.id;
    var index = $.inArray(id, selected);
    if (index === -1) 
    {
    selected.pus         


        
相关标签:
2条回答
  • 2021-01-18 04:16

    Guruprasad code (perfect) with small handy updates.

    var table= $('#YourTableName').DataTable();
    $('#button').click(function () {
               var arr = [];
               $.each(table.rows('.selected').data(), function() {
                   arr.push(this["ColomnName"]);
               });
    });
    
    0 讨论(0)
  • 2021-01-18 04:40

    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

    0 讨论(0)
提交回复
热议问题