简体   繁体   English

数据表ajax过滤器重载数据

[英]datatable ajax filter reload data

I have a datatable with data coming from ajax. 我有一个数据表,数据来自ajax。 I have some input fields and when hiting a button I would like the datatable to make a new ajax request and reload the data. 我有一些输入字段,单击按钮时,我希望数据表发出一个新的ajax请求并重新加载数据。

I have: 我有:

<script>
    var domain = [];
    var subdomain = [];
    var job_role = [];
    var month = [];
    var meta_activity = [];

    $.fn.dataTableExt.oApi.fnFilterAll = function (oSettings, sInput, iColumn, bRegex, bSmart) {
        var settings = $.fn.dataTableSettings;

        for (var i = 0; i < settings.length; i++) {
            settings[i].oInstance.fnFilter(sInput, iColumn, bRegex, bSmart);
        }
    };

    $(document).ready(function() {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        //Init select2 boxes
        $("#domain").select2({
            allowClear: true,
            ajax: {
                url: "{!! route('ajaxlistdomain') !!}",
                dataType: 'json',
                type: "GET",
                processResults: function (data) {
                    return {
                        results: $.map(data, function (item) {
                            return {
                                text: item.text,
                                id: item.text
                            }
                        })
                    };
                }
            }
        });
        //Init select2 boxes
        $("#subdomain").select2({
            allowClear: true,
            ajax: {
                url: "{!! route('ajaxlistsubdomain') !!}",
                dataType: 'json',
                type: "GET",
                processResults: function (data) {
                    return {
                        results: $.map(data, function (item) {
                            return {
                                text: item.text,
                                id: item.text
                            }
                        })
                    };
                }
            }
        });
        //Init select2 boxes
        $("#jobrole").select2({
            allowClear: true,
            ajax: {
                url: "{!! route('ajaxlistjobrole') !!}",
                dataType: 'json',
                type: "GET",
                processResults: function (data) {
                    return {
                        results: $.map(data, function (item) {
                            return {
                                text: item.text,
                                id: item.text
                            }
                        })
                    };
                }
            }
        });
        //Init select2 boxes
        $("#month").select2({
            allowClear: true,
            data:[
                { id: 'Jan', text: 'Jan' },
                { id: 'Feb', text: 'Feb' },
                { id: 'Mar', text: 'Mar' },
                { id: 'Apr', text: 'Apr' },
                { id: 'May', text: 'May' },
                { id: 'Jun', text: 'Jun' },
                { id: 'Jul', text: 'Jul' },
                { id: 'Aug', text: 'Aug' },
                { id: 'Sep', text: 'Sep' },
                { id: 'Oct', text: 'Oct' },
                { id: 'Nov', text: 'Nov' },
                { id: 'Dec', text: 'Dec' }
            ]
        });
        //Init select2 boxes
        $("#metaactivity").select2({
            allowClear: true,
            ajax: {
                url: "{!! route('ajaxlistmetaactivity') !!}",
                dataType: 'json',
                type: "GET",
                processResults: function (data) {
                    return {
                        results: $.map(data, function (item) {
                            return {
                                text: item.text,
                                id: item.text
                            }
                        })
                    };
                }
            }
        });

        $("#update").click(function()
            {
                domain = [];
                subdomain = [];
                job_role = [];
                month = [];
                meta_activity = [];

                $("#domain option:selected").each(function()
                    {
                    // log the value and text of each option
                    domain.push($(this).val());
                });
                $("#subdomain option:selected").each(function()
                    {
                    // log the value and text of each option
                    subdomain.push($(this).val());
                });
                $("#jobrole option:selected").each(function()
                    {
                    // log the value and text of each option
                    job_role.push($(this).val());
                });
                $("#month option:selected").each(function()
                    {
                    // log the value and text of each option
                    month.push($(this).val());
                });
                $("#metaactivity option:selected").each(function()
                    {
                    // log the value and text of each option
                    meta_activity.push($(this).val());
                });
                employeeActivityTable.ajax.reload();
                projectActivityTable.ajax.reload();
            }
        );

        var employeeActivityTable = $('#employeeActivityTable').DataTable({
            ajax: {
                    url: "{!! route('ajaxactivityperemployee') !!}",
                    type: "POST",
                    data: {
                        'domain[]': domain,
                        'subdomain[]': subdomain,
                        'job_role[]': job_role,
                        'month[]': month,
                        'meta_activity[]': meta_activity
                          },
                    dataType: "JSON"
                },
            columns: [
                { data: 'employee_id', name: 'employee_id' },
                { data: 'employee_name', name: 'employee_name' },
                { data: 'month', name: 'month' },
                { data: 'sum_task_hour', name: 'sum_task_hour' }
                ],
            columnDefs: [
                {
                    "targets": [ 0 ],
                    "visible": false,
                    "searchable": false
                }
                ]
        } );

        var oTable0 = $("#table1").dataTable();

        $("#Search_All").keyup(function () {
           // Filter on the column (the index) of this element
           oTable0.fnFilterAll(this.value);
        });

        var projectActivityTable = $('#projectActivityTable').DataTable( {
            ajax: {
                    "url": "{!! route('ajaxactivityperproject') !!}",
                    "type": "POST",
                    data: {
                        'domain[]': domain,
                        'subdomain[]': subdomain,
                        'job_role[]': job_role,
                        'month[]': month,
                        'meta_activity[]': meta_activity
                          },
                    dataType: "JSON"
            },
            columns: [
                { data: 'project_id', name: 'project_id' },
                { data: 'customer_name', name: 'customer_name' },
                { data: 'project_name', name: 'project_name' },
                { data: 'meta_activity', name: 'meta_activity' },
                { data: 'employee_id', name: 'employee_id' },
                { data: 'employee_name', name: 'employee_name' },
                { data: 'month', name: 'month' },
                { data: 'sum_task_hour', name: 'sum_task_hour' }
                ],
            columnDefs: [
                {
                    "targets": [ 0 ],
                    "visible": false,
                    "searchable": false
                },
                {
                    "targets": [ 4 ],
                    "visible": false,
                    "searchable": false
                }
                ]
        } );

        var oTable1 = $("#table1").dataTable();

        $("#Search_All").keyup(function () {
           // Filter on the column (the index) of this element
           oTable1.fnFilterAll(this.value);
        });
    } );
</script> 

I have tried also to clear with clear() but nothing is happening. 我也尝试过用clear()清除,但是什么也没有发生。 Also, I don't see any errors in the console, it just doesn't do anything. 另外,我在控制台中看不到任何错误,它什么也没做。

I have tried also to shutdown MySQL database and then I get an error from datatables in the console when I click the button. 我也尝试关闭MySQL数据库,然后单击按钮时从控制台的数据表中收到错误消息。 This shows to me that it is trying to get the new data but it doesn't update them on the screen. 这向我表明,它正在尝试获取新数据,但不会在屏幕上更新它们。

To achieve this, I'd suggest using Datatables build-in reload() capability: 为此,我建议使用Datatables内置的reload()功能:

var table = $('#example').DataTable( {
    ajax: "data.json"
} );

setInterval( function () {
    table.ajax.reload();
}, 30000 );

The above code will refresh a table with given ID example every 3 seconds from the specified ajax source. 上面的代码将从指定的ajax源每3秒刷新一次具有给定ID example的表。

For your personal use, the part you really need is tableVar.ajax.reload(); 对于您的个人用途,您真正需要的部分是tableVar.ajax.reload(); where you replace tableVar with your own variable. 使用您自己的变量替换tableVar

to refresh employeeActivityTable : employeeActivityTable.ajax.reload(); 刷新employeeActivityTableemployeeActivityTable.ajax.reload();

Source: https://datatables.net/reference/api/ajax.reload%28%29 来源: https : //datatables.net/reference/api/ajax.reload%28%29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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