使用jqGrid的colModel的非类型化与JSON的“jsonmap'属性 [英] Using the 'jsonmap' property of jqGrid colModel with untyped JSON

查看:1984
本文介绍了使用jqGrid的colModel的非类型化与JSON的“jsonmap'属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的支持AJAX的WCF服务使用合同返回JSON,

My AJAX-enabled WCF service returns JSON using this contract,

[DataContract]
public class JQGridContract
{
    [DataContract]
    public class Row
    {
        [DataMember]
        public int id { get; set; }

        [DataMember]
        public List<string> cell { get; set; }

        public Row()
        {
            cell = new List<string>();
        }
    }

    [DataMember]
    public int page { get; set; }

    [DataMember]
    public int total { get; set; }

    [DataMember]
    public int records { get; set; }

    [DataMember]
    public List<Row> rows { get; set; }

    public JQGridContract()
    {
        rows = new List<Row>();
    }
}  

所以在结果的每一行数据是类型化 - 一排本质上只是没有任何附加列名的列表。

So each row of data in the results is untyped -- a row is essentially just a List without any column names attached.

我认为,它使我不可能用colModel的jsonmap'属性?基本上我从数据库中检索一个DataTable,然后把的DataTable这个JQGridContract形式。从包含在JSON数据表心不是列的信息传递到客户端,虽然。

I think that makes it impossible for me to use the 'jsonmap' attribute of colModel? Basically I am retrieving a DataTable from the database, and then putting that DataTable into this JQGridContract form. Column information from the DataTable isnt contained in the json passed to the client, though.

Id喜欢能够我的底层数据表的一列映射到我的jqGrid的一列,但无需强类型我的数据契约。这可能吗?我想这可能是使用匿名类型,其中匿名对象的列表(每个匿名对象是一个行)有对应于基础DataTable的每一列的属性,但我一直无法使这项工作。

Id like to be able to map a column of my underlying DataTable to a column of my jqGrid, but without the need to strongly type my data contract. Is this possible? I thought it might be using anonymous types, where a list of anonymous objects (each anonymous object being a row) that have properties corresponding to each column in the underlying DataTable, but I haven't been able to make that work.

感谢。

修改

下面是我想要实现(使用服务器端code,而不是JavaScript的)想要的例子。

Here is an example of want I want to achieve (using server-side code, rather than javascript).

下面基本上是对的jqGrid一列模型多数民众赞成在C#中完成的:

Below is essentially a column model for a jqGrid thats done in c#:

            return new JQGridColumnCollection()
            {
                new JQGridColumn()
                {
                    DataField = "ID",     // maps to the DataTable
                    DataType = typeof(int),
                    HeaderText = "ID",
                    PrimaryKey = true,
                },
                new JQGridColumn()
                {
                    DataField = "Name",  
                    DataType = typeof(string),
                    HeaderText = "Name"
                },
                new JQGridColumn()
                {
                    DataField = "Birthdate",  
                    DataType = typeof(DateTime),
                    HeaderText = "Birth Date" 
                }                
            };

每列的数据字段属性映射该列中的数据表underyling一列。 DataTable中列的顺序可能会有所不同:

The 'DataField' property of each column maps that column to a column in the underyling DataTable. The order of the columns in the DataTable could be different:

DataTable table = GetDataTable(" SELECT [Birthdate], [ID], [Name] From PersonTable "); 

但不管我怎么查询我的数据库,网格仍然会显示其中第一列是ID,第二列是名称,第三列是生日。我没有改变我的SQL查询,以改变我的网格中列的顺序。

But regardless of how I query my database, the grid will still show up where the first column is ID, the second column is Name, and the third column is Birthdate. I dont have to change my SQL query in order to change the order of the columns in my grid.

基本上我想在我的客户端colModel为一个jqGrid的数据字段属性的等价物。这就要求我的JSON列命名的,或者说我可以在jqGrid的列ATLEAST映射到JSON数据源中的列的数字索引。

I essentially want the equivalent of a DataField property in my client-side colModel for the jqGrid. That would require that my JSON columns are named, or that I can atleast map a jqGrid column to the numerical index of a column in the JSON data source.

推荐答案

您使用将获得可以通过标准来读取格式的数据的 JQGridContract jsonReader (见的这里详情)。在结果中的每行数据不是无类型。它具有类型为字符串。有没有需要使用 jsonmap 行中的字符串的位置定义为字符串所属网格的列。所以对于数据映射在单元的位置应使用列表中。

The JQGridContract class which you use will get the data in the format which can be read by the standard jsonReader (see here for details). Each row of data in the results is not "untyped". It has the type "string". There are no needs to use jsonmap. The position of the string in the row defines to which column of grid the string belong. So for the data mapping the position in the cell list should be used.

如果您使用 JQGridContract 类,你不需要任何的强类型数据转换。您可以轻松将任何数据类型的字符串和您的数据库表,使数据以 JQGridContract 实例。如果你这样做会出现问题,你应该追加与您使用的jqGrid的 colModel 定义你的问题。

If you use the JQGridContract class you don't need any strongly type data conversion. You can easy convert any data type to the string and so the data of your database table to JQGridContract instance. If you do will have problems you should append your question with the colModel definition of the jqGrid which you use.

更新时间::不要紧,在命令你使用的选择字段。其中为了您从可变数据的地方 JQGridContract 的实例,这是唯一重要的。你有一个方法,例如, GetUserBirthday 返回 JQGridContract 。该方法应在细胞列表中的 ID 转换成字符串,然后再在名地方,然后生日转换在ISO日期格式(YYY-MM-DD)。如果你想使用 JQGridContract ,你决定了你应该这样做。

UPDATED: It doesn't matter in which order you use fields in the SELECT. It is only important in which order you place the data from the table variable to the instance of JQGridContract. You have a method, for example, GetUserBirthday which returns JQGridContract. The method should place in the cell list the ID converted to string first, then the Name and then the Birthdate converted in the ISO date format (yyy-mm-dd). If you want use the JQGridContract which you defines you should do this.

顺便说一下数据表中不是最好的方式,让每选择数据。更有效地利用的SqlCommand SqlDataReader的

By the way the DataTable in not the best way to get the data per SELECT. More effectively to use SqlCommand and SqlDataReader.

这篇关于使用jqGrid的colModel的非类型化与JSON的“jsonmap'属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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