且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery数据表从所选行中获取数据

更新时间:2023-12-03 16:13:40

doc 您可以执行以下操作:

  var table = $('#mytable')。DataTable(); 
var rows_selected = table.rows('。row_selected')。data();
$ .each(rows_selected,function(i,v){
apprReq.push(v);
});


I have a jquery datatable, and have checkboxes which set the row selected class to the rows which are selected. However when I try to get the first column data from these selected rows, it gives me objects.

I am unable to get to the problem inspire of going through a number of posts on the subjects on the data tables and stack overflow forums.

The codes are provided below.

Regards, Arjun

HTML:

<table id="mytable" class="table table-striped table-bordered" cellspacing="0" width="100%">

    <thead>
            <tr>
                <th><input type="checkbox" name= "check_all" id="check_all"/></th>
                <th>Request ID</th>
                <th>Request Date</th>
                <th>Leave Type</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>Status</th>
            </tr>
        </thead>
    </table>

Javscript

$("#check_all").click(function () {
    $(".checkBoxClass").prop('checked', $(this).prop('checked'));
});

$(".checkBoxClass").change(function(){
    if (!$(this).prop("checked")){
        $("check_all").prop("checked",false);
        $(this).closest('tr').removeClass('row_selected');
    }
        $(this).closest('tr').addClass('row_selected');
});

function sendval(){
    var apprReq = [];
    var rejReq = [];

    var otable = $('#mytable').DataTable();
    var aTrs = otable.rows();

    for ( var i=0 ; i<aTrs.length ; i++ )                                       
    {
        if ( $(aTrs[i]).hasClass('row_selected') )
        {
            apprReq.push( aTrs.rows().data[i] );
        }
            rejReq.push( aTrs[i] );
    }
    console.log(apprReq);
    console.log(rejReq);

}

from datables doc you can do:

var table = $('#mytable').DataTable();
var rows_selected = table.rows('.row_selected').data();
$.each(rows_selected,function(i,v){
    apprReq.push(v);
});