【问题标题】:Smart table - angularJs - ajax refresh table智能表——angularJs——ajax刷新表
【发布时间】:2026-01-13 18:55:01
【问题描述】:

我正在使用 angularjs mudule 智能表。

我的表有服务器端处理,所有数据加载都位于服务器上。我想在表格之外有一个刷新按钮。

这是调用服务器的函数,我希望能够手动调用它,但我不知道如何在我的控制器中检索表状态。

this.callServer = function callServer(tableState) {

        ctrl.isLoading = true;

        var pagination = tableState.pagination;

        var start = pagination.start || 0;     // This is NOT the page number, but the index of item in the list that you want to use to display the table.
        var number = pagination.number || 10;  // Number of entries showed per page.

        service.getPage(start, number, tableState, ctrl.startDateFilter,
                ctrl.endDateFilter).then(function (result) {
            ctrl.displayed = result.data;
            tableState.pagination.numberOfPages = result.numberOfPages;
            ctrl.isLoading = false;
        });
    };

我的目标是有一个这样的功能,我怎样才能得到表格状态?

    this.refreshTable = function(){
        tableState = getTableState();
        ctrl.callServer(tableState);
    }

【问题讨论】:

    标签: angularjs ajax smart-table


    【解决方案1】:

    解决方案是将表状态放入控制器变量中。

    每次调用 callServer 函数时,都会更新这个变量。这样,我就可以刷新表格了。

        this.tableState = null;
    
        this.callServer = function callServer(tableState) {
            ctrl.tableState = tableState;
            ...
        }
    
        this.refreshGrid = function(){
            ctrl.callServer(ctrl.tableState);
        }
    

    【讨论】:

      【解决方案2】:

      您也可以创建一个指令来执行此操作。

      查看

      <table st-pipe="productsTableCtrl.getTableData" 
      st-table="productsTableCtrl.tableData" refresh-table>
      

      在控制器内部

      $scope.$broadcast('refreshMyTable');
      

      指令

      app.directive("refreshTable", function(){
          return {
              require:'stTable',
              restrict: "A",
              link:function(scope,elem,attr,table){
                  scope.$on("refreshMyTable", function() {
                      table.pipe(table.tableState());
                  });
          }
      }});
      

      感谢 gtaylor44:https://github.com/lorenzofox3/Smart-Table/issues/363#issuecomment-246636293

      【讨论】: