Persist expanded groups after read

1 Answer 29 Views
Grid
William
Top achievements
Rank 1
William asked on 18 Jan 2024, 02:09 AM

Hello,

 

We have a grid with a default grouping applied to it, and are trying to persist the expanded groups through a read operation (remote datasource).

 

We have been successful in persisting them through some operations using the following code:

function getExpandedGroups() {
    expandedGroups = [];
    try {
        let groupRows = $(".k-grouping-row");
        for (let i = 0; i < groupRows.length; i++) {
            let e = groupRows[i];
            let expanded = e.children[0]["ariaExpanded"];
            if (expanded === "true") {
                let groupName = $(e).find(".k-reset").text();
                let slicePos = groupName.lastIndexOf(" (");
                groupName = groupName.slice(0, slicePos);
                expandedGroups.push(groupName);
            }
        }
    }
    catch { }
    return expandedGroups;
}

function restoreExpandedGroups() {
    try {
        groupRows = $(".k-grouping-row");
        for (let i = 0; i < groupRows.length; i++) {
            let e = groupRows[i];
            let groupName = $(e).find(".k-reset").text();
            let slicePos = groupName.lastIndexOf(" (");
            groupName = groupName.slice(0, slicePos);
            if (expandedGroups.includes(groupName)) {
                attachmentTable.expandGroup(e);
            }
        }
    }
    catch { }
}

which works fine if our code calls the read event, since we can execute it before and after eg.

    let expandedGroups = getExpandedGroups();
    attachmentTable.dataSource.read().then(function () {    
        restoreExpandedGroups(expandedGroups);
    });

however, when we use any of the grid functionality like sorting on a column, there doesn't seem to be any events that we can subscribe to that occur after the grid sets the groups back to their collapsed state, and we aren't able to persist the grouping in the same way.

 

Are there any potential workarounds for this issue?

 

Thanks

 

Regards,

Will Douglas

 

1 Answer, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 22 Jan 2024, 10:38 AM

Hello William,

The Grid API provides the sort event when the Grid is sorted. You can try to handle the event in order to get information about the applied sorting.

https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/sort

However, I would suggest taking a look at the following articles that could help achieve the desired result:

- Linked here you will find a Knowledge Base article regarding - Persist Collapsed State of Grouped Recordshttps://docs.telerik.com/kendo-ui/knowledge-base/persist-grouped-grid-collapsed-details-state. As you will see in the article, when the 'Refresh Grid' button is clicked, the applied sorting is persisted.

- The Grid Persist State Demo demonstrates how the current filters, sorts, groups, visible columns, etc can be persisted using the getOptions and setOtpions methods. - https://demos.telerik.com/kendo-ui/grid/persist-state

Please take a look at the provided information and let me know in case you have any additional questions on the matter. 

Regards,
Neli
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Tags
Grid
Asked by
William
Top achievements
Rank 1
Answers by
Neli
Telerik team
Share this question
or