日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

使用Dynamic LINQ实现Ext Grid的远程排序

發(fā)布時(shí)間:2024/9/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Dynamic LINQ实现Ext Grid的远程排序 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

要實(shí)現(xiàn)Ext Grid的遠(yuǎn)程排序其實(shí)很簡(jiǎn)單,只要修改查詢語(yǔ)句的排序關(guān)鍵字就可以了,但是,如果你的項(xiàng)目是使用Linq進(jìn)行開(kāi)發(fā)的,會(huì)發(fā)現(xiàn)動(dòng)態(tài)修改排序關(guān)鍵字并不是那么容易的事,解決辦法就是使用LINQ Dynamic Query Library。LINQ Dynamic Query Library是一個(gè)很實(shí)用很強(qiáng)大的庫(kù)函數(shù),通過(guò)該庫(kù),可以輕松實(shí)現(xiàn)一些需要通過(guò)動(dòng)態(tài)參數(shù)實(shí)現(xiàn)的Linq查詢。

本文將通過(guò)一個(gè)實(shí)例演示如何使用LINQ Dynamic Query Library實(shí)現(xiàn)Ext Grid的遠(yuǎn)程排序。

LINQ Dynamic Query Library可以在VS2008的例程里找到,也可以從以下鏈接下載:

?

  • VB Dynamic Query Library (included in the /Language Samples/LINQ Samples/DynamicQuery directory)
  • C# Dynamic Query Library (included in the /LinqSamples/DynamicQuery directory)

?

本例子將使用SQL Server的“NORTHWND”樣例數(shù)據(jù)庫(kù)。Ext Grid顯示的是Employees表的數(shù)據(jù)。

以下是客戶端的完整代碼:

<html>

<head>

? <title></title>

? <meta http-equiv="Content-Type" content="text/html; charset=utf-8" xmlns="" />

? <link rel="stylesheet" type="text/css" href="lib/ext/resources/css/ext-all.css" />

? <link rel="stylesheet" type="text/css" href="css/application.css" />

</head>

? <script type="text/javascript" src="lib/ext/ext-base.js"></script>

? <script type="text/javascript" src="lib/ext/ext-all.js"></script>

? <script type="text/javascript" src="lib/ext/locale/ext-lang-zh_CN.js"></script>

<body scroll="no">

? <div id="loading-mask"></div>

? <div id="loading">

??? <div class="loading-indicator"><img alt="" src="lib/ext/resources/images/default/shared/large-loading.gif" width="32" height="32" style="margin-right:8px;" align="absmiddle"/>正在加載...</div>

? </div>

? <script type="text/javascript">

?

var app={}

?

Ext.onReady(function(){

?????? Ext.BLANK_IMAGE_URL='lib/ext/resources/images/default/s.gif';

? Ext.QuickTips.init();

?????? Ext.form.Field.prototype.msgTarget = 'side';

?????? Ext.Msg.minWidth=300;

?

?

?????? app.store=new Ext.data.Store({

??? url:'employees_action.ashx?act=list',

??? baseParams:{},

??? reader:new Ext.data.JsonReader({

??? ?????? totalProperty: "results",

?????? ? ??? root:"rows",

? ?????????? id:"id"

????????????? },[{name: 'id',type:'int'},{name:'lastname'},{name:'firstname'},

???????????????????? {name:'title'},{name:'titleofcourtesy'},{name:'city'},

????????????? ?????? {name:'address'},{name:'region'},{name:'postalcode'},{name:'homephone'},{name:'country'},

? ?????????? {name:'birthdate',type: 'date',dateFormat:'Y-m-d'},

? ?????????? {name:'hiredate',type: 'date',dateFormat:'Y-m-d'}

????????????? ]),

??? remoteSort: true

? }) //store

?

? app.pageToolbar=new Ext.PagingToolbar({

? ??? pageSize:3,displayInfo:true,store:app.store

? ??? });

?

? app.grid=new Ext.grid.GridPanel({layout:'fit',

? ??? store:app.store, autoExpandColumn:2,tbar:app.pageToolbar,

??? columns:

??? [

????? {id:'id',header: "ID",width:80,dataIndex:'id',sortable: true},

????? {header: "FirstName",width:80, dataIndex:'firstname',sortable: true},

????? {header: "LastName",width:80, dataIndex:'lastname',sortable: true},

????? {header: "Title",width:80, dataIndex:'title',sortable: true},

????? {header: "Title of Courtesy",width:80, dataIndex:'titleofcourtesy',sortable: true},

????? {header: "City",width:80, dataIndex:'city',sortable: true},

????? {header: "Region",width:80, dataIndex:'region',sortable: true},

????? {header: "Country",width:80, dataIndex:'country',sortable: true},

????? {header: "Postalcode",width:80, dataIndex:'postalcode',sortable: true},

????? {header: "Homephone",width:80, dataIndex:'homephone',sortable: true},

????? {header: "Birthdate", width: 120,dataIndex:'birthdate',sortable: true,renderer:Ext.util.Format.dateRenderer('Y-m-d')},

????? {header: "Hiredate", width: 120,dataIndex:'hiredate',sortable: true,renderer:Ext.util.Format.dateRenderer('Y-m-d')}

??? ]

? })

?

?

?????? var viewport = new Ext.Viewport({layout:'fit',items:[app.grid]});

?

?

?????? app.store.load();

??????

?

?????? setTimeout(function(){

??? Ext.get('loading').remove();

??? Ext.get('loading-mask').fadeOut({remove:true});

? }, 250);

?

?

})//onReady

</script>

</body>

</html>

?

?? 代碼很簡(jiǎn)單,定義了一個(gè)Store、PagetoolBarGrid。因?yàn)?/span>Employees表數(shù)據(jù)只有9條,所以設(shè)置了每頁(yè)3條數(shù)據(jù)。在Store定義中將remoteSort設(shè)置為true,說(shuō)明數(shù)據(jù)要實(shí)現(xiàn)遠(yuǎn)程排序。Grid的每一列都將sortable屬性設(shè)置為true,說(shuō)明都可以通過(guò)單擊Grid的列標(biāo)題實(shí)現(xiàn)排序。

?

以下是服務(wù)器端的完整代碼:

<%@ WebHandler Language="C#" Class="employees_action" Debug="true" %>

?

using System;

using System.Web;

using System.Linq;

using System.Linq.Dynamic;

using System.Collections;

using System.Collections.Generic;

using System.Web.Security;

using LitJson;

?

?

public class employees_action : IHttpHandler

{

???

? public void ProcessRequest (HttpContext context) {

??? string action = context.Request.Params["act"];

??? string outputStr = "";

??? if (action == null) action = "";

??? switch (action.ToLower())

??? {

????? case "list":

??????? outputStr = List(context);

??????? break;

????? default:

??????? outputStr = HDQ.Functions.WriteJsonResult(false, "錯(cuò)誤的操作類(lèi)型!");

??????? break;

??? }

??? context.Response.ContentType = "text/javascript";

??? context.Response.Write(outputStr);

? }

?

? public bool IsReusable {

??? get {

??????? return false;

??? }

? }

?

? private string List(HttpContext context)

? {

? ??int limit=0;

??? int.TryParse(context.Request.Params["limit"], out limit);

??? if (limit == 0) limit = 3;

??? int start=0;

??? int.TryParse(context.Request.Params["start"], out start);

??? string orderColumn = context.Request.Params["sort"];

??? string orderBy = context.Request.Params["dir"] == "ASC" ? "" : "descending";

??? switch (orderColumn)

??? {

????? case "id":

??????? orderColumn = "EmployeeID";

??????? break;

????? case "lastname":

??????? orderColumn = "LastName";

??????? break;

????? case "firstname":

??????? orderColumn = "FirstName";

??????? break;

????? case "title":

??????? orderColumn = "Title";

??????? break;

????? case "titleofcourtesy":

??????? orderColumn = "TitleOfCourtesy";

??????? break;

????? case "birthdate":

??????? orderColumn = "BirthDate";

??????? break;

????? case "hiredate":

??????? orderColumn = "HireDate";

??????? break;

????? case "address":

??????? orderColumn = "Address";

??????? break;

????? case "city":

??????? orderColumn = "City";

??????? break;

????? case "region":

??????? orderColumn = "Region";

??????? break;

????? case "postalcode":

??????? orderColumn = "PostalCode";

??????? break;

????? case "country":

??????? orderColumn = "Country";

??????? break;

????? case "homephone":

??????? orderColumn = "HomePhone";

???? ???break;

????? default:

??????? orderColumn = "EmployeeID";

??????? break;

??? }

??? DBDemosDataContext dc = new DBDemosDataContext();

??? int recordCount=0;

??? JsonWriter jw = new JsonWriter();

??? jw.WriteObjectStart();

??? jw.WritePropertyName("rows");

??? jw.WriteArrayStart();

??? recordCount = dc.Employees.Count();

??? if (start > recordCount) start = 0;

??? var q=dc.Employees.OrderBy(orderColumn + " " + orderBy).Skip(start).Take(limit);

??? foreach (var c in q)

??? {

????? jw.WriteObjectStart();

?? ???jw.WritePropertyName("id");

????? jw.Write(c.EmployeeID);

????? jw.WritePropertyName("firstname");

????? jw.Write(c.FirstName);

????? jw.WritePropertyName("lastname");

????? jw.Write(c.LastName);

????? jw.WritePropertyName("title");

????? jw.Write(c.Title);

????? jw.WritePropertyName("titleofcourtesy");

????? jw.Write(c.TitleOfCourtesy);

????? jw.WritePropertyName("address");

????? jw.Write(c.Address);

????? jw.WritePropertyName("city");

????? jw.Write(c.City);

????? jw.WritePropertyName("region");

????? jw.Write(c.Region);

????? jw.WritePropertyName("country");

????? jw.Write(c.Country);

????? jw.WritePropertyName("postalcode");

????? jw.Write(c.PostalCode);

????? jw.WritePropertyName("homephone");

????? jw.Write(c.HomePhone);

????? jw.WritePropertyName("birthdate");

????? jw.Write(c.BirthDate == null ? "" : Convert.ToDateTime(c.BirthDate).ToString("yyyy-MM-dd"));

????? jw.WritePropertyName("hiredate");

????? jw.Write(c.HireDate == null ? "" : Convert.ToDateTime(c.HireDate).ToString("yyyy-MM-dd"));

????? jw.WriteObjectEnd();

??? }

???

??? jw.WriteArrayEnd();

??? jw.WritePropertyName("results");

??? jw.Write(recordCount.ToString());

??? jw.WriteObjectEnd();

??? return jw.ToString();

? }

?

?

?

}

代碼中ProcessRequest方法根據(jù)提交的參數(shù)action執(zhí)行對(duì)應(yīng)的方法。本文主要是執(zhí)行List方法。

List方法的開(kāi)頭首先獲取了客戶端提交的幾個(gè)參數(shù),參數(shù)對(duì)應(yīng)的說(shuō)明請(qǐng)看下表:

參數(shù)

說(shuō)明

limit

每頁(yè)總數(shù),本例子是3

start

提取數(shù)據(jù)開(kāi)始位置

sort

要排序的列

dir

排序順序

?

獲取數(shù)據(jù)后需要對(duì)排序的列名和順序做一下轉(zhuǎn)換,以下語(yǔ)句就是實(shí)現(xiàn)排序順序的轉(zhuǎn)換:

string orderBy = context.Request.Params["dir"] == "ASC" ? "" : "descending";

?

列名的轉(zhuǎn)換則通過(guò)switch語(yǔ)句實(shí)現(xiàn)。如果在客戶端定義的列名與數(shù)據(jù)庫(kù)的真實(shí)列名相同,也可以不實(shí)施轉(zhuǎn)換。不過(guò),出于安全考慮,建議無(wú)論如何,還是要實(shí)行轉(zhuǎn)換。

轉(zhuǎn)換完成后,就可以定義查詢語(yǔ)句了,相當(dāng)?shù)暮?jiǎn)單:

var q=dc.Employees.OrderBy(orderColumn + " " + orderBy).Skip(start).Take(limit);

?

將列名變量和順序變量組合成字符串作為OrderBy方法的參數(shù)就可以了。LINQ Dynamic Query Library會(huì)自動(dòng)重新生成Linq語(yǔ)句執(zhí)行。

后面的代碼就是將查詢結(jié)果組合成Json格式數(shù)據(jù)輸出。

?

如果不使用LINQ Dynamic Query Library,遠(yuǎn)程排序的實(shí)現(xiàn)最直接的方法就是使用switch語(yǔ)句,根據(jù)提交的列和排序順序?qū)懖煌?/span>Linq語(yǔ)句,就不如本例的代碼那么簡(jiǎn)潔了。

?

?

以下是本例程的代碼下載地址:

http://download.csdn.net/source/1212462

轉(zhuǎn)載于:https://www.cnblogs.com/hainange/archive/2009/04/15/6334343.html

總結(jié)

以上是生活随笔為你收集整理的使用Dynamic LINQ实现Ext Grid的远程排序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。