JQGrid:将列重置为原始顺序和状态 [英] JQGrid: Resetting the Columns to original Order and state

查看:125
本文介绍了JQGrid:将列重置为原始顺序和状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jqGrid选项 sortable:true 并使用columnChooser插件。
我想创建一个重置按钮


  1. 将列重新排序为原始状态

  2. 添加可能已被columnChooser插件隐藏的所有列。这是我能够使用这里提供的答案来完成的。 / li>

但是,如何完成将列重新排序为原始状态的第一个点。

解决方案

恢复隐藏所有列的状态非常简单,您已找到相应的代码示例。因此,我回答了如何重置列顺序。



问题是方法 remapColumns 可用于对列重新排序使用列号而不是列名。 jqGrid的许多其他方法或内部参数也存在同样的问题。要使用方法 remapColumns ,您需要使用基于当前索引顺序的列索引。在用户多次更改列的顺序后,很难提供您希望相对于当前列顺序的列号。保持列名而不是列索引会更容易



我开发免费jqGrid 作为jqGrid的分支guriddo.net/?page_id=103334rel =nofollow noreferrer> Guriddo jqGrid JS 。我在jqGrid中实现了许多变化,这些变化扰乱了我并实现了许多新功能。改变中的一个是将所有内部选项移动到保持名称而不是索引,尽可能在不破坏与先前版本的兼容性的情况下。我添加了方法 remapColumnsByName ,这简化了列重映射的使用(请参阅答案) 使用演示)。



在免费的jqGrid中实现您的要求可能非常简单。您只需将原始列顺序保存在数组中,然后将其用作 remapColumnsByName 的参数来重置列



< pre class =lang-js prettyprint-override> $(#grid1)。jqGrid({
...
onInitGrid:function(){
var p = $(this).jqGrid(getGridParam);
//保存jqGrid的自定义选项中列的名称
p.originalColumnOrder = $ .map(p.colModel,function(cm) {
return cm.name;
});
//alert(JSON.stringify(p.originalColumnOrder));
}
})。jqGrid( navGrid,{add:false,edit:false,del:false})
.jqGrid(navButtonAdd,{
buttonicon:fa-repeat,
caption:,
title:重置原始列顺序,
onClickButton:function(){
var $ self = $(this),p = $ self.jqGrid(getGridParam);
$ self.jqGrid(remapColumnsByName,p.originalColumnOrder,true);
}});

参见演示 https://jsfiddle.net/OlegKi/r6b2os5b/2/



如果你无法迁移到免费jqGrid因为某些原因你必须模拟相同的行为。您应该将原始列顺序保存为列名,然后在调用 remapColumns 之前将名称直接转换为索引。它也应该有效。


I am using jqGrid with the option sortable:true and also using the columnChooser plugin. I would like to create a reset button which will

  1. Reorder the columns to the original state
  2. Add back all columns which might have been hidden by columnChooser plugin. This I was able to accomplish using the answer provided here

However, how do I accomplish the first point reordering the column to original state.

解决方案

Restore hidden state of all columns is very simple and you found already the corresponding code examples. Thus I answer how to reset the column order.

The problem is that the method remapColumns which can be used to reorder the columns use column numbers instead of column names. The same problem exist in many other methods or internal parameters of jqGrid. To use the method remapColumns you need to use the index of columns based on the current index order. After the user changed the order of columns multiple times it would be difficult to provide the column numbers which you want to have relatively to the current column order. It would be much more easy to hold the names of columns instead of column indexes.

I develop free jqGrid as a fork of jqGrid after Tony had changed the licence agreement of jqGrid and renamed it to Guriddo jqGrid JS. I implemented many changes in jqGrid which disturbs my and have implemented many new features. One from the changes was moving all internal options to holding names instead of indexes as far it was possible without breaking the compatibility to the previous versions. I added the method remapColumnsByName, which simplifies the usage of column remapping (see the answer with the demo).

The implementation of your requirements in free jqGrid could be very simple. You need just save the original column order in an array and then use it as the parameter of remapColumnsByName to reset the columns

$("#grid1").jqGrid({
    ...
    onInitGrid: function () {
        var p = $(this).jqGrid("getGridParam");
        // save names of columns in custom option of jqGrid
        p.originalColumnOrder = $.map(p.colModel, function (cm) {
            return cm.name;
        });
        //alert(JSON.stringify(p.originalColumnOrder));
    }
}).jqGrid("navGrid", { add: false, edit: false, del: false })
.jqGrid("navButtonAdd", {
    buttonicon: "fa-repeat",
    caption: "",
    title: "Reset original column order",
    onClickButton: function () {
        var $self = $(this), p = $self.jqGrid("getGridParam");
        $self.jqGrid("remapColumnsByName", p.originalColumnOrder, true);
    }});

See the demo https://jsfiddle.net/OlegKi/r6b2os5b/2/

If you can't migrate to free jqGrid because of some reasons then you have to simulate the same behavior. You should save original column order as array of column names and then to convert the names to indexes directly before calling of remapColumns. It should work too.

这篇关于JQGrid:将列重置为原始顺序和状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆