简体   繁体   中英

Get multiple value that selected from Datatable Jquery

I have some issues in getting values from datatable that i imported from excel.

i Want to pass the rows that selected (at least can be viewed in alert), here's the case.

the value that i want is like

Name : A. Mused , No HP : 087.... Name : Aida Bugg, No HP : 089.... Name : Allie Grater, No HP : 087...

but the result is just like this screenshot : result popup alert and data

here's the code :

html

 @if (Model != null)
    {
        <table id="tablePenerima" class="table table-striped table-bordered animate__animated animate__fadeInRight" cellpadding="0" cellspacing="0">
            <thead>
                <tr>
                    
                    @foreach (DataColumn col in Model.Tables[0].Columns)
                    {

                        <th align="center">@col.ColumnName</th>
                    }
                </tr>
            </thead>
            <tbody>
                @foreach (DataRow row in Model.Tables[0].Rows)
                {
                    <tr >
                       
                        @foreach (DataColumn col in Model.Tables[0].Columns)
                        {

                            <td align="center">@row[col.ColumnName]</td>
                        }

                    </tr>
                }
            </tbody>
        </table>
    }

and the javascript :

 $(document).ready(function () {
        var table = $('#tablePenerima').DataTable({
            dom: 'Bfrtip',
            buttons: [
               
                'selectAll',
                'selectNone',
                
            ],
            select: true
        });

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

        $('#btnBlast').click(function () {
            var ids = $.map(table.rows('.selected').data(), function (item) {
                return item[0]
            });
            var data = $('#tablePenerima').DataTable().row('.selected').ids();
            console.log(ids);
            alert("Name:" + ids[0] + "\nNo HP:" + ids[2]);

           });
    });

I hope all of you can solve my problem because my knowledge in js is still weak. Thanks :)

I have no experience with DataTable package, but i think the problem is your map function. It returns the first row of selected rows.

var ids = $.map(table.rows('.selected').data(), function (item) {
                return item[0]
            });

Instead of

return item[0]

You should return all selected rows;

return item

In $('#btnBlast').click event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM