UI Grid AngularJS:将自定义过滤器应用于页脚

时间:2017-04-20 16:07:35

标签: javascript angularjs pdf angular-ui-grid

我为我的ui-grid创建了一个自定义过滤器,我使用ui-grid-exporter将其导出为pdf,将十进制数限制为两个位置,并且当我导出ui-grid时它可以工作但是它不起作用当它是出口商时的页脚。

页脚 预计:32.34452436927346234523623462342345 需要:32.34

这是自定义过滤器。此过滤器在ui-grid内工作。

Map<FingerprintData, Long> stream1Timestamps = new HashMap<>();
Map<FingerprintData, Long> stream2Timestamps = new HashMap<>();

StreamClass stream1 = howEverYouFetchStream(1);
StreamClass stream2 = howEverYouFetchStream(2);

while(true /*or if you have conditional to terminate place it here*/){
     FingerprintData stream1fpd = stream1.getFingerprintData();
     FingerprintData stream2fpd = stream2.getFingerprintData();

     if(stream1Timestamps.keySet().contains(stream2fpd)){
         Long firstHeard = stream1Timestamps.get(stream2fpd);
         System.out.println("stream 2 is delayed:" +System.nanoTime()-firstHeard + " nanoseconds");
         stream1Timestamps.remove(stream2fpd);
     }else{
         stream2Timestamps.put(stream2fpd, System.nanoTime();
     }

     if(stream2Timestamps.keySet().contains(stream1fpd)){
         Long firstHeard = stream2Timestamps.get(stream1fpd);
         System.out.println("stream 1 is delayed:" +System.nanoTime()-firstHeard + " nanoseconds");
         stream2Timestamps.remove(stream1fpd);
     }else{
         stream1Timestamps.put(stream1fpd, System.nanoTime();
     }

  }

这里为ui-grid

声明了列
app.filter('rentalFilter', function () {
  return function (value, scope) {
    // Only perform logic if the value is actually defined
    if(typeof value != 'undefined') {
        if(value == null || value == "")
            value = 0;
        value = value.toFixed(2);
        if(value >= 0) {
            var parts=value.toString().split(".");
            return parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");
        }
        else {
            value = value * -1.00;
            value = value.toFixed(2);
            var parts=value.toString().split(".");
            return "-" + parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");
        }
    }
  };
});

导出功能将ui-grid作为pdf导出。显示的数字带有2位小数限制

$scope.columns = [ {
    name : 'parm',
    category : "column1",
    width : '6%',
    enableColumnMenu : false,
    displayName : $scope.rollup
}, {
    name : 'mondayAwrv',
    displayName : 'AWRV',
    cellClass : 'green',
    category : "MONDAY",
    exporterPdfAlign: 'right',
    width : '6%',
    cellFilter : 'number : 2',
    footerCellFilter : 'number : 2',
    aggregationType : uiGridConstants.aggregationTypes.sum,
    aggregationHideLabel : true,
    enableColumnMenu : false
}, {
    name : 'mondayCredit',
    cellClass : function(grid, row, col, rowRenderIndex, colRenderIndex) {
        if (grid.getCellValue(row, col) < 0)
            return 'negative';
        return 'positive'
    },
    category : "MONDAY",
    exporterPdfAlign: 'right',
    width : '6%',
    cellFilter : 'number : 2',
    footerCellFilter : 'number : 2',
    aggregationType : uiGridConstants.aggregationTypes.sum,
    aggregationHideLabel : true,
    enableColumnMenu : false
}, {
    name : 'mondayPer',
    displayName : '%',
    category : "MONDAY",
    exporterPdfAlign: 'right',
    width : '6%',
    cellFilter : 'number : 2',
    footerCellFilter : 'number : 2',
    aggregationType : uiGridConstants.aggregationTypes.avg,
    aggregationHideLabel : true,
    enableColumnMenu : false
}, 
.....
//and so on for other days of the week
];

我正在尝试将相同的内容应用于getFooterValue函数

$scope.export = function(){
      //$scope.gridApi.exporter.pdfExport( uiGridExporterConstants.ALL, uiGridExporterConstants.ALL );
      var exportData = [];
      var exportColumnHeaders = $scope.gridOptions.showHeader ? uiGridExporterService.getColumnHeaders($scope.gridApi.grid, uiGridExporterConstants.VISIBLE) : [];
      angular.forEach($scope.gridApi.grid.rows, function(row) {
        if (row.visible) {
          var values = [];
          angular.forEach(exportColumnHeaders, function(column) {
            var value = row.entity[column.name];
            if (column.name == 'mondayAwrv' || column.name == 'mondayCredit' || column.name == 'mondayPer' ||
                column.name == 'tuesdayAwrv' || column.name == 'tuesdayCredit' || column.name == 'tuesdayPer' ||
                column.name == 'wednesdayAwrv' || column.name == 'wednesdayCredit' || column.name == 'wednesdayPer' ||
                column.name == 'thursdayAwrv' || column.name == 'thursdayCredit' || column.name == 'thursdayPer' ||
                column.name == 'fridayAwrv' || column.name == 'fridayCredit' || column.name == 'fridayPer') value = $filter('rentalFilter')(value);


            values.push({
              value: value
            });

          });
          exportData.push(values);
        }
      });

      var content = uiGridExporterService.prepareAsPdf($scope.gridApi.grid, exportColumnHeaders, exportData);

      pdfMake.createPdf(content).open();


    }

这是导出页脚值的原因。

$scope.getFooterValue = function(i){        
        return $scope.gridApi.grid.columns[i].getAggregationValue();
//template
    //[ '', {text: ($scope.getFooterValue(1)), alignment: 'right'}, {text: ($scope.getFooterValue(2)), alignment: 'right'},

        /*
        var exportData = [];
          var exportColumnHeaders = $scope.gridOptions.showHeader ? uiGridExporterService.getColumnHeaders($scope.gridApi.grid, uiGridExporterConstants.VISIBLE) : [];
          angular.forEach($scope.gridApi.grid.rows, function(row) {
            if (row.visible) {
              var values = [];
              angular.forEach(exportColumnHeaders, function(column) {
                var value = row.entity[column.name];
                if (column.name == 'mondayAwrv' || column.name == 'mondayCredit' || column.name == 'mondayPer' ||
                    column.name == 'tuesdayAwrv' || column.name == 'tuesdayCredit' || column.name == 'tuesdayPer' ||
                    column.name == 'wednesdayAwrv' || column.name == 'wednesdayCredit' || column.name == 'wednesdayPer' ||
                    column.name == 'thursdayAwrv' || column.name == 'thursdayCredit' || column.name == 'thursdayPer' ||
                    column.name == 'fridayAwrv' || column.name == 'fridayCredit' || column.name == 'fridayPer') value = $filter('rentalFilter')(value);


                values.push({
                  value: value
                });

              });
              exportData.push(values);
            }
          });

          var content = uiGridExporterService.prepareAsPdf($scope.gridApi.grid, exportColumnHeaders, exportData);

          content.pageMargins = [40, 80, 40, 60];   

          //pdfMake.createPdf(content).open();
          return $scope.gridApi.grid.columns[i].getAggregationValue();
        */

    }  

如何将过滤器应用于页脚以限制2个小数点?
非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

AngularJS有built in filter用于按小数位数过滤数字。

您可以在任何自定义过滤器之后管道此过滤器。例如:

<span>{{myNumber | myCustomFilter | number:2}}</span>

答案 1 :(得分:0)

我必须做的是在exporterPdfFooter

中添加过滤器到主体
body: [
            [{text: $filter('netFilter')($scope.getFooterValue(1))} ],

            ]