1

我已经按照文档设置了智能电网,一切正常。我在模式对话框中添加新记录,成功添加新记录后,我想刷新智能网格。我从文档中了解到,我只需要在使用属性时刷新集合st-safe-src

我的问题是,假设我正在显示 17 条记录,我现在在第 2 页,现在我打开模式添加新记录并在成功添加后关闭它,我正在重置集合。但在这种情况下,页面仅保留在第 2 页,而不是重置到第 1 页。如何做到这一点?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<table st-table="displayedCollection" st-pipe="getMasterJobs" st-safe-src="rowCollection" class="table table-condensed table-striped table-hover">
  <thead>
    <tr>
      <th>Name</th>
      <th>Description</th>
      <th></th>
    </tr>
  </thead>
  <tbody ng-show="!isDataLoading">
    <tr ng-repeat="row in displayedCollection">
      <td>{{row.name}}</td>
      <td>{{row.description}}</td>
      <td>
        <button type="button" ng-click="editRow(row)" class="btn btn-xs btn-info">
          <i class="fa fa-edit"></i> Edit
        </button>
      </td>
    </tr>
  </tbody>
  <tbody ng-show="isDataLoading">
    <tr>
      <td colspan="3" class="text-center">Loading...</div>
      </td>
    </tr>
  </tbody>
  <tfoot ng-hide="isDataLoading">
    <tr>
      <td class="text-center" st-pagination="" st-items-by-page="10" st-displayed-pages="displayedPages" colspan="4"></td>
    </tr>
  </tfoot>
</table>

我的 controller.js 文件中有这个:

$scope.rowCollection = [];
$scope.isDataLoading = false;
//copy the references (you could clone ie angular.copy but then have to go through a dirty checking for the matches)
$scope.displayedCollection = [].concat($scope.rowCollection);

$scope.getMasterJobs = function(tableState) {
  console.log(tableState);
  var start = 0;
  var length = 10;
  var pagination = tableState.pagination;
  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.
  length = pagination.number || 10; // Number of entries showed per page.
  $scope.isDataLoading = true;
  AdminJobMasterService.getMasterJobs(start, length).success(function(response, status, headers, config) {
    $scope.rowCollection = response.data;
    $scope.displayedCollection = [].concat($scope.rowCollection);

    //set the number of pages so the pagination can update
    tableState.pagination.numberOfPages = response.numberOfPages;
    $scope.displayedPages = Math.ceil(response.numberOfPages / length);
    $scope.isDataLoading = false;
  }).error(function(data, status, headers, config) {
    console.error(data);
    $scope.isDataLoading = false;
  });
};

现在,当我单击新建/编辑按钮并在模式上单击关闭时调用不同的控制器,我触发事件并在上述控制器中处理该事件。请看,我正在重置 smart-table 观察到的集合,并尝试初始化 tableState 变量。没有 tableState 变量,我不能按getMasterJobs()原样调用。tableState.paginationnull

$scope.$on('new-job-added', function(event, data) {
  $log.info("Time to refresh!");
  $scope.rowCollection = [];
  $scope.displayedCollection = [].concat($scope.rowCollection);
  var tableState = {
    sort: {},
    search: {},
    pagination: {
      start: 0
    }
  };
  $scope.getMasterJobs(tableState);
});

4

0 回答 0