使用JSON字符串为jQuery加载Flexigrid [英] Loading Flexigrid for jQuery with JSON String
问题描述
我正在尝试使用WCF服务返回的JSON字符串加载Flexigrid。
I am trying to load the Flexigrid by using a JSON String which is returned by a WCF Service.
我的服务有一个公共字符串GetContacts(string CustomerID)
方法,它返回一个Json字符串。
My Service has a public string GetContacts(string CustomerID)
method and it returns a Json string.
使用
从List对象创建JSON字符串 System.Web.Script.Serialization.JavaScriptSerializer
class。所以,我的目标是将JSON字符串绑定到我的Flexigrid作为对象。我使用
That JSON string is created from a List object by using
System.Web.Script.Serialization.JavaScriptSerializer
class. So, my aim is to bind the JSON string to the my Flexigrid as objects. I convert the web service result to objects using
var customer = eval("("+result+")");
结果是从服务返回的JSON字符串。有没有办法将客户对象绑定到Flexigrid?
The result is the JSON string being returned from service. Is there any way to bind customer objects to Flexigrid?
推荐答案
Flexigrid需要json中的格式如下
Flexigrid requires a format as follows in json
编辑感谢EAMann的格式更新。
EDIT Thanks to EAMann for the format update.
total: (no of rec)
page : (page no)
rows : [{cell: [ (col1 value) , (col2 value) ,.. ] },
{cell: [ (col1 value) , (col2 value) ,.. ] }]
以便绑定数据在网格上我更喜欢通过网络发送数据然后在客户端上格式化它,但那只是我的一个例子
in order to bind the data to the grid i prefer sending the data across the wire and then formatting it on the client, but thats just me heres an example
function formatCustomerResults(Customers){
var rows = Array();
for (i = 0; i < Customers.length; i++) {
var item = Customers[i];
//Do something here with the link
var link = "alert('opening item " + item.DealGuid + "');"
rows.push({ cell: [item.DealId,
item.Created,
item.CurrentStatus,
item.LastNote,
'<a href="javascript:void(0);" onclick="' + link + '" >view</a>']
});
}
return {
total: Customers.length,
page: 1,
rows: rows
};
}`
然后您只需要
$("#FlexTable").flexAddData(formatCustomerResults(eval(data)));
ps这最后一位是jquery语法
ps this last bit is jquery syntax
这篇关于使用JSON字符串为jQuery加载Flexigrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!