How to get table names dynamically with datatable and Jquery

Viewed 41

I have two tables and I am using multiple datatable

<div class="row">
    <div class="col-sm-12">
        <div class="card-box table-responsive">
            <table id="table1" class="table table-striped table-bordered dt-responsive table-hover" data-table="tableOne">
                <thead>
                    <tr>
                        <th>First name</th>
                        <th>Last name</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-sm-12">
        <div class="card-box table-responsive">
            <table id="table2" class="table table-striped table-bordered dt-responsive table-hover" data-table="tableTwo">
                <thead>
                    <tr>
                        <th>First name</th>
                        <th>Last name</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>

I want to send a parameter that would be the idtable or the name of the table through ajax where I have the attribute for each table data-table .

$('table.table').DataTable({
        "autoWidth": false,
        "paging": true,
        "info": true,
        "responsive": true,
        "filter": true,
        ajax: {
            url: base_url+"controller/action",
            "type": "POST",
            data: function (d) {
                d.data = "data-table"; //id_table
            }
        },
        "stateSave": true,
        "processing": true,
        "serverSide": true,
    });

In theory it should work like this: d.data = $(this).attr("data-table"); of form dynamically , and its result would be for table1 = tableOne and for table2 = tableTwo, but it does not work for me :(

It works for me like this; d.data = $('table.table').attr("data-table"); , but the first table is repeated twice table1 = tableOne and table2 = tableTwe, and that way it doesn't work for me.

Can someone help me please, I would really appreciate it.

0 Answers
Related