1

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.nameandposition.name放入dataFields配置对象中dataFieldList,但它返回空白列。

4

1 回答 1

3

通常,您不需要在 dxDataGrid 数据源中使用 Knockout 驱动的视图模型,因为 dxDataGrid 提供了开箱即用的 CRUD 支持。因此,没有什么能阻止您构建与您的列模式完全匹配的平面对象,而不是使用生成的视图模型。

此外,如果您使用的是 ODataContext,它不会加载导航属性,除非您通过扩展选项明确指定。有关详细信息,请阅读协会文档。

您的数据源定义可能如下:

playersDataSource = new DevExpress.data.DataSource({
    store: Application1.db.players,
    map: function (item) {
        return {
            firstname: item.firstname,
            lastname: item.lastname,
            date_of_birth: item.date_of_birth,
            country: item.country.name,
            team: item.team.name,
            position.item.position.name,
            fullname: item.fullname
        };
    },
    expand: ['country', 'team', 'position']
}),

更新 我之前的答案不是最好的,因为如果你这样声明数据源,你将不会编辑国家、团队和职位。要支持编辑,请从数据源中删除map方法并使用查找列。在这种情况下,您甚至不需要扩展导航属性,因为它们将通过查找进行扩展。这是一个简短的例子:

playersDataSource = new DevExpress.data.DataSource({
    store: Application1.db.players
}),
columns:[{ 
    dataField: country, 
    lookup: { 
        dataSource: { store: Application1.db.countries }, 
        displayExpr: 'name',
        valueExpr: 'id'
    }
}]
于 2015-02-05T06:51:39.777 回答