根据角度范围值动态更改分页页面大小

Dynamically change Pagination page size from angular scope value

本文关键字:分页 动态 范围      更新时间:2023-09-26

我试图允许用户更改要显示的表中的行数。该网络应用程序适用于所有类型的设备,因此用户需要能够选择要显示的行数,以最大限度地减少滚动。

这是javascript模型

$scope.pageSizes = [
  { size: 10},
  { size: 25, isSelected: true },
  { size: 50}
];

HTML代码。这是我的页面大小选择器。

<li ng-repeat="pageSize in pageSizes">
  <a href="javascript:void(0)" ng-click="changePageSize(pageSize.size)"><span ng-class="{ orange: pageSize.isSelected }">{{pageSize.size}}</span></a>
</li>

和同一HTML文件中的表。

<table st-table="users" st-safe-src="safeUsers" class="table-striped argus-table">
  <thead>
    <tr>
      <th st-sort="id" class="sortable min-width">Id</th>
      <th st-sort="username" class="sortable">Username</th>
      <th st-sort="email" class="sortable">Email</th>
      <th class="table-action-header"></th>
      <th class="table-action-header"></th>
      <th class="table-action-header"></th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="user in users">
      <td>ID</td>
      <td>Username</td>
      <td>Email</td>
      <td>Option 1</td>
      <td>Option 2</td>
      <td>Option 3</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td class="text-center" st-pagination="" st-items-by-page="25" colspan="6"></td>
    </tr>
  </tfoot>
</table>

st-items-by-page="25"是我认为我必须放置角度代码的地方。

我尝试使用范围函数来查找新选择的

$scope.changePageSize = function (pageSize) {
  $scope.filter.pageSize = pageSize;
  _.find($scope.pageSizes, function (page) {
    if (page === pageSize) {
      $scope.selectedPageSize = page.size;
      page.isSelected = true;
    }
    else
      page.isSelected = false;
  });
};

和页脚再次

<tfoot>
  <tr>
    <td class="text-center" st-pagination="" st-items-by-page="{{selectedPageSize}}" colspan="6"></td>
  </tr>
</tfoot>

然而,这会导致错误

Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 2 of the expression [{{getSelectedPageSize}}] starting at [{getSelectedPageSize}}].

如有任何帮助,我们将不胜感激。感谢

不要使用模板表示法({{scopeValue}})为每页提供项目,只需提供一个解析为$scope 上的值的表达式

这个

st-items-by-page="{{selectedPageSize}}"

应该像这个

st-items-by-page="selectedPageSize"

文档

中有一个例子