Angular ngGrid数据表示

时间:2014-08-04 19:17:11

标签: javascript angularjs ng-grid plunker

我是Angular的新手,我对Javascript的体验并不是很广泛。我无法使用以下代码在ngGrid中显示数据。有什么问题?

实质上。我正在从Web服务加载数据,对它执行转换(数据透视),然后我想将它呈现在网格中。

请参阅以下

app.js - >开始poing

var konstruktApp= angular.module('konstruktApp',['ngGrid']);  

dataService.js - >网络服务电话

'use strict';
konstruktApp.service('DataService',function DataService($http){
   var callHttp = function(){
     delete $http.defaults.headers.common['X-Requested-With'];
       return $http.get("http://83.250.197.214/konstrukt.service/Konstrukt.SL.DummyBudgetService.svc/GetDummyBudgetData/");
   };
    return {
        getDummyData: callHttp
    };
});

ngGridController.js - >逻辑所在的位置......

$scope.getData = DataService.getDummyData;
$scope.gridOptions = {data:'result'};
var getData = function() {
    $scope.getData().then(function (response) {
        var res = pivotData(response.data);
        $scope.result = res.data.PivotedRows;

        $scope.columns = res.cols;

        console.log('from the success handler at ' + new Date());
    }, function (reason) {
        console.log('failed: ');
        console.log(reason);
    });

};

..这里是“转动”数据的逻辑

var pivotData = function(data) {
  var firstColumn = "Dim1";
  var secondColumn = "Period";

  var columns = [];
  columns.push({
    field: firstColumn,
    enableCellEdit: false
  });


  var pivotedArray = {};

  pivotedArray.PivotedRows = [];
  var rowItems = [];
  var rowArray = {};

  var previusFirstColumnValue = -1;
  var firstColumnValue = 1;

  //for each row
  for (var i = 0; i < data.Rows.length; i = i + 1) {
    //firstColumnValue = $scope.dataCollection.Rows[i].FindCell.Cells[firstColumn].Value;
    firstColumnValue = findCell(data.Rows[i].Cells, firstColumn).Value;

    //var secondColumnValue = data.Rows[i].Cells[secondColumn].Value;
    var secondColumnValue = findCell(data.Rows[i].Cells, secondColumn).Value;
    //if first column value has changed, add new row
    if (firstColumnValue != previusFirstColumnValue) {

      if (i !== 0) {
        for (var j = 0; j < rowItems.length; j = j + 1) {
          rowArray[rowItems[j].name] = rowItems[j].value;
        }
        pivotedArray.PivotedRows.push( rowArray);
        rowArray = {};
        rowItems = [];
      }

      rowItems.push({
        name: firstColumn,
        //value: $scope.dataCollection.Rows[i].Cells[firstColumn].Value
        value: findCell(data.Rows[i].Cells, firstColumn).Value
      });
    }

    //if (columns.indexOf({field: secondColumnValue}) == -1) {
    if (i < 12) {
      columns.push({
        field: secondColumnValue,
        editableCellTemplate: "<input ng-class=\"'colt' + col.index\" ng-input=\"COL_FIELD\" ng-blur=\"lostFocus()\" ng-model=\"COL_FIELD\" ng-change=\"dataChanged(col,row,row.entity)\"/>",
        enableCellEdit: true
      });
    }

    rowItems.push({
      name: secondColumnValue,
      value: findCell(data.Rows[i].Cells, secondColumn).Value
    });
    previusFirstColumnValue = firstColumnValue;
  }


  for (var k = 0; k < rowItems.length; k = k + 1) {
    rowArray[rowItems[k].name] = rowItems[k].value;
  }

 // $scope.columns = columns;

  pivotedArray.PivotedRows.push( rowArray);

  return {data: pivotedArray, cols: columns};
};

plnkr:http://plnkr.co/edit/ZqC7696xGbUtuWGIvnYs?p=preview

编辑:数据关联行&lt; - &gt;列是正确的,我怀疑pivotedArray.PivotedRows数组中的数据有问题。

1 个答案:

答案 0 :(得分:0)

事实证明,将代码移动到新的plnkr会产生差异。现在,那是我生命中的几个小时,我想要回来:)