如何按用户添加的列保存组

how to save group by columns added by user

本文关键字:保存 添加 何按 用户      更新时间:2023-09-26

我是java脚本的新手,刚开始研究剑道网格的一个需求。要求是,当用户拖放列进行分组时,即使在用户注销wpf应用程序并返回后,他们也希望保存此分组。

因此,有人能建议我是否有办法将这个分组保存在某个地方,这样我每次加载网格时都可以引用它,并按上次保存的分组逐列显示数据。

如有任何帮助,我们将不胜感激。

是的,您可以通过JavaScript将首选项存储在浏览器cookie中来实现这一点。您可以跟踪列的位置,并将它们与user-id一起存储在cookie变量中。然后,每当用户访问页面时,您只需从cookie中检索保存的变量并获取信息,然后相应地设置列的位置。

设置和获取cookie 的JavaScript函数

/*function to set/update a cookie variable.
 *@data is the data to be stored.
 *@key is the key value to the data.*/
function saveToCookie(data,key){
    /*setting cookie variable and expiry date/time*/
    var exdays=365
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(data) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie = key+ "="+c_value
}

/*function to get a cookie variable.
 *@key is the key value to the variable*/
function getCookie(key){
    var cookies = document.cookie.match(key+'=(.*?)(;|$)');
    if (cookies) {
        return unescape(cookies[1]);
    }
    return null;
}

您可以监听DataGrid dataSource更改事件以获取组信息。

var groupingFields;
var grid = $("#grid").kendoGrid({
    dataSource: {
        type: "odata",
        transport: {
            read: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Customers"
        },
        pageSize: 20,
        change: function(e) {
            // array of grouping objects
            groupingFields = this.group();
            console.log(groupingFields);
            // here you can check if grouping has changed
            // then you save changed groupings to cookies
        }
    },
    height: 550,
    groupable: true,
    sortable: true,
    pageable: {
        refresh: true,
        pageSizes: true,
        buttonCount: 5
    },
    columns: [{
        field: "ContactName",
        title: "Contact Name",
        width: 200
    }, {
        field: "ContactTitle",
        title: "Contact Title"
    }, {
        field: "CompanyName",
        title: "Company Name"
    }, {
        field: "Country",
         width: 150
    }]
}).data("kendoGrid");
// like this you can set grouping to grid datasource
function setGrouping(groups) {
    grid.dataSource.group(groups)
}