如何在angularFire 0.5.0和最新的ng-grid之间创建3路数据绑定?

时间:2014-01-05 03:44:42

标签: javascript angularjs firebase ng-grid angularfire

angularFire $ bind方法可以在这里找到:http://angularfire.com/flatdoc.html,最新的ng-grid可以在这里找到:http://angular-ui.github.io/ng-grid/

我尝试了最简单的解决方案,但它没有用:

$scope.myData = [{name: "Moroni", age: 50},
                 {name: "Tiancum", age: 43},
                 {name: "Jacob", age: 27},
                 {name: "Nephi", age: 29},
                 {name: "Enos", age: 34}];
$scope.gridOptions = { 
    data: 'myData',

    rowTemplate:'<div ng-style="{\'cursor\': row.cursor, \'z-index\': col.zIndex(), \'color\': \'rgb(248, 248, 248)\',\'background\': \'none repeat scroll 0% 0% rgba(51, 51, 51, 0.7)\' }" ng-repeat="col in renderedColumns" ng-class="col.colIndex()" class="ngCell {{col.cellClass}}" ng-cell></div>',
    headerRowTemplate: '<div ng-style="{ height: col.headerRowHeight,\'color\': \'rgb(248, 248, 248)\',\'background\': \'none repeat scroll 0% 0% rgba(51, 51, 51, 0.7)\'  }" ng-repeat="col in renderedColumns" ng-class="col.colIndex()" class="ngHeaderCell" ng-header-cell></div>',
    multiSelect: false,
    enableCellEditOnFocus: true,
    columnDefs: [{field: 'name', displayName: 'Name',editableCellTemplate: '<input type="text" ng-class="\'colt\' + col.index" ng-input="COL_FIELD" ng-model="COL_FIELD" />'},
                 {field:'age', displayName:'Age',editableCellTemplate: '<input type="text" ng-class="\'colt\' + col.index" ng-input="COL_FIELD" ng-model="COL_FIELD" />'}]


};
$scope.myData.$bind($scope,'gridData');

然后

<div class="gridStyle" ng-grid="gridOptions" ng-model="gridData"></div>

我的意思是,这甚至可能吗? :)

1 个答案:

答案 0 :(得分:6)

这当然是可能的。在我们开始讨论之前,让我们回顾几个重要的细节:

  1. Firebase和angularFire都在对象中工作,而ngGrid想要数组(我们需要一个过滤器)
  2. angularFire是常见用例的绝佳包装,但不是访问Firebase的唯一方法
  3. Firebase中有一项功能可自动将顺序数字ID转换为数组,我们可以利用这些功能。
  4. 因此,考虑到这些细节,有两种简单的方法。

    利用Firebase阵列模拟

    假设我们的数据是一个简单的数组并且我们不会删除键(如果id不是顺序的,我们的数组就成了一个对象),那么我们就可以直接从Firebase引用数据。

    这是a fiddle demonstrating the following example

    var app = angular.module('app', ['firebase', 'ngGrid']);
    var fb = new Firebase('https://nggrid-example.firebaseio-demo.com/');
    
    app.controller('ctrl', function($timeout, $scope) {
        $scope.myData = [];
        fb.on('value', function(snap) {
            // force a compile since we aren't in $digest
            $timeout(function() {
               $scope.myData = snap.val();
            });
        });
        $scope.gridOptions = { data: 'myData' };
    });
    

    在angularFire数据上使用过滤器

    但是,如果我们想坚持使用纯angularFire,或者我们的数据将由多个用户实时编辑(这会对阵列造成严重破坏),我们可以使用orderByPriority将数据过滤到数组中过滤

    这是a fiddle demonstrating the following example

    var app = angular.module('app', ['firebase', 'ngGrid']);
    var fb = new Firebase('https://nggrid-example.firebaseio-demo.com/');
    
    app.controller('ctrl', function($firebase, $scope, orderByPriorityFilter) {
        $scope.data = $firebase(fb);
        $scope.myData = $firebase(fb);
        $scope.$watchCollection('data', function() {
           $scope.myData = orderByPriorityFilter($scope.data); 
        });
        $scope.gridOptions = { data: 'myData' };
    });