我根据文档设置了smart-grid,一切正常。我在模态对话框中添加了一条新记录,并成功添加了新记录,我想刷新智能网格。我从文档中了解到,当我使用st-safe-src
attrubute时,我只需要刷新集合。
我的问题是,假设,我正在显示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.pagination
将为null
。
$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);
});