2016-07-06 34 views
0

我正在使用angular-ua-pivot-table.js,我想將更新的表信息保存在控制器的$ scope變量中。

下面是完整的指令:

angular.module('ua.pivottable', []) 
.directive('pivotTable', pivotTableDirective); 

function pivotTableDirective() { 
return { 
    restrict: 'E', 
    link: function (scope, element, attrs) { 
    var derivers = $.pivotUtilities.derivers; 
    function updatePivotTable(newValue, oldValue) { 
     if (newValue) { 
     var derived = scope.$eval(attrs.derivedAttributes); 
     console.log(derived); 
     var finalDerived = {}; 
     derived.forEach(function (e, i) { 
      if (e.type === "date") { 
      finalDerived[e.name] = derivers.dateFormat("donationDate", "%m/%d/%y"); 
      finalDerived[e.name + " (month)"] = derivers.dateFormat("donationDate", "%n"); 
      finalDerived[e.name + " (year)"] = derivers.dateFormat("donationDate", "%y"); 
      } else if (e.type === "lookup") { 
      finalDerived[e.name] = function (row) { return row[e.attr] || "(none)" } 
      } else if (e.type === "hidden") { 
      finalDerived[e.name] = function (row) { return "(none)" }; 
      } 
     }); 
     console.log(finalDerived); 
     var pivotData = scope.$eval(attrs.pivotData); 
     if (pivotData) { 
      var pivotAttrs = $.extend({}, { 
      rows: attrs.rows ? scope.$eval(attrs.rows) : undefined, 
      cols: attrs.cols ? scope.$eval(attrs.cols) : undefined, 
      aggregatorName: attrs.aggregatorName ? scope.$eval(attrs.aggregatorName) : undefined, 
      vals: attrs.vals ? scope.$eval(attrs.vals) : undefined, 
      rendererName: attrs.rendererName ? scope.$eval(attrs.rendererName) : undefined, 
      aggregators: attrs.aggregators ? scope.$eval(attrs.aggregators) : undefined, 
      derivedAttributes: finalDerived ? finalDerived : undefined, 
      hiddenAttributes: attrs.hiddenAttributes ? scope.$eval(attrs.hiddenAttributes) : undefined, 
      onRefresh: function (config) { 
       var config_copy = JSON.parse(JSON.stringify(config)); 
       //delete some values which are functions 
       delete config_copy["aggregators"]; 
       delete config_copy["renderers"]; 
       //delete some bulky default values 
       delete config_copy["rendererOptions"]; 
       delete config_copy["localeStrings"]; 
       //$("#output").text(JSON.stringify(config_copy, undefined, 2)); 
       scope.saveState = config_copy; 
       scope.$parent.saveState = config_copy; 
       console.log(scope); 
      } 
      }); 
      console.log(pivotAttrs); 
      angular.element(element) 
      .pivotUI(pivotData, pivotAttrs); 
     } 
     } 
    }; 

    scope.$watchGroup([ 
     attrs.pivotData, 
     attrs.rows, 
     attrs.cols, 
     attrs.aggregatorName, 
     attrs.vals, 
     attrs.rendererName, 
     attrs.aggregators, 
     attrs.derivedAttributes, 
     attrs.hiddenAttributes 
    ], updatePivotTable, true); 
    } 
}; 
} 

的onRefresh方法是我一直在努力。每次數據透視表被編輯時,表格的值都保存在一個名爲config_copy的變量中。我想讓我的控制器可以訪問名爲$ scope.saveState的$ scope變量中的config_copy。

下面是HTML:

<pivot-table id="pivotTable" class="table" save-state="pivotTable" pivot-data="pivotTable.data" 
         rows="pivotTable.rows" 
         cols="pivotTable.cols" 
         vals="pivotTable.vals" 
         aggregators="pivotTable.aggregators" 
         aggregator-name="pivotTable.aggregatorName" 
         renderer-name="pivotTable.rendererName" 
         derived-attributes="pivotTable.derivedAttributes" 
         hidden-attributes="pivotTable.hiddenAttributes"> 
      </pivot-table> 

並初始化數據透視表的控制器代碼:

var prepPivot = function (savedTable) { 

     if ($scope.donations.length>0) { 
     console.log('pivotInit', pivotInit) 

     var derivers = $.pivotUtilities.derivers; 
     var tpl = $.pivotUtilities.aggregatorTemplates; 
     var numberFormat = $.pivotUtilities.numberFormat; 
     var currencyFormat = numberFormat({ prefix: "$" }); 
     console.log($scope.donations); 
     $scope.derived = [ 
      { 
      name: "Donation date", 
      type: "date", 
      attr: "donationDate", 
      }, 
      { 
      name: "Donation status", 
      type: "lookup", 
      attr: "status", 
      }, 
      { 
      name: "Disbursement status", 
      type: "lookup", 
      attr: "disbursementStatus", 
      }, 
      { 
      name: "$$hashKey", 
      type: "hidden", 
      } 
     ]; 

     var settings = { 
      hiddenAttributes: [], 
      derivedAttributes: $scope.derived, 
      aggregators: { 
      "Donation Count": function() { return tpl.count()() }, 
      "Receipt Amount": function() { 
       return tpl.sum(currencyFormat)(["receiptAmount"]); 
      }, 
      } 
     }; 

     // hide everything, so we can format the columns better 
     settings.hiddenAttributes.push('$$hashKey'); 
     for (var property in $scope.donations[0]) { 
      if ($scope.donations[0].hasOwnProperty(property)) { 
      settings.hiddenAttributes.push(property); 
      } 
     } 

     if (savedTable) { 
      $scope.pivotTable = savedTable; 
     } else { 
      $scope.pivotTable = { 
      rows: ['donationDate'], 
      cols: ['disbursementStatus'], 
      aggregatorName: 'Donation Count', 
      vals: ['receiptAmount'], 
      rendererName: 'Table', 
      aggregators: settings.aggregators, 
      derivedAttributes: settings.derivedAttributes, 
      hiddenAttributes: settings.hiddenAttributes, 
      }; 
     } 


     console.log($scope.pivotTable); 

     $scope.pivotTable.data = $scope.donations; 

     pivotInit= true; 
     } 
    }; 

如何打開config_copy從指示功能onRefresh()到$範圍。 saveState可以在我的控制器中訪問?

回答

1

您可以設置範圍節這樣的指令:

scope: { 
    saveState: "=" 
} 

然後是$scope.pivotTable會更新每次都會從東西指令更改時間。

1

首先,您的指令是否與您的controller相同scope?如果是,那麼他們將共享scope,您可以將數據設置爲scope.myData,並且您的controller將有權訪問該變量。如果指令嵌套或隔離在您的controller中,則還可以使用scope.$parent訪問控制器。

0

下面是我最終如何解決這個問題,以解決將來可能遇到類似問題的任何人。

$scope.savePivotTable = function (data) { 
    console.log('SAVED!'); 
    $scope.savedPivotTable = data; 
    } 

將它添加到我的指令,在HTML模板:

<pivot-table id="pivotTable" class="table" pivot-data="pivotTable.data" 
    rows="pivotTable.rows" 
    cols="pivotTable.cols" 
    vals="pivotTable.vals" 
    aggregators="pivotTable.aggregators" 
    aggregator-name="pivotTable.aggregatorName" 
    renderer-name="pivotTable.rendererName" 
    derived-attributes="pivotTable.derivedAttributes" 
    hidden-attributes="pivotTable.hiddenAttributes" 
    save-pivot-table="savePivotTable"> 
</pivot-table> 

把它添加到的範圍對象在我的指令代碼:

我在我的控制器創造了這個功能

function pivotTableDirective() { 
return { 
    restrict: 'E', 
    scope: { 
    pivotData: "=", 
    rows: "=", 
    cols: "=", 
    aggregatorName: "=", 
    vals: "=", 
    rendererName: "=", 
    aggregators: "=", 
    derivedAttributes: "=", 
    hiddenAttributes: "=", 
    savePivotTable: "=" 
    }, 
    link: function (scope, element, attrs) { 
    // Rest of the directive 

然後從onRefresh方法裏面調用函數:

var pivotAttrs = $.extend({}, { 
rows: scope.rows ? scope.rows : undefined, 
cols: scope.cols ? scope.cols : undefined, 
aggregatorName: scope.aggregatorName ? scope.aggregatorName : undefined, 
vals: scope.vals ? scope.vals : undefined, 
rendererName: scope.rendererName ? scope.rendererName : undefined, 
aggregators: scope.aggregators ? scope.aggregators : undefined, 
derivedAttributes: finalDerived ? finalDerived : undefined, 
hiddenAttributes: scope.hiddenAttributes ? scope.hiddenAttributes : undefined, 
onRefresh: function (config) { 
    var config_copy = JSON.parse(JSON.stringify(config)); 
    //delete some values which are functions 
    delete config_copy["aggregators"]; 
    delete config_copy["renderers"]; 
    //delete some bulky default values 
    delete config_copy["rendererOptions"]; 
    delete config_copy["localeStrings"]; 
    scope.savePivotTable(config_copy); 
    $("#output").text(JSON.stringify(config_copy, undefined, 2)); 
    } 
}); 

這給了我訪問

相關問題