数据表 - 刷新/重新加载ajax不起作用

时间:2015-12-21 14:55:15

标签: javascript jquery ajax datatables

大家好我能够成功地创建一个可以从ajax调用中获取数据的数据表。所以对于初始加载工作正常。我创建了一个名为refresh的按钮,我希望如果我调用我的函数initiatearchiveIncidents,它将再次获取我的数据并重新加载表,但我得到错误cannot re-initialise the Datatable

所以我尝试了dtTable.ajax.reload();,但这失败了,因为它理解我的ajax调用。有人会知道如何正确地做到这一点吗?

    initiatearchiveIncidents: function () {

        $.getJSON(this.basePath() + '/XPROD_Incidents?$filter=ÉtatValue%20eq%20%27Fermé%27&$orderby=Modifié asc',
            function (data) {
            //Sharepoint REST returns a nested data format, so we go deep to get the useful parts
              var myData = data.d.results;
              moment.locale('fr');

            //this is the key part of datatables       
                dtTable = $('#example').DataTable({

                    data: myData,
                    columns:[
                        {
                                "width": "30px",
                                sortable: false,
                                "render": function (data, type, full, meta) {
                                return "<button class='edit_button btn btn-default btn-sm' name ='btnSubmit' type='button' value='Edit' data-ID='"+full.ID+"'><i class='fa fa-pencil fa-2'></i></button>";
                                }
                        },
                        { data: "Incident", "width": "75px" },        
                        {
                                "width": "65px",
                                sortable: false,
                                "render": function (data, type, full, meta) {
                                return "<h4><span class='priorité_span'>"+full.PrioritéValue+ "</span></h4>";
                                }
                        },      
                        { data: "Composante","width": "75px",className: "over_flow_control" },         
                        { data: "Description","width": "300px",className: "over_flow_control" },
                        { data: "Début_imputabilité","width": "100px"  },
                        { data: "ResponsableValue","width": "100px",className: "over_flow_control"  }

                        ],
                        "order": [[ 5, "desc" ]],
                    "drawCallback": function( settings ) {
                    IncidentManager.table_conditional_format();
                    $('#loading').hide("slow");
                    },

                })
            });

    },  

1 个答案:

答案 0 :(得分:0)

您需要重写initiatearchiveIncidents()函数,以便在ajax.urlajax.dataSrc选项中使用jQuery DataTables Ajax功能,请参阅下文。

如果您需要重新加载数据,请致电$('#example').DataTable().ajax.reload(),有关详细信息,请参阅ajax.reload()

initiatearchiveIncidents: function() {

    moment.locale('fr');

    //this is the key part of datatables       
    dtTable = $('#example').DataTable({
        ajax: {
            url: this.basePath() + '/GDI_PROD_Incidents?$filter=ÉtatValue%20eq%20%27Fermé%27&$orderby=Modifié asc',
            dataSrc: "d.results"
        },
        columns: [
            {
                "width": "30px",
                sortable: false,
                "render": function(data, type, full, meta) {
                    return "<button class='edit_button btn btn-default btn-sm' name ='btnSubmit' type='button' value='Edit' data-ID='" + full.ID + "'><i class='fa fa-pencil fa-2'></i></button>";
                }
            }, {
                data: "Incident",
                "width": "75px"
            }, {
                "width": "65px",
                sortable: false,
                "render": function(data, type, full, meta) {
                    return "<h4><span class='priorité_span'>" + full.PrioritéValue + "</span></h4>";
                }
            }, {
                data: "Composante",
                "width": "75px",
                className: "over_flow_control"
            }, {
                data: "Description",
                "width": "300px",
                className: "over_flow_control"
            }, {
                data: "Début_imputabilité",
                "width": "100px"
            }, {
                data: "ResponsableValue",
                "width": "100px",
                className: "over_flow_control"
            }

        ],
        "order": [[5, "desc"]],
        "drawCallback": function(settings) {
            IncidentManager.table_conditional_format();
            $('#loading').hide("slow");
        }

    });
},