本文介绍了如何从jqgrid json数据中删除多余的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免费Jqgrid具有操作"列.型号:

Free Jqgrid has actions column. colmodel:

{"hidden":false,"label":"","name":"_actions","width":72
,"align":"left","template":"actions","fixed":false,"resizable":true,
  "formatoptions":{"editbutton":true,"delbutton":true,"delOptions":{"url":"Delete" }}},

{"label":"Nimetus","name":"Nimi","index":"Nimi","editoptions":{"maxlength":80,"size":80 }

它是从远程json数据中填充的,例如

It is populated from remote json data like

{"total":1,
 "page":1,
  "rows":[{"id":"2ARVELDUSARV", "cell":[null,"2ARVELDUSARV"]},
          {"id":"ACME","cell":[null,"ACME"]},
          {"id":"KAKSKOERA","cell":[null,"KAKSKOERA"]}
 ]
}

在单元格数组中不使用第一列.如果删除了此列,则jqgrid无法正确呈现数据,因为此列必须作为操作列的占位符.如何解决此问题,以便jqgrid接受不带第一列的数据:

In cell array first column is not used.If this column is removed, jqgrid does not render data correctly since this column presence is required as placeholder for actions column.How to fix this so that jqgrid will accept data without first column:

{"total":1,
 "page":1,
  "rows":[{"id":"2ARVELDUSARV", "cell":[null,"2ARVELDUSARV"]},
          {"id":"ACME","cell":["ACME"]},
          {"id":"KAKSKOERA","cell":["KAKSKOERA"]}
 ]
}

更新

我寻找了答案中建议的数据格式更改.使用以下代码从ASP.NET MVC4中的sql select语句创建jqgrid数据.Web API将此序列化为jqgrid的json格式.

I looked for data format change as recommended in answer.jqgrid data is created from sql select statement in ASP.NET MVC4 using code below. Web API serializes this to format for json for jqgrid automatically.

如何创建可以序列化为propertyname的结果:答案中建议的值格式?

How to create result which can serialized to propertyname: value format recommended in answer ?

  object GetDataForJqGrid() {
        IDbConnection conn;
        using (var dataReader = DataAccessBase.ExecuteReader(sql.ToString(), out conn,
               CommandBehavior.CloseConnection | CommandBehavior.SingleResult,
               sql.GetParameters.ToArray()))
        {
            var rowList = new List<GridRow>();
            var pkeys = DatabasePrimaryKey();
            while (dataReader.Read())
            {
                var pkv = new List<object>();
                int offset = 1; // required for actions column
                var row = new GridRow
                {
                    id = IdHelper.EncodeId(pkv),
                    cell = new object[dataReader.FieldCount + offset + imageCount]
                };
                for (int j = 0; j < dataReader.FieldCount; j++)
                   row.cell[offset + j] = dataReader.GetValue(j);
                rowList.Add(row);
            }

            return new
            {
                total = rowList.Count() < rows ? page : page + 1,                                  page,
          rows = rowList
            };
}

public class GridRow
{
    public string id;
    public object[] cell;
}

推荐答案

最简单的方法是更改​​从服务器返回的数据格式,以使用数据的 repeatitems:false 样式.我的意思是

The most easy way would be to chanege the format of data returned from the server to use repeatitems: false style of the data. I mean the usage of

{
    "total": 1,
    "page": 1,
    "rows": [
        { "id": "2ARVELDUSARV", "Nimi": "2ARVELDUSARV" },
        { "id": "ACME", "Nimi": "ACME" },
        { "id": "KAKSKOERA", "Nimi": "KAKSKOERA"}
    ]
}

,或者在 Nimi

{
    "total": 1,
    "page": 1,
    "rows": [
        { "Nimi": "2ARVELDUSARV" },
        { "Nimi": "ACME" },
        { "Nimi": "KAKSKOERA"}
    ]
}

代替

{
    "total": 1,
    "page": 1,
    "rows": [{
        "id": "2ARVELDUSARV",
        "cell": ["2ARVELDUSARV"]
    }, {
        "id": "ACME",
        "cell": ["ACME"]
    }, {
        "id": "KAKSKOERA",
        "cell": ["KAKSKOERA"]
    }]
}

或者,您可以对当前数据格式使用 jsonReader:{repeatitems:false} 事件,并向其中添加jsonmap:"cell.0"属性,这意味着获取第一个元素(索引0)从数组 cell :

Alternatively one can use jsonReader: { repeatitems: false } event with your current format of data and add jsonmap: "cell.0" property to, which means getting the first element (index 0) from the array cell:

$("#list").jqGrid({
    datatype: "json",
    url: "andrus.json",
    colModel: [
        { label: "", name: "_actions", template: "actions" },
        { label: "Nimetus", name: "Nimi", jsonmap: "cell.0" }
    ],
    iconSet: "fontAwesome",
    jsonReader: { repeatitems: false }
});

请参见演示.

我个人建议您不要使用原始格式(带有值数组的 cell ),而应仅使用带有附加 id 属性的named属性(如果 id 值尚未包含在该项目中).如果确实要将解决方案与 jsonmap 一起使用,则应谨慎更改列的顺序(使用 remapColumns ),并在以后重新加载数据.更改列顺序后,您可能需要更新 jsonmap 值.因此,我重复一遍,建议您更改从服务器返回的数据格式.

I personally would recommend you don't use your original format (cell with array of values) and use just the named property with additional id property (if id value is not included in the item already). If you would do use the solution with jsonmap you should be carefully with changing the order of the columns (using remapColumns) and later reloading of data. You could required to update jsonmap values after the changing the column order. Thus I repeat that I recommend you to change format of data returned from the server.

已更新:问题的已更新部分构成了与jqGrid无关的绝对新问题.这是纯C#问题.尽管如此,我还是尝试回答,因为我也使用C#.

UPDATED: The Updated part of your question formulate absolutely new question which have no relation with jqGrid. It's pure C# problem. Nevertheless I try to answer, because I use C# too.

对代码进行最少的更改后,您可以执行以下操作:首先应使用System.Dynamic;添加;并使用System.Linq; 添加.然后,您应该使用(...){...} 内的代码替换为以下内容

What you can do with minimal changes of your code is the following: You should add using System.Dynamic; and using System.Linq; first of all. Then you should replace the code inside of using (...) {...} to about the following

var rowList = new List<dynamic>();
while (dataReader.Read()) {
    var row = new ExpandoObject() as IDictionary<string, object>;
    for (int j = 0; j < dataReader.FieldCount; j++) {
        if (!dataReader.IsDBNull(j)) {
            row.Add(dataReader.GetName(j), dataReader.GetValue(j));
        }
    }
    rowList.Add(row);
}

rowList 的序列化将产生names属性.如果您知道数据的主键,则可以以相同的方式添加具有相应值的 id 属性(使用 row.Add("id",IdHelper.EncodeId(pkv))).我没有包含该部分,因为您发布的代码不完整,并且 pkv 当前始终是 new List< object>(),这是错误的.如果数据具有组合键(多个值集是唯一的),则可以使用'_'(下划线)作为分隔符来对键进行字符串串联.

Serializing of rowList will produce the names properties. If you know the primary key of the data, then you can add id property with the corresponding value in the same way (using row.Add("id", IdHelper.EncodeId(pkv))). I don't included the part because the code which you posted is not full and pkv is currently always new List<object>(), which is wrong. If the data have composed key (multiple value set is unique) then you can make string concatenation of the keys using '_' (underscore) as the separator.

这篇关于如何从jqgrid json数据中删除多余的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:34