javascript - 为网格创建通用变量以重用每个网格中的配置

标签 javascript jquery asp.net-mvc model-view-controller devextreme

我正在使用 DevExpress 的 DevExtreme 控件,更具体地说是 DxDataGrid。

现在,每次我使用 DxDataGrid 时,我都需要指定如下配置:

var grid = $("#myGrid").dxDataGrid({
    // These options need to be specified for each DxDataGrid
    dataSource: {
        ...
    },
    columns: [
        ...                
    ],
    editing: {
        ...                
    },
    // The following options are always the same for every DxDataGrid
    columnChooser: {
        enabled: true
    },
    allowColumnReordering: true,
    sorting: {
        mode: 'multiple'
    },
    groupPanel: {
        visible: true,
        emptyPanelText: 'Blabla'
    },
    pager: {
        visible: true,
        showNavigationButtons: true,
        showInfo: true,
        showPageSizeSelector: true,
        allowedPageSizes: [10, 20, 50, 100]
    },
    paging: {
        pageSize: 10
    },
    filterRow: {
        visible: true
    },
    searchPanel: {
        visible: true,
        placeholder: 'Buscar'
    },
}).dxDataGrid('instance');

我想避免为每个 DxDataGrid 相同的所有配置参数重复代码。以下question DevExpress 支持成员解释说:

The data grid configuration is a regular JavaScript object. You can create an object variable with the default grid configuration and use it for each grid. Use the jQuery.extend() function to add new options/properties to your default configure object when necessary.

我不知道如何执行此操作,并且没有可用的代码示例。有什么帮助吗?

最佳答案

例如,创建一个包含常用选项的全局变量的外部文件

var commonOptions = { 
    columnChooser: {
        enabled: true
    },
    allowColumnReordering: true,
    ....
};

并在 View 中

<script src="~/Scripts/DxDataGridCommonOptions.js"></script> // load the common options into the view
var viewOptions = { 
    dataSource: {
    .... // your view specific options
    },
    ....
};
var grid = $("#myGrid").dxDataGrid(
    $.extend(commonOptions, viewOptions) // merge the 2 sets of options
).dxDataGrid('instance');

关于javascript - 为网格创建通用变量以重用每个网格中的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37525056/

相关文章:

javascript - 使用 websockets Golang 将更改推送到客户端

javascript - 将事件绑定(bind)到多个追加数据

javascript - 如何使svg之外的div不可点击

jquery - slider 上的模糊效果

javascript - 如何在没有 jquery/javascript 的情况下为 HTML5 日期字段添加验证/限制

javascript - 如何使用 jquery 在 div id 中查找类?

c# - 如何检测我的页面是否是回发的结果

asp.net-mvc - asp.net mvc 3 中的 AuthorizeAttribute 属性中使用 RedirectResult 的问题

asp.net-mvc - 使用 Telerik MVC Extensions Grid 在数据库服务器上进行分页

javascript - 在 JQuery 中使用 Modal 灵活的错误消息