Angularjs数据表插件第二次没有绑定

时间:2017-08-24 06:28:42

标签: javascript angularjs datatable angular-datatables

我正在使用angularjs数据表,其中包含2个名为withButton和withColumnfilter的插件。

我正在使用oclazyload加载插件文件。不知何故,当我第一次加载具有上述插件之一的数据表的页面时。它会工作正常,但如果我改变了另一个插件也具有datatble的页面,那么它给了我“不是函数”错误。

$stateProvider.state('firms.browse', {
        url: "/browse",
        templateUrl: "modules/firms/browse/browse.html",
        controller: 'browseController',
        data: {pageTitle: 'Browse Firm'},
        resolve: {
            loadPlugin: function ($ocLazyLoad) {
                return $ocLazyLoad.load([{
                    serie: true,
                    files: ['js/plugins/dataTables/datatables.min.js', 'js/plugins/dataTables/dataTables.columnFilter.js', 'css/plugins/dataTables/datatables.min.css']
                }, {
                    serie: true,
                    name: 'datatables',
                    files: ['js/plugins/dataTables/angular-datatables.min.js']
                }, {
                    serie: true,
                    name: 'datatables.columnfilter',
                    files: ['js/plugins/dataTables/angular-datatables.columnfilter.min.js']
                }, {
                    serie: true,
                    name: 'datatables.buttons',
                    files: ['js/plugins/dataTables/angular-datatables.buttons.min.js']
                }]);
            },
            checkUserPermission: checkUserPermission('attorney_firm', 'view')
        }
    }).state('invoice.browse', {
        url: "/browse?invoice_number",
        templateUrl: "modules/invoice/browse.html",
        controller: 'invoiceController',
        data: {pageTitle: 'invoice'},
        resolve: {
            loadPlugin: function ($ocLazyLoad) {
                return $ocLazyLoad.load([{
                    serie: true,
                    files: ['js/plugins/dataTables/datatables.min.js', 'css/plugins/dataTables/datatables.min.css','css/plugins/ng-tags-input/ng-tags-input.css','css/plugins/ng-tags-input/ng-tags-input.bootstrap.css']
                }, {
                    serie: true,
                    name: 'datatables',
                    files: ['js/plugins/dataTables/angular-datatables.min.js']
                }, {
                    serie: true,
                    name: 'datatables.light-columnfilter',
                    files: ['js/plugins/dataTables/dataTables.lightColumnFilter.js','js/plugins/dataTables/angular-datatables.light-columnfilter.js']
                },
                {
                    files: ['css/plugins/iCheck/custom-blue.css', 'js/plugins/iCheck/icheck.min.js']
                },
                {
                    insertBefore: '#loadBefore',
                    name: 'localytics.directives',
                    files: ['css/plugins/chosen/bootstrap-chosen.css', 'js/plugins/chosen/chosen.jquery.js', 'js/plugins/chosen/chosen.js']
                },
                {
                    serie: true,
                    name: 'ngTagsInput',
                    files: ['js/plugins/ng-tags-input/ng-tags-input.min.js']
                }
                ]);
            },
            checkUserPermission: checkUserPermission('invoice', 'view')
        }
    })

上面是config.js文件代码。

$scope.dtOptions = DTOptionsBuilder.newOptions()
            .withDataProp('data')
            .withOption('ajax', function (data, callback, settings) {
                // map your server's response to the DataTables format and pass it to
                invoiceFactory.showDataTable('/api/invoice/get-invoice-listing', data).success(function (res) {
                    if (res.error) {
                        $scope.reloadData();
                    }
                    else {
                        $scope.selectAll = true;
                        $scope.invoiceArray = {};
                        callback(res);
                    }
                }).error(function (err) {
                    if (err.error !== "token_not_provided") {
                        $scope.reloadData();
                    }
                });
            })
            .withOption('processing', true)
            .withLanguage({
                "sEmptyTable": "NO INVOICE AVAILABLE IN TABLE",
                "sInfo": "SHOWING _START_ TO _END_ OF _TOTAL_ INVOICES",
                "sInfoEmpty": "SHOWING 0 TO 0 OF 0 INVOICES",
                "sInfoFiltered": "(FILTERED FROM _MAX_ TOTAL INVOICES)",
                "sInfoPostFix": "",
                "sInfoThousands": ",",
                "sLengthMenu": "SHOW _MENU_ INVOICES",
                "sLoadingRecords": "<img src='img/loading_bar.gif'/>",
                "sProcessing": "<img src='img/loading_bar.gif'/>",
                "sSearch": "SEARCH:",
                "sZeroRecords": "NO MATCHING INVOICE FOUND",
                "oPaginate": {
                    "sFirst": "FIRST",
                    "sLast": "LAST",
                    "sNext": "NEXT",
                    "sPrevious": "PREVIOUS"
                },
                "oAria": {
                    "sSortAscending": ": ACTIVATE TO SORT COLUMN ASCENDING",
                    "sSortDescending": ":   ACTIVATE TO SORT COLUMN DESCENDING"
                }
            })
            // .withOption('language', {"processing": "<img src='img/loading_bar.gif'/>",
            //     "sZeroRecords": "<div class='text-center'>No Record Found!</div>",
            //     "sInfo": "Showing START to END of TOTAL Records",
            //     "sInfoEmpty": "Showing 0 to 0 of 0 Records",
            //     "sInfoFiltered": "(filtered from MAX total Records)"})
            .withOption('serverSide', true)
            .withOption('stateSave', true)
            .withPaginationType('simple_numbers')
            .withOption('searchDelay', 500)
            .withOption('order', [1, 'desc'])
            .withOption('createdRow', $scope.createdRow)
            .withOption('headerCallback', function (header) {
                // Use this headerCompiled field to only compile header once
                if (!$scope.headerCompiled) {
                    $compile(angular.element(header).contents())($scope);
                }
            })
            .withLightColumnFilter({
                '1': {
                    type: 'text'
                },
                '2': {
                    type: 'text'
                },
                '3': {
                    type: 'text'
                },
                '4': {
                    type: 'text'
                },
                '5': {
                    type: 'text'
                },
                '6': {
                    type: 'text'
                }
            });

以上是控制器代码。

enter image description here

3 个答案:

答案 0 :(得分:2)

您得到的错误是因为角度数据表无法识别名为withLightColumnFilter的函数。

您可能已将其添加为插件,但我想这不是使用它的方式。

如果插件的文档没有给你一个明确的例子,我会尝试这个。

$scope.dtOptions = DTOptionsBuilder.newOptions()
     ...
.withOption('LightColumnFilter', {
     ...
 };

答案 1 :(得分:1)

我认为这完全取决于延迟负载因为我也面临这种类型的问题。所以我认为你需要直接加载到index.html文件而不是lazyloading。

答案 2 :(得分:0)

这似乎是'withButton'错误,所以如果datatables.min.js已经包含'buttons'插件,请检查它是否已添加到模块依赖

angular.module('moduelName', ['datatables.buttons'])