Extjs 4.x用按钮向上/向下移动网格记录

发布于 2025-01-26 18:29:15 字数 1313 浏览 3 评论 0原文

这篇文章是我较早的帖子的延续(在这里)我找不到工作解决方案(尚未)。简而言之: 我创建了一个Extjs(4.x)网格,该网格具有父母和孩子的记录。记录按顺序排序,在儿童中总是出现在父母下方,如下所示,“ p”将记录指定为父母,而“ c”将记录指定为儿童的记录。

“梅”是彼得的父母,奥丁是雷神和洛基的父母,布鲁斯还没有孩子被标记为父母(独立实体)

“在此处输入图像说明”

fiddle: https://fiddle.sencha.com/#view/#view/editor&eeditor&amp.fiddle/3jks

我希望更改排序的顺序当我上升/下降时,父母还带着孩子。起初,我想尝试拖放,但找不到一个工作解决方案(我尝试了Gird DragandDrop以及TreeGrid,Fiddles在我以前的 post )。

根据上一篇公开帖子的建议,我尝试对哪些小组和孩子进行分组,但我失去了拖放整个小组的能力。为了使用我创建的知识,我可以根据用户单击来向上或向下移动组。该分组基于等级列,因此,一旦我更新等级,记录应该理想地跳跃组。由于某种原因,当我在排名上单击“向上”按钮时:2组,布鲁斯更改为排名:1,梅·帕克(May Parker)更改为排名:2是正确的,但彼得·帕克(Peter Parker)仍然保持在等级:1。在调试代码之后,我发现商店出于某种原因将彼得的记录删除,并用另一份May Parker替换了(尽管网格没有显示)。我输入了控制台消息,我们可以观察到这一点。这里有什么问题?非常感谢指针和建议。如果有更好的方法可以根据等级移动这些记录,我也可以更改整个逻辑。

This post is continuation of my earlier post (here) for which I couldnt find a working solution (yet). In brief:
I have created a Extjs (4.X) grid which has records of parents and childrens. The records are in sorted order where in the childrens always appear just below their parents as shown here, where 'P' designates a record as parent while 'C' designates a record as children.

'May' is the parent of Peter, Odin is the parent of Thor and Loki, Bruce has no children yet marked as Parent (standalone entity)

enter image description here

fiddle: https://fiddle.sencha.com/#view/editor&fiddle/3jks

I wish to change sort order of the parents while also bringing their childrens along when I move the groups up/down. At first I wanted to try drag and drop but I couldnt find a working solution for that (I tried Gird DragAndDrop as well as TreeGrid, fiddles are on my previous post).

Based on a suggestion on the previous open post I tried Grouping which groups parent and child but I lost the ability to drag and drop the entire group. To work with what knowledge I have I have created buttons instead, for moving the groups up or down based on the user click. The grouping is based on Rank column so as soon as I update the rank the records should ideally jump groups. For some reason when I click on "UP" button on the Rank:2 group, BRUCE changes to Rank:1 and May Parker changes to Rank:2 which is correct but the Peter Parker still stays at Rank:1. After debugging the code I found that the store for some reason removed Peter's records and replaced it with another copy of May Parker (the grid doesnt show it though). I have put console messages where we can observe this. What could be the problem here? Pointers and suggestions are very much appreciated. I am open to changing the entire logic as well if there is a better way to move these records based on rank.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2025-02-02 18:29:15

这是一个工作 fiddle 用于GRID GROUPER drag-Drop。

以下是更多详细信息,有助于建立您的上下按钮示例:

您正在多次更新记录。

我建议您通过RecordID设置排名,以确保仅处理一次每个项目。否则,在您处理它们时,记录切换组。

    // I am using onBeforeGroupClick to handle collapsing correctly
    onBeforeGridGroupClick: function (grid, node, dataIndex, e, eOpts) {
        const targetIsButton = e.getTarget().type === 'button';

        console.log('[onBeforeGridGroupClick] targetIsButton', targetIsButton);

        if (targetIsButton) {
            this.updateRank(grid, node, dataIndex, e)
        }

        // return false will stop gridgroupclick from running
        return !targetIsButton;
    },

    // here I am updating the rank
    updateRank: function (grid, node, group, e) {
        console.log('[updateRank] dataIndex', group);

        const store = grid.getStore();

        // collect the group data for the clicked group
        const groups = store.getGroups(),
            currentGroupIndex = groups.findIndex('rank', group),
            currentGroup = groups.items[currentGroupIndex],
            // Ext.pluck gets the IDs from the current group
            currentRecordIds = Ext.pluck(currentGroup.items, 'id');

        // collecting the group data for the group you want to switch with
        const isUp = e.getTarget().value === 'Up';
            targetId = currentGroupIndex + (isUp ? 1 : -1)
            targetGroupIndex = groups.findIndex('rank', targetId),
            targetGroup = groups.items[targetGroupIndex],
            targetRecordIds = Ext.pluck(targetGroup.items, 'id');

        // define out of range conditions
        if(targetGroupIndex < 0) return;

        // stop the grid from updating while you work on the store
        store.suspendEvents(true);

        // set the rank for the target and current records
        Ext.Array.each(targetRecordIds, function(id) {
            const record = store.findRecord('id', id);
            
            record.set('rank', currentGroupIndex);
        })
        Ext.Array.each(currentRecordIds, function(id) {
            const record = store.findRecord('id', id);
            
            record.set('rank', targetGroupIndex);
        })

        // update the grid from all suspended store events
        store.resumeEvents();
    }

Here is a working fiddle for grid grouper header drag-drop.

Here are more details and some help to build your up-down button example:

You are updating the records several times.

I would suggest, that you set the rank by recordId to ensure each item is processed only once. Otherwise records switch groups while you are working on them.

    // I am using onBeforeGroupClick to handle collapsing correctly
    onBeforeGridGroupClick: function (grid, node, dataIndex, e, eOpts) {
        const targetIsButton = e.getTarget().type === 'button';

        console.log('[onBeforeGridGroupClick] targetIsButton', targetIsButton);

        if (targetIsButton) {
            this.updateRank(grid, node, dataIndex, e)
        }

        // return false will stop gridgroupclick from running
        return !targetIsButton;
    },

    // here I am updating the rank
    updateRank: function (grid, node, group, e) {
        console.log('[updateRank] dataIndex', group);

        const store = grid.getStore();

        // collect the group data for the clicked group
        const groups = store.getGroups(),
            currentGroupIndex = groups.findIndex('rank', group),
            currentGroup = groups.items[currentGroupIndex],
            // Ext.pluck gets the IDs from the current group
            currentRecordIds = Ext.pluck(currentGroup.items, 'id');

        // collecting the group data for the group you want to switch with
        const isUp = e.getTarget().value === 'Up';
            targetId = currentGroupIndex + (isUp ? 1 : -1)
            targetGroupIndex = groups.findIndex('rank', targetId),
            targetGroup = groups.items[targetGroupIndex],
            targetRecordIds = Ext.pluck(targetGroup.items, 'id');

        // define out of range conditions
        if(targetGroupIndex < 0) return;

        // stop the grid from updating while you work on the store
        store.suspendEvents(true);

        // set the rank for the target and current records
        Ext.Array.each(targetRecordIds, function(id) {
            const record = store.findRecord('id', id);
            
            record.set('rank', currentGroupIndex);
        })
        Ext.Array.each(currentRecordIds, function(id) {
            const record = store.findRecord('id', id);
            
            record.set('rank', targetGroupIndex);
        })

        // update the grid from all suspended store events
        store.resumeEvents();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文