DevExtreme
我正在构建一个包含大量数据网格的应用程序(多通道),并且存在这个问题,我在教程和指南中找不到解决方案。
我dxDataGrid
用这种knockout
方法实现了一个,这是我的实体:
(function() {
Application1.playerViewModel = function(data) {
this.id = ko.observable();
this.firstname = ko.observable();
this.lastname = ko.observable();
this.fullname = ko.observable();
this.date_of_birth = ko.observable();
this.country = ko.observable();
this.team = ko.observable();
this.teamname = ko.observable();
this.position = ko.observable();
if(data)
this.fromJS(data);
};
$.extend(Application1.playerViewModel.prototype, {
toJS: function () {
return {
id: this.id(),
firstname: this.firstname(),
lastname: this.lastname(),
date_of_birth: this.date_of_birth(),
fullname: this.firstname()+" "+this.lastname,
country: Application1.db.objectLink("countries", this.country() ? this.country().id(): undefined),
team: Application1.db.objectLink("teams", this.team() ? this.team().id() : undefined),
teamname: this.team().name(),
position: Application1.db.objectLink("positions", this.position() ? this.position().id(): undefined),
};
},
fromJS: function(data) {
if(data) {
this.id(data.id);
this.firstname(data.firstname);
this.lastname(data.lastname);
this.fullname(data.firstname + " " + data.lastname);
this.date_of_birth(data.date_of_birth);
if(data.country)
this.country(new Application1.countryViewModel(data.country));
if(data.team){
this.team(new Application1.teamViewModel(data.team));
this.teamname = data.team.name;
}
if(data.position)
this.position(new Application1.positionViewModel(data.position));
}
}
});
})();
这是viewModel
:
Application1.players = function (params, viewInfo) {
"use strict";
var shouldReload = false,
openCreateViewAsRoot = viewInfo.layoutController.name === "split",
playersDataSource = new DevExpress.data.DataSource({
store: Application1.db.players,
map: function (item) {
return new Application1.playerViewModel(item);
}
}),
dataFieldList = [
{ dataField: 'firstname', allowGrouping: false },
{ dataField: 'lastname', allowGrouping: false },
{ dataField: 'date_of_birth', sortIndex: 0, sortOrder: 'asc', allowGrouping: false },
{ dataField: 'country', visible: false },
{ dataField: 'team', allowGrouping: true },
{ dataField: 'position', allowGrouping: true },
{ dataField: 'fullname', visible: false, allowGrouping: false}
], columnChooser = { enabled: true }, allowColumnReordering = true, sorting = { mode: 'multiple' },
groupPanel = { visible: true, emptyPanelText: 'Drag a column header here to group grid records' },
pager = { visible: true },
paging = { pageSize: 10 },
editing = {
editEnabled: true,
editMode: 'row',
insertEnabled: true,
removeEnabled: true
},
filterRow = { visible: true },
searchPanel = { visible: true },
selection = { mode: 'none' } ;
function handleplayersModification() {
shouldReload = true;
}
function handleViewShown() {
if (shouldReload) {
shouldReload = false;
dataSource.pageIndex(0);
dataSource.load();
}
}
function handleViewDisposing() {
Application1.db.players.off("modified", handleplayersModification);
}
function refreshList() {
dataSource.pageIndex(0);
dataSource.load();
}
Application1.db.players.on("modified", handleplayersModification);
return {
refreshList: refreshList,
viewShown: handleViewShown,
viewDisposing: handleViewDisposing,
openCreateViewAsRoot: openCreateViewAsRoot,
players: playersDataSource,
dataFieldList: dataFieldList,
columnChooser: columnChooser,
allowColumnReordering: allowColumnReordering,
sorting: sorting,
groupPanel: groupPanel,
pager: pager,
paging: paging,
editing: editing,
filterRow: filterRow,
searchPanel: searchPanel,
selection: selection
};
};
的HTML:
<div style="height:800px; margin: 0 auto"
data-bind="dxDataGrid:{ dataSource:players, columns:dataFieldList, columnChooser:columnChooser, allowColumnReordering:allowColumnReordering, sorting:sorting, groupPanel:groupPanel, pager:pager, paging:paging, editing:editing, filterRow:filterRow, searchPanel:searchPanel, selection:selection }"></div>
结果:
我尝试将team.name
andposition.name
放入dataFields
配置对象中dataFieldList
,但它返回空白列。