I have a table with lots of columns and some rows may have undefined values so I want to show a default text instead of an empty cell depending on the column. How can I set that value?
我有一个有很多列的表,有些行可能有未定义的值,所以我想要显示一个默认的文本,而不是依赖于列的空单元格。我如何设置那个值?
I would expect something like this:
我想大概是这样:
columnDefs: [
{name: 'Code', field: 'code', default: 'My default value for code column'},
{name: 'Name', field: 'name', default: 'My default value for name column'}
];
Thanks
谢谢
2 个解决方案
#1
1
Use cellTemplates
:
使用cellTemplates:
var codeTemplate = '<div ng-if="row.entity.code">{{row.entity.code}}</div>' +
'<div ng-if="!row.entity.code">My Default Value for code column</div>';
var nameTemplate = '<div ng-if="row.entity.name">{{row.entity.name}}</div>' +
'<div ng-if="!row.entity.name">My Default Value for name column</div>';
$scope.gridOptions = {
enableSorting: true,
columnDefs: [{
name: 'Code',
field: 'code',
cellTemplate: codeTemplate
}, {
name: 'Name',
field: 'name',
cellTemplate: nameTemplate
}],
data: [{
"code": "122",
"name": "Maximus"
}, {
"code": "123",
"name": "Minimus"
}, {
"code": null,
"name": "Nocodeus"
}, {
"code": '',
"name": "Emptyus"
}, {
"code": '124',
"name": ""
}, {
"code": '',
"name": ""
}]
};
Example is here
这里的例子是
#2
1
Yeah you can use cellTemplate
, look that, it's most easy:
是的,你可以用cellTemplate,看,它是最简单的:
columnDefs: [
{name: 'Code', field: 'code', cellTemplate:'<div>{{row.entity.code="My default value for code column"}}</div>'},
{name: 'Name', field: 'name', cellTemplate:'<div>{{row.entity.name="My default value for name column"}}</div>'}
];
#1
1
Use cellTemplates
:
使用cellTemplates:
var codeTemplate = '<div ng-if="row.entity.code">{{row.entity.code}}</div>' +
'<div ng-if="!row.entity.code">My Default Value for code column</div>';
var nameTemplate = '<div ng-if="row.entity.name">{{row.entity.name}}</div>' +
'<div ng-if="!row.entity.name">My Default Value for name column</div>';
$scope.gridOptions = {
enableSorting: true,
columnDefs: [{
name: 'Code',
field: 'code',
cellTemplate: codeTemplate
}, {
name: 'Name',
field: 'name',
cellTemplate: nameTemplate
}],
data: [{
"code": "122",
"name": "Maximus"
}, {
"code": "123",
"name": "Minimus"
}, {
"code": null,
"name": "Nocodeus"
}, {
"code": '',
"name": "Emptyus"
}, {
"code": '124',
"name": ""
}, {
"code": '',
"name": ""
}]
};
Example is here
这里的例子是
#2
1
Yeah you can use cellTemplate
, look that, it's most easy:
是的,你可以用cellTemplate,看,它是最简单的:
columnDefs: [
{name: 'Code', field: 'code', cellTemplate:'<div>{{row.entity.code="My default value for code column"}}</div>'},
{name: 'Name', field: 'name', cellTemplate:'<div>{{row.entity.name="My default value for name column"}}</div>'}
];