Angularjs 角度用户界面网格
道歉,如果这真的很简单,但 我想显示一个数据网格,但需要查找一些列。这将成为一个可编辑的网格,但我的基本痛苦,所以上帝帮助我 我有两组数据:Angularjs 角度用户界面网格,angularjs,angular-ui-grid,Angularjs,Angular Ui Grid,道歉,如果这真的很简单,但 我想显示一个数据网格,但需要查找一些列。这将成为一个可编辑的网格,但我的基本痛苦,所以上帝帮助我 我有两组数据: $gridOptions1.data = [{"group_id":"1","location_id":"-1","group_name":"Cars","active":"1"},{"group_id":"2","location_id":"1","group_name":"Trains","active":"1"},{"group_id":"3"
$gridOptions1.data = [{"group_id":"1","location_id":"-1","group_name":"Cars","active":"1"},{"group_id":"2","location_id":"1","group_name":"Trains","active":"1"},{"group_id":"3","location_id":"2","group_name":"Buses","active":"0"}]
及
并希望显示在网格中
$scope.gridOptions1 = {
enableSorting: true,
columnDefs: [
{ field: 'location_id' },
{ field: 'group_name' },
{ field: 'active' },
{ name: 'Location', field:'location_id', cellFilter: 'maplocation:this'}
],
onRegisterApi: function( gridApi ) {
$scope.grid1Api = gridApi;
}
})
我需要做的是映射位置id
我以为我可以使用过滤器,但似乎无法访问范围数据
如果有人能给我一个简单的例子来说明如何做到这一点,我将非常感激,因为我正在努力寻找我想做的任何例子
从我所看到的“this”参数是指向记录的指针,而不是定义网格选项的范围
我不想在过滤器中定义数据,因为它来自数据库
希望这是有意义的。如果您想使用应用程序范围中的部分数据来转换UI网格中显示的数据,最好使用自定义模板并从模板中调用函数 像这样的方法应该会奏效:
columnDefs: [
{ field: 'location_id' },
{ field: 'group_name' },
{ field: 'active' },
{ name: 'Location', field:'location_id', cellTemplate: 'location-template'}
],
然后是HTML:
<script type="text/ng-template" id="location-template">
<div class="ui-grid-cell-contents" title="TOOLTIP">{{grid.appScope.formatLocation(row)}}</div>
</script>
{{grid.appScope.formatLocation(行)}
现在,您所要做的就是在控制器的作用域中定义函数formatLocation
,并在那里发挥神奇的作用
从单元格模板中调用函数时,请确保使用grid.appScope访问控制器的作用域,如我提供的示例所示。多亏了@Ethnar,这里有一个可行的解决方案,可以将模板保留在源代码中:
columnDefs: [ { field: 'location_id' },
{ field: 'group_name' },
{ field: active' },
{ name: 'Location', field:'location_id',
cellTemplate: '<div class="ui-grid-cell-contents" title="TOOLTIP">{{grid.appScope.formatLocation(row)}}</div>'
}],
我使用了Ethnar的例子:$scope.formatLocation=function(row){locationid=row.entity.location_id;if(locationid&&$scope.locations.length){var selected=$filter('filter')($scope.locations,{value:locationid});return selected.length?selected[0]。text:'Not set';}else{return'Not set';}}}谢谢@Ethnar我有点搞不清楚它是如何组合在一起的,我希望可以使用一个过滤器在.js文件中保留尽可能多的代码,以便尽可能多的逻辑保持在同一个地方。我已经用下面的代码实现了这个功能。`$scope.formatLocation=function(行){locationid=row.entity.location_id;if(locationid&&$scope.locations.length){var selected=$filter('filter')($scope.locations,{value:locationid});return selected.length?selected[0]。文本:'Not set';}否则{return Not set';}`
columnDefs: [ { field: 'location_id' },
{ field: 'group_name' },
{ field: active' },
{ name: 'Location', field:'location_id',
cellTemplate: '<div class="ui-grid-cell-contents" title="TOOLTIP">{{grid.appScope.formatLocation(row)}}</div>'
}],
$scope.formatLocation=function(row)
{
locationid=row.entity.location_id;
if(locationid && $scope.locations.length) {
var selected = $filter('filter')($scope.locations, {value: locationid});
return selected.length ? selected[0].text : 'Not set';
} else {
return 'Not set';
} };