JQuery 2数据表使用一页

时间:2017-12-22 14:43:22

标签: jquery ajax datatables

我的页面中有两个表,我使用Ajax获取此数据。现在我想添加默认包含添加按钮。当我点击按钮时,它将带有一个id。但是当我通常为一张桌子做它的工作时。但是,当我使用两个具有不同id的表时,它不能正常工作,它的ID不能准确显示。

$(function () {
        var table = $('#example6').DataTable({
            'paging': true,
            "serverSide": true,
            ajax: {
                url: "{{ url('/manage-order/data') }}",
                type: 'post'
            },

            "columns": [

                {"data": "vn_name"},
                {"data": "vn_id"},
                {"data": "total_payment_amount"},
                {"data": "total_due_ammont"},

                {
                    "data": null,
                    "defaultContent": "<a id='addBtn' class='btn btn-success btn-xs' title='Add Purchase'>\n" +
                    "<span class='glyphicon glyphicon-plus'></span>\n" +
                    "</a>\n" 
                }
            ]
        });

        var table = $('#example5').DataTable({
            'paging': true,
            "serverSide": true,
            ajax: {
                url: "{{ url('/manage-general-sales/data') }}",
                type: 'post'
            },
            "columns": [

                {"data": "vendor_name"},
                {"data": "total_payment_amount"},
                {"data": "total_due_amount"},
                {
                    "data": null,
                    "defaultContent": "<a id='addBtnGneral' class='btn btn-success btn-xs' title='Add Purchase'>\n" +
                    "<span class='glyphicon glyphicon-plus'></span>\n" +
                    "</a>\n" 
                }
            ]
        });
        $('#example6 tbody').on('click', '#addBtn', function () {
            var data = table.row($(this).parents('tr')).data();
            var vn_id = data['vn_id'];
            var url = "{{url('/create-order/')}}/" + vn_id;
            window.location.href = url;
        });


        $('#example5 tbody').on('click', '#addBtnGneral', function () {
            var data = table.row($(this).parents('tr')).data();
            var id = data['id'];
            var url = "{{url('/create-general-sales/')}}/" + id;
            window.location.href = url;
        });

    })

有人请帮助我。

1 个答案:

答案 0 :(得分:0)

您使用了相同的table变量,您应该使它们不同,以便对它们执行某些操作。

$(function () {
        var table1 = $('#example6').DataTable({
            'paging': true,
            "serverSide": true,
            ajax: {
                url: "{{ url('/manage-order/data') }}",
                type: 'post'
            },

            "columns": [

                {"data": "vn_name"},
                {"data": "vn_id"},
                {"data": "total_payment_amount"},
                {"data": "total_due_ammont"},

                {
                    "data": null,
                    "defaultContent": "<a id='addBtn' class='btn btn-success btn-xs' title='Add Purchase'>\n" +
                    "<span class='glyphicon glyphicon-plus'></span>\n" +
                    "</a>\n" 
                }
            ]
        });

        var table2 = $('#example5').DataTable({
            'paging': true,
            "serverSide": true,
            ajax: {
                url: "{{ url('/manage-general-sales/data') }}",
                type: 'post'
            },
            "columns": [

                {"data": "vendor_name"},
                {"data": "total_payment_amount"},
                {"data": "total_due_amount"},
                {
                    "data": null,
                    "defaultContent": "<a id='addBtnGneral' class='btn btn-success btn-xs' title='Add Purchase'>\n" +
                    "<span class='glyphicon glyphicon-plus'></span>\n" +
                    "</a>\n" 
                }
            ]
        });
        $('#example6 tbody').on('click', '#addBtn', function () {
            var data = table1.row($(this).parents('tr')).data();
            var vn_id = data['vn_id'];
            var url = "{{url('/create-order/')}}/" + vn_id;
            window.location.href = url;
        });


        $('#example5 tbody').on('click', '#addBtnGneral', function () {
            var data = table2.row($(this).parents('tr')).data();
            var id = data['id'];
            var url = "{{url('/create-general-sales/')}}/" + id;
            window.location.href = url;
        });

    })