开发者

Using the 'jsonmap' property of jqGrid colModel with untyped 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.

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 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开发者_Python百科 underlying DataTable, but I haven't been able to make that work.

Thanks.

EDIT

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

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" 
                }                
            };

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 "); 

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.

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.


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.

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.

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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜