ng-grid完全操纵该字段

时间:2014-03-12 16:03:48

标签: javascript angularjs ng-grid

大家好我有几个关于ng-grid的问题, 这是我的网格选项

$scope.gridOptions= {
    data:'data',
    selectedItems:sel,
    multiSelect:false,
    columnDefs: [{ field: 'field1', displayName: 'field1',cellTemplate: template,  cellClass:'cellToolTip'},
        {field:'field2', cellTemplate: template, visible:false},
        { field:'field3', displayName:'field3', cellTemplate: template,  cellClass:'cellToolTip' },
        { field: 'field4', displayName: 'field4',cellTemplate: templatetest} ,
        { field: 'dataInserimento', displayName: 'Data richiesta',cellTemplate: template,  cellClass:'cellToolTip'} ,
        { displayName: 'field5', cellTemplate:'<div ><button ng-diasbled="pdfenabled" ng-click="reportpdf()"><i class="glyphicon glyphicon-file"></i></button></div>', cellClass:'gridCell' }]
    }
}

这两个可能的细胞模板是

var template = '<div class="ngCellText" ng-class="col.colIndex()"><a id="gridField"  ng-cell-text>{{row.getProperty(col.field)}}</a></div>';
var templatetest = '<div ng-disabled="enabled" class="ngCellText" ng-class="col.colIndex()"><a id="gridField"  ng-cell-text>{{row.getProperty(col.field)}}</a></div>'

数据数组类似于那个

[{field1:'something',
  field2:'something',
  field3:'something',
  field4:[{otherstuff:'something',morestuff:'something'}],
  field5:'something'}
  {field1:'something',
  field2:'something',
  field3:'something',
  field4:[{otherstuff:'something',morestuff:'something'}],
  field5:'something'}
  {field1:'something',
  field2:'something',
  field3:'something',
  field4:[{otherstuff:'something',morestuff:'something'}],
  field5:'something'}
  }]

所以第一个问题是:如何在field4字段中显示元素而不是数组?是否有可能只获得一个或两个值?
第二个问题,可以使用单元字段属性进行另一个单元格绑定吗? 例如

if(field3==false){
    $scope.enabled=true;
}

1 个答案:

答案 0 :(得分:0)

像这样编写模板:

var templatetest = '<div ng-disabled="enabled" class="ngCellText" ng-class="col.colIndex()" ng-bind="row.getProperty(col.field)[0].otherstuff"></div>';

看看这个Plunker

甚至更好的是你可以使用过滤器:

app.filter('somefilter', function() {
  return function(x) {
    return x[0].otherstuff + ' / ' + x[0].morestuff;
  };
});

与此模板结合使用:

var templatetest = '<div ng-disabled="enabled" class="ngCellText" ng-class="col.colIndex()" ng-bind="row.getProperty(col.field)|somefilter"></div>';

看看这个Plunker

或者(这包括对第二个问题的回答)使用row.entity来查找完整的rowdata:

var templatetest = '<div ng-disabled="enabled" class="ngCellText" ng-class="col.colIndex()"> <a href="#">{{row.entity.field4[0].otherstuff+"/"+row.entity.field3}}</a></div>';

这是最后的Plunker

希望你能继续这样做。