How to get current table id to add to ajax data when initialisation is used for multiple tables?

风格不统一 提交于 2019-12-25 13:19:48

问题


I have the following initialisation for multiple tables with the same class but different id attributes:

var $table = $('table.jobs');

$table.DataTable({

    ....
    ajax: {
        url: '/my-url',
        dataSrc: function (json) {

            return json.data
        },
        data: function(data) {

            data.table_id = $table.attr('id');
            // gives the same id for all tables

        }
    },
    ...
});

Is there a way I can identify which table is sending the ajax request? I'm trying to avoid duplicating the entire initialisation for every table.


回答1:


You can try something like this:

$('.table.jobs').each(function () {
    $('#'+$(this).attr('id')).dataTable({
         url: '/my-url',
    dataSrc: function (json) {

        return json.data
    },
    data: function(data) {

        data.table_id = $table.attr('id');
        // gives the same id for all tables

    }
});


来源:https://stackoverflow.com/questions/46036269/how-to-get-current-table-id-to-add-to-ajax-data-when-initialisation-is-used-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!