我的fuelux数据网格在骨干渲染功能中看起来像这样
var dataSource = new StaticDataSource({
columns: [
{
property: 'id',
label: 'id',
sortable: true
},
{
property: 'name',
label: 'groups',
sortable: true
},
{
property: 'name',
label: 'Roles',
sortable: true
}
],
data: this.collection,
delay: 250
});
$('#sectionName').html('Groups');
$('#MyGrid').datagrid({
dataSource: dataSource,
stretchHeight: true
});
this.collection返回json如下
[
{
"id":1,
"roles":[
{"id":1,"authority":"ROLE1"},
{"id":2,"authority":"ROLE2"},
{"id":3,"authority":"ROLE3"}
],
"groups":[
{"id":1,"name":"A"},
{"id":2,"name":"B"},
{"id":3,"name":"C"}
]
},
{
"id":2,
"roles":[
{"id":5,"authority":"ROLE4"},
{"id":5,"authority":"ROLE5"},
{"id":6,"authority":"ROLE6"}
],
"groups":[
{"id":4,"name":"D"},
{"id":5,"name":"E"},
{"id":6,"name":"F"}
]
}
我希望fuelux datagrid具有列ID,角色和组。它应该如下所示:
id roles groups
1 role1, role2 , role3 A, B, C
12 role3, role4 , role5 D, E, F
我试着写这样的格式化程序函数,但它不起作用
var dataSource = new StaticDataSource({
columns: [
{
property: 'id',
label: 'id',
sortable: true
},
{
property: 'name',
label: 'groups',
sortable: true
},
{
property: 'name',
label: 'Roles',
sortable: true
}
],
formatter: function (items) {
$.each(items, function (index, item) {
item.roles= //string made from groups with comma
item.groups= //string made from roles with comma
},
data: this.collection,
delay: 250
});
$('#MyGrid').datagrid({
//as above
});
formatter: function (items) {
$.each(items, function (index, item) {
item.roles= //string of roles made from roles with comma
item.groups= /string of groups made from groups with comma
});
},
如果有人在这里帮助我,那将是非常有帮助的。
答案 0 :(得分:1)
对于列defs,每个property
应与数据源返回的属性名称匹配。试试这个:
{
property: 'id',
label: 'ID',
sortable: true
},
{
property: 'roles',
label: 'Roles',
sortable: true
},
{
property: 'groups',
label: 'Groups',
sortable: true
}
答案 1 :(得分:1)
我已经能够解决我的问题如下
formatter: function (items) {
$.each(items, function (index, item) {
var roleCell = new app.RoleSupplier({ model: item });
roleCell.render();
item.roles = roleCell.$el; //set property name above to roles
});
},
然后,我创建了RoleSupplier,如下所示
app.RolesSupplier = Backbone.View.extend({
template: _.template($('#roles-cell-template').html()),
render: function (eventName) {
this.$el.html(this.template(this.model.toJSON()));
}
});
我创建了如下模板
<script type="text/template" id="roles-cell-template">
<div>
<@ _.each(roles.toJSON(), function( role, index, list ){ @>
<@ if(index < (list.length - 1)) { @>
<@=role.authority + ','@>
<@ } else { @>
<@=role.authority@>
<@ } @>
<@ }); @>
</div>
</script>
这对我有用。但是,fuelux的用户必须了解的是:
您可以将属性名称设置为json中键的名称以设置其值
属性: 'ID', label:'ID'//将id值设置为列名ID
如果您有一些json值,并且您希望以特定方式显示它们,或者您无法在单元格中显示某些html,则将其格式设置如下
formatter: function (items) { $.each(items, function (index, item) { item.yourPropertyName1= format your html here that you want appear in label1 item.yourProeprtyName 2= fromat your html like button , anything you want inside column cell of label2 }); },