【发布时间】:2011-06-14 00:49:48
【问题描述】:
我正在使用一个 ASMX 后端来填充 jqGrid 客户端,包括客户端的网格分页,以便一次加载所有数据行。我的问题是这是否是最好的方法性能和可靠性?此外,如果 WCF 比 ASMX 更好,那么将这个现有设置转换为 WCF 是否相当容易(我猜我应该使用 REST 样式的 WebGet 方法,但我并不肯定)。只希望一切都尽可能快速和响应迅速,理想情况下几乎没有回发。
这是 ASMX 代码:
public class JQGrid
{
public class Row
{
public int id { get; set; }
public List<string> cell { get; set; }
public Row()
{
cell = new List<string>();
}
}
public int page { get; set; }
public int total { get; set; }
public int records { get; set; }
public List<Row> rows { get; set; }
public JQGrid()
{
rows = new List<Row>();
}
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[ScriptService]
public class JQGridService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public JQGrid GetJQGrid(int pageIndex, int pageSize, string sortIndex, string sortDirection)
{
DataTable dt = GetDataTable( // command string );
if (dt == null)
throw new Exception("Unable to retrieve data.");
JQGrid jqGrid = new JQGrid();
foreach (DataRow sourceRow in dt.Rows)
{
JQGrid.Row targetRow = new JQGrid.Row();
targetRow.id = Convert.ToInt32(sourceRow["ID"]);
targetRow.cell.Add(sourceRow["ID"].ToString());
targetRow.cell.Add(sourceRow["SomeColumn"].ToString());
jqGrid.rows.Add(targetRow);
}
jqGrid.page = pageIndex;
jqGrid.records = jqGrid.rows.Count;
jqGrid.total = jqGrid.rows.Count; // Set this to total pages in your result...
return jqGrid;
}
}
客户端 JS:
function getData(pdata)
{
var params = new Object();
params.pageIndex = pdata.page;
params.pageSize = pdata.rows;
params.sortIndex = pdata.sidx;
params.sortDirection = pdata.sord;
$.ajax(
{
type: "POST",
contentType: "application/json; charset=utf-8",
url: "JQGridService.asmx/GetJQGrid",
data: JSON.stringify(params),
dataType: "json",
success: function (data, textStatus)
{
if (textStatus == "success") {
var thegrid = $("#jqGrid")[0];
thegrid.addJSONData(data.d);
}
},
error: function (data, textStatus) {
alert('An error has occured retrieving data!');
}
});
}
function pageLoad() { loadGrid() };
function loadGrid() {
$("#jqGrid").jqGrid({
gridComplete: function() {
$("#jqGrid").setGridParam({ datatype: 'local' });
},
datatype: function (pdata) {
getData(pdata);
},
colNames: ['ID', SomeColumn],
colModel: [
{ name: 'ID', index: 'ID', width: 150 },
{ name: SomeColumn, index: SomeColumn, width: 250}],
rowNum: 10,
rowList: [10, 20, 30],
viewrecords: false,
pagination: true,
pager: "#jqPager",
loadonce: true,
sortorder: "desc",
sortname: 'id',
cellEdit: false
});
}
【问题讨论】:
-
A 同意 David Hoerster 的观点,即 WFC 是最佳选择。我只想建议您重写您的客户端部分以不使用
datatype作为函数。此外,您应该使用sortname: 'ID'而不是sortname: 'id'并使用loadonce:true而不是手动将datatype更改为'local'内部的gridComplete。而不是使用params重命名postData您可以针对 jqGrid 的prmNames选项执行此操作:prmNames: {page: 'pageIndex', rows: 'pageSize', sort: 'sortIndex', order: 'sortDirection'}。
标签: javascript jquery asp.net wcf jqgrid