简体   繁体   English

使用jqGrid内联编辑排序

[英]Sorting with jqGrid Inline editing

Is there any direct way to sort the data on jqGrid at client side with inline editing? 有没有直接方法可以通过内联编辑在客户端对jqGrid上的数据进行排序? It does not sort data when the row is editable even if the row header is clicked. 即使单击行标题,当行可编辑时,它也不数据排序

Grid can't be sort if some line is editing. 如果某些行正在编辑,则网格无法排序。 I think that the reason of your question is a misunderstanding about how inline editing works and how sorting works. 我认为您提出这个问题的原因是对内联编辑的工作方式和排序方式的误解。

At the beginning of editing the original data from the editing row will be saved in internal parameter savedRow array. 在编辑开始时,来自编辑行的原始数据将保存在内部参数savedRow数组中。 In any time the user can revert the current modifications back to original state or save the current values in the grid. 用户可以随时将当前修改恢复到原始状态,或将当前值保存在网格中。 If keys: true option of inline editing is enabled then the user can use Esc or Enter keys to revert/save the changes of the current row. 如果启用了内联编辑的keys: true选项,则用户可以使用EscEnter键还原/保存当前行的更改。 It is permitted that multiple rows could be in the same time in editing mode and the user could save some rows and some other revert. 允许在编辑模式下同时显示多行,并且用户可以保存一些行和其他一些还原。

The sorting of grid means rebuilding of full grid content applying of the current filter from internal postData parameter. 网格排序意味着从内部postData参数应用当前过滤器重建整个网格内容 jqGrid supports multiple pages . jqGrid支持多个页面 Sorting of grid means always the sorting of optionally filtered over all data of the grid . 网格排序意味着始终对网格的所有数据进行可选过滤的排序。 After sorting if should be displayed only the current page based on the value of page parameter. 排序后,如果仅显示基于page参数值的当前页面。

To sort the grid which is in editing mode one would need to decide what should be done with currently editing rows. 要对处于编辑模式的网格进行排序,需要确定当前正在编辑的行应执行的操作。 Neither discarding of current changes nor saving could be good solution in common case. 在通常情况下,丢弃当前更改或保存都不是好的解决方案。 Even if one would try first to save the current editing data (old state and current state) then sort the data and later start editing one more time it could not work in common case. 即使人们先尝试保存当前的编辑数据(旧状态和当前状态),然后对数据进行排序,然后再开始编辑一次,在通常情况下却无法正常工作。 It could be many problems of implementation such scenario 实施这种情况可能是很多问题

  1. the current editing row could be on another page, which is not visible now 当前的编辑行可能在另一页上,该页面现在不可见
  2. the data could be changed now. 现在可以更改数据了。 So filling savedRow array with old data could be wrong and the current editing data could be also wrong. 因此,用旧数据填充savedRow数组可能是错误的,而当前的编辑数据也可能是错误的。
  3. the current editing row could be deleted from another user. 当前的编辑行可以从其他用户删除。 So it could not exist in new grid content. 因此它不可能存在于新的网格内容中。
  4. if we decide to save data before sorting if could be some validation errors or concurrency errors during saving. 如果我们决定在排序之前保存数据,则在保存期间是否可能存在验证错误或并发错误。 So one need to ask user first to solve all the conflicts before saving could take place. 因此,需要先要求用户解决所有冲突,然后才能进行保存。
    ... ...

So to sort grid which is in editing mode is not easy. 因此,对处于编辑模式的网格进行排序并不容易。 The implementation way could depend on the project requirements. 实施方式可能取决于项目要求。 Because of described above problems (and many other which I didn't mention) jqGrid just test whether internal savedRow array (used to save old state of row before editing started) is empty or not. 由于上述问题(以及许多其他我没有提到的问题),jqGrid只是测试内部的savedRow数组(用于在编辑开始之前保存行的旧状态)是否为空。 If the savedRow array is not empty then there are some row or cell (in case of sell editing mode) which is editing now. 如果savedRow数组不为空,则有一些行或单元格(在销售编辑模式下)正在编辑。 In the case any click on the corresponding column header will be ignored and no sorting is done. 在这种情况下,对相应列标题的任何单击都将被忽略,并且不进行任何排序。

I had this same problem --not being able to sort if any row is in edit mode. 我遇到了同样的问题-如果有任何行处于编辑模式,则无法排序。 In my case it didn't matter what the state of the user edits were. 就我而言,用户编辑状态是什么都没关系。 My grid is clientSide and there is a save button on the page the user's clicks to submit the changes. 我的网格是clientSide,用户单击以提交更改的页面上有一个保存按钮。

I did some digging around and found a solution that works for me. 我进行了一些挖掘,找到了适合我的解决方案。 I hope it helps others. 我希望它可以帮助其他人。 Basically what I did was reorder the click events on the column headings so that mine was first. 基本上,我要做的是对列标题上的click事件进行重新排序,使我的事件排在第一位。 Then in my click event I call saveRows. 然后在点击事件中,我调用saveRows。

Here is a piece of code I got from another thread here about reordering events : 这是我从另一个线程获得的有关事件重新排序的一段代码:

        $.fn.bindUp = function(name, fn) {
        // bind as you normally would
        // don't want to miss out on any jQuery magic
        this.on(name, fn);

        // Thanks to a comment by @Martin, adding support for
        // namespaced events too.
        this.each(function() {
            var handlers = $._data(this, 'events')[name.split('.')[0]];
            // take out the handler we just inserted from the end
            var handler = handlers.pop();
            // move it at the beginning
            handlers.splice(0, 0, handler);
        });
    };

Then in my code I add a handler to save the rows: 然后在我的代码中添加一个处理程序以保存行:

        $("th.ui-th-column", $("#pacPaymenReviewGridNEW")[0].grid.hDiv).bindUp('click', function(e) {
        var $th = $(e.currentTarget).closest("th");
        if ($th.length > 0) {
            pacTransferFundMaint.saveRowsById('#pacPaymenReviewGridNEW');
        }
    });     

After research, I found the following algorithm: 经过研究,我发现以下算法:

  1. Catch the header click event. 捕获标题单击事件。 eg ('th').click(function(){}) 例如('th')。click(function(){})
  2. Save the the row data which is in edit mode using saveRow when this event occurs 发生此事件时,使用saveRow保存处于编辑模式的行数据
  3. Identify the header which is clicked. 标识被单击的标题。
  4. Sort grid data with that header in ascending or descending order. 使用该标题以升序或降序对网格数据进行排序。 like 喜欢
    $('#dataGrid').jqGrid('setGridParam', ({ data: list, sortname: columnHeader, sortorder: 'asc',rowNum: list.length })).trigger('reloadGrid');

  5. Make the selected row editable again. 使所选行再次可编辑。 editRow can be used for this. editRow可以用于此目的。

Note: Steps 2 to 5 should be executed in event at steps 1 (header click event) 注意:应该在步骤1的事件中执行步骤2至5(标题单击事件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM