【问题标题】:Angularjs UI Grid Totals per Row inside the row行内每行的Angularjs UI Grid Totals
【发布时间】:2015-04-12 22:52:11
【问题描述】:

{{ HelloWorld }}

我正在尝试访问 angular-ui-grid 中同一行中其他列的值。我有两个相关的问题。

  1. 如何将同一行中多列的值相加得到总计?
  2. 如何使用行索引将其作为参数传递并删除当前行?

正如您在上图中所见,我要查找的总计仅与整数值有关,与初始字符串无关。
此外,带有 X 的按钮应在单击时删除该行。行动态包含在网格中。

我已经查看了UI Grid Tutorial,但这些示例并未显示如何完成我的任何一个问题。

我的页脚工作正常。它被配置为在整个行集中按列给出总计:

这是我目前发现的一些代码:

var removeTemplate = '<button type="button" class="grid-remove btn btn-default btn-sm"><span class="glyphicon glyphicon-remove" aria-hidden="true" ng-click="removeRow($event, row.entity)"></span></button>';

$scope.gridAccountableHours = {
    enableCellEditOnFocus: true,
    showColumnFooter: true
};

$scope.gridAccountableHours.columnDefs = [
  { name: "contractCode", displayName: ""},
  { name: "hours1", displayName: "1", type: "number" },
  { name: "hours2", displayName: "2", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours3", displayName: "3", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours4", displayName: "4", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours5", displayName: "5", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours6", displayName: "6", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours7", displayName: "7", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours8", displayName: "8", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours9", displayName: "9", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours10", displayName: "10", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours11", displayName: "11", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours12", displayName: "12", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours13", displayName: "13", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours14", displayName: "14", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "hours15", displayName: "15", type: "number", aggregationType: uiGridConstants.aggregationTypes.sum },
  { name: "total", displayName: "Total" },
  { name: "remove", displayName: "", cellTemplate: removeTemplate }
];

$scope.removeRow = function ($event, entity) {
    $event.stopPropagation();
    $scope.gridAccountableHours.data.splice($scope.gridAccountableHours.data.indexOf(entity), 1);
};

感谢您的宝贵时间。

【问题讨论】:

    标签: angularjs angular-ui-grid


    【解决方案1】:

    您可以使用 gridRow 上的函数对整个网格进行总计。

    $scope.gridAccountableHours.forEach(function(rowEntity) {
      rowEntity.total = function() {
        var total = 0;
        for( var 1 = 1; i++; i<=15 ){
          total += this['hours'+i];
        }
      };
    });
    

    你的总列 def 应该自动绑定到这个总函数。参考http://ui-grid.info/docs/#/tutorial/106_binding

    对于删除按钮,您缺少 appScope,请参阅 http://ui-grid.info/docs/#/tutorial/305_appScope

    您的模板 ng-click 应该是:

    ng-click="grid.appScope.removeRow($event, row.entity)"
    

    【讨论】:

    • 两种解决方案都有效。我确实更喜欢在每个字段上单独进行摘要,它工作得很好。谢谢保罗。我希望这对其他人也有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2015-08-15
    • 2016-01-28
    • 2016-02-25
    • 1970-01-01
    相关资源
    最近更新 更多