如何在AngularJS中使用智能表显示记录总数

如何在AngularJS中使用智能表显示记录总数,angularjs,pagination,smart-table,Angularjs,Pagination,Smart Table,我正在使用智能表显示记录。 使用自定义分页显示每页20条记录 我想显示分页,如:25条记录中的1-20条 我怎样才能做到这一点? -我尝试使用(页数*总页数),但这将使结果更加接近 您可以使用totalItemCount,它位于tableState.pagination中。 从服务器获取数据后,只需填充该值,并在分页模板中使用该值。我找到了解决问题的方法。我知道这是一个老问题,但为了让它对其他人更有帮助,我想展示如何使这个解决方案更好 你在第一页和后面几页给出的答案都是正确的,但不总是在最后一页

我正在使用智能表显示记录。 使用自定义分页显示每页20条记录

我想显示分页,如:25条记录中的1-20条 我怎样才能做到这一点?
-我尝试使用(页数*总页数),但这将使结果更加接近

您可以使用
totalItemCount
,它位于
tableState.pagination
中。
从服务器获取数据后,只需填充该值,并在分页模板中使用该值。

我找到了解决问题的方法。我知道这是一个老问题,但为了让它对其他人更有帮助,我想展示如何使这个解决方案更好

你在第一页和后面几页给出的答案都是正确的,但不总是在最后一页

你是这样做的:

<span class="pagination-info itemCount">
     {{(currentPage-1)*stItemsByPage+1}}-
     {{currentPage*stItemsByPage}} 
     of {{numPages*stItemsByPage}} Records
</span>

{{(当前页面-1)*拼接子页面+1}-
{{currentPage*stItemsByPage}
{{numPages*stItemsByPage}}记录的
如果最后一页上的记录数小于stItemsByPage,则永远不会正确。在这种情况下,这会更好:

<span class="pagination-info itemCount">
     {{(currentPage-1)*stItemsByPage+1}}-
     {{(currentPage*stItemsByPage) > totalItemCount?totalItemCount:(currentPage*stItemsByPage)}} 
     of {{numPages*stItemsByPage}} Records
</span>

{{(当前页面-1)*拼接子页面+1}-
{(currentPage*stItemsByPage)>totalItemCount?totalItemCount:(currentPage*stItemsByPage)}
{{numPages*stItemsByPage}}记录的

首先,您应该创建指令

ticketsApp.directive('stSummary', function () {

    return {
    restrict: 'E',
    require: '^stTable',
    template:'<span><b>[[pagination.start ]]  of [[pagination.totalItemCount]] items</b></span>',
    scope: {},
    link: function (scope, element, attr, ctrl) {
      var listener = function (val) {
        scope.pagination = ctrl.tableState().pagination;
        scope.to = Math.min(scope.pagination.start + scope.pagination.number, scope.total || 0);
      };
      scope.$watch(ctrl.tableState(), listener, true);
    }
  }
});`
ticketsApp.directive('stSummary',function(){
返回{
限制:'E',
要求:“^stTable”,
模板:[[pagination.totalItemCount]]项的“[[pagination.start]],
作用域:{},
链接:函数(范围、元素、属性、ctrl){
var listener=函数(val){
scope.pagination=ctrl.tableState().pagination;
scope.to=Math.min(scope.pagination.start+scope.pagination.number,scope.total | 0);
};
scope.$watch(ctrl.tableState(),listener,true);
}
}
});`
之后,您应该在视图中使用“stSummary”标记。 例:


` 

受dhiraj tiwari answer的启发,手表只有包装在函数调用中才能工作:

  .directive('stPaginationSummary', function () {
    return {
      restrict: 'E',
      require: '^stTable',
      template: '<span>Showing <strong>{{from}}</strong>-<strong>{{to}}</strong> of <strong>{{total}}</strong> Record(s)</span>',
      scope: {},
      link: function (scope, element, attr, ctrl) {
        var listener = function () {
          var pagination = ctrl.tableState().pagination
          scope.from = pagination.totalItemCount === 0 ? 0 : pagination.start + 1
          scope.to = Math.min(pagination.start + pagination.number, pagination.totalItemCount)
          scope.total = pagination.totalItemCount
        }
        scope.$watch(function () {
          return ctrl.tableState()
        }, listener, true)
      }
    }
  })
指令('stPaginationSummary',函数(){ 返回{ 限制:'E', 要求:“^stTable”, 模板:“显示{{from}}-{{to}}{{total}}记录”, 作用域:{}, 链接:函数(范围、元素、属性、ctrl){ var listener=函数(){ var pagination=ctrl.tableState().pagination scope.from=pagination.totalItemCount==0?0:pagination.start+1 scope.to=Math.min(pagination.start+pagination.number,pagination.totalItemCount) scope.total=pagination.totalItemCount } 范围:$watch(函数(){ 返回ctrl.tableState() },listener,true) } } })
链接到代码-请在问题中发布您的代码,否则,如果您的链接失效,此问题将对其他任何人无效。谢谢!这就是我需要的。我可以将总记录计数设置为tableState.pagination.totalItemCount,并可以在分页html中像{{totalItemCount}一样访问它。再次感谢。
  .directive('stPaginationSummary', function () {
    return {
      restrict: 'E',
      require: '^stTable',
      template: '<span>Showing <strong>{{from}}</strong>-<strong>{{to}}</strong> of <strong>{{total}}</strong> Record(s)</span>',
      scope: {},
      link: function (scope, element, attr, ctrl) {
        var listener = function () {
          var pagination = ctrl.tableState().pagination
          scope.from = pagination.totalItemCount === 0 ? 0 : pagination.start + 1
          scope.to = Math.min(pagination.start + pagination.number, pagination.totalItemCount)
          scope.total = pagination.totalItemCount
        }
        scope.$watch(function () {
          return ctrl.tableState()
        }, listener, true)
      }
    }
  })