jQuery 學(xué)習(xí)第六課 實現(xiàn)一個Ajax的TreeView
更新時間:2010年05月17日 22:51:27 作者:
TreeView是asp.net自帶的控件,不過自帶的控件在靈活性上有諸多限制。在jQuery的幫助下,自己實現(xiàn)一個TreeView也不困難。本文是前幾篇文章所講內(nèi)容的一個綜合演練。
最終實現(xiàn)的效果是一個目錄文件查看器,如圖所示:
其原理是,當用戶單擊一個目錄的時候,將這個目錄的路徑發(fā)送給服務(wù)器端,服務(wù)器端返回這個目錄中的文件和目錄信息。在服務(wù)器端,定義一個如下的類來表示要傳遞的文件信息:
public class FileInformation
{
public string FullPath
{
get; set;
}
public string Name
{
get; set;
}
public string Info
{
get; set;
}
public bool IsFolder
{
get; set;
}
}
其中FullPath是文件的完整路徑,用于獲取它的子文件夾/文件用,Name是文件的名字,用于顯示,IsFolder是區(qū)分這條數(shù)據(jù)是一個文件還是文件夾,以便用不同的圖標來顯示,最后一個Info是一些附加信息,在此例中沒有用到。根據(jù)一個路徑獲得目錄中的文件信息的C#代碼很簡單,順便就貼在這里:
public class FileManager
{
public static List<FileInformation> GetFolderContent(string fullpath)
{
List<FileInformation> res = new List<FileInformation>();
DirectoryInfo info = new DirectoryInfo(fullpath);
if (info.Exists)
{
foreach (DirectoryInfo d in info.GetDirectories())
{
res.Add(new FileInformation
{
FullPath = d.FullName, Name = d.Name,IsFolder = true,
Info = "Any More Information goes here"
});
}
foreach (FileInfo f in info.GetFiles())
{
res.Add(new FileInformation
{
FullPath = f.FullName,Name = f.Name,IsFolder = false,
Info = "Any More Information goes here"
});
}
}
return res;
}
}
此例中采用JSON數(shù)據(jù)的格式來傳遞這些信息。因此要將這些數(shù)據(jù)序列化。在.Net 3.5中,有現(xiàn)成的將實體類序列化成JSON數(shù)據(jù)的類,使用方法如下
public static string ToJson<T>(T obj)
{
DataContractJsonSerializer d = new DataContractJsonSerializer(typeof(T));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
d.WriteObject(ms, obj);
string strJSON = System.Text.Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return strJSON;
}
如果是.net 2.0,則可以尋找一些第三方的組件,自己寫一個也不麻煩。
至此,服務(wù)器端的主要工作已經(jīng)完成了。新建一個Genric Handler文件,filelist.ashx,代碼如下,簡單的響應(yīng)下請求,輸出數(shù)據(jù)即可:
public class FileList : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string path = context.Request.QueryString["path"];
string data = JsonHelper.ToJson<List<FileInformation>>(FileManager.GetFolderContent(path));
context.Response.Write(data);
}
public bool IsReusable {
get {
return false;
}
}
}
下面考慮客戶端html代碼的編寫。最主要的就是兩個事件,也就是鼠標點擊發(fā)送ajax請求,處理返回的json數(shù)據(jù)生成html代碼,鼠標再次點擊將html代碼清空。在這里,采用ul li來顯示這個treeview,在li中有一個不可見的span,里面包含了文件完整的路徑,它將用作發(fā)起ajax請求的參數(shù),但是對用戶是不可見的。
HTML代碼很簡單,就4行:
<body>
<ul>
</ul>
</body>
首先需要初始化一個根目錄,例如D:,代碼如下:
$(function() {
$('<li class="folder">D:\\<span class="fullpath">D:\\</span></li>').appendTo('ul');
$('li').hover(function() {
$(this).css('cursor', 'pointer');
},
function() { $(this).css('cursor', 'default'); });
$('li.folder').toggle(LoadFile, CloseFolder);
});
構(gòu)造好一個li結(jié)點,添加到ul中去,然后設(shè)置下鼠標動作的樣式,最后為其綁定事件處理程序,LoadFile和CloseFolder。
function LoadFile(event) {
if (this == event.target) {
var path = $(this).find('span').html();
var node = $('<ul>');
$(this).append(node);
$.getJSON('filelist.ashx', { path: path }, function(data) {
$.each(data, function() {
if (this.IsFolder) {
node.append($('<li>').addClass('folder').html(this.Name).append($('<span>').addClass('fullpath').html(this.FullPath)));
}
else {
node.append($('<li>').addClass('file').html(this.Name).append($('<span>').addClass('fullpath').html(this.FullPath)));
}
});
node.find('li.folder').toggle(LoadFile, CloseFolder);
});
}
}
首先要判斷event的target和this是否是同一個對象,以避免點擊子節(jié)點事件浮升的時候造成多次觸發(fā)。首先利用find和html函數(shù)獲得完整的路徑。構(gòu)造好一個ul節(jié)點并把它添加到當前的li中。此時ul是空的,接下來發(fā)起ajax請求,獲得服務(wù)器端的數(shù)據(jù)。對每條數(shù)據(jù)生成一個li,其中對于是否是目錄加以判斷,生成帶有不同class的li,再加到node中。最后,不要忘記為新增的節(jié)點也綁定事件處理程序。代碼還是比較簡單的,至于關(guān)閉目錄節(jié)點的代碼就更加簡單了,
function CloseFolder(event) {
if (this == event.target)
$(this).find('ul').remove();
}
至此此范例已經(jīng)完成了。還少了幾句css,不再列出。
這個例子實現(xiàn)的功能和樣式都比較粗糙,不過在此基礎(chǔ)上做更多的擴展和美化已經(jīng)不是難事。例如可以加上一點現(xiàn)成的動畫效果:
function CloseFolder(event) {
if (this == event.target) {
var node = $(this).find('ul');
node.hide('slow', function() { $(this).find('ul').remove(); });
}
}
先隱藏,再刪除。類似地,可以加載完畢后立刻隱藏,再淡出。
其原理是,當用戶單擊一個目錄的時候,將這個目錄的路徑發(fā)送給服務(wù)器端,服務(wù)器端返回這個目錄中的文件和目錄信息。在服務(wù)器端,定義一個如下的類來表示要傳遞的文件信息:
復(fù)制代碼 代碼如下:
public class FileInformation
{
public string FullPath
{
get; set;
}
public string Name
{
get; set;
}
public string Info
{
get; set;
}
public bool IsFolder
{
get; set;
}
}
其中FullPath是文件的完整路徑,用于獲取它的子文件夾/文件用,Name是文件的名字,用于顯示,IsFolder是區(qū)分這條數(shù)據(jù)是一個文件還是文件夾,以便用不同的圖標來顯示,最后一個Info是一些附加信息,在此例中沒有用到。根據(jù)一個路徑獲得目錄中的文件信息的C#代碼很簡單,順便就貼在這里:
復(fù)制代碼 代碼如下:
public class FileManager
{
public static List<FileInformation> GetFolderContent(string fullpath)
{
List<FileInformation> res = new List<FileInformation>();
DirectoryInfo info = new DirectoryInfo(fullpath);
if (info.Exists)
{
foreach (DirectoryInfo d in info.GetDirectories())
{
res.Add(new FileInformation
{
FullPath = d.FullName, Name = d.Name,IsFolder = true,
Info = "Any More Information goes here"
});
}
foreach (FileInfo f in info.GetFiles())
{
res.Add(new FileInformation
{
FullPath = f.FullName,Name = f.Name,IsFolder = false,
Info = "Any More Information goes here"
});
}
}
return res;
}
}
此例中采用JSON數(shù)據(jù)的格式來傳遞這些信息。因此要將這些數(shù)據(jù)序列化。在.Net 3.5中,有現(xiàn)成的將實體類序列化成JSON數(shù)據(jù)的類,使用方法如下
復(fù)制代碼 代碼如下:
public static string ToJson<T>(T obj)
{
DataContractJsonSerializer d = new DataContractJsonSerializer(typeof(T));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
d.WriteObject(ms, obj);
string strJSON = System.Text.Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return strJSON;
}
如果是.net 2.0,則可以尋找一些第三方的組件,自己寫一個也不麻煩。
至此,服務(wù)器端的主要工作已經(jīng)完成了。新建一個Genric Handler文件,filelist.ashx,代碼如下,簡單的響應(yīng)下請求,輸出數(shù)據(jù)即可:
復(fù)制代碼 代碼如下:
public class FileList : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string path = context.Request.QueryString["path"];
string data = JsonHelper.ToJson<List<FileInformation>>(FileManager.GetFolderContent(path));
context.Response.Write(data);
}
public bool IsReusable {
get {
return false;
}
}
}
下面考慮客戶端html代碼的編寫。最主要的就是兩個事件,也就是鼠標點擊發(fā)送ajax請求,處理返回的json數(shù)據(jù)生成html代碼,鼠標再次點擊將html代碼清空。在這里,采用ul li來顯示這個treeview,在li中有一個不可見的span,里面包含了文件完整的路徑,它將用作發(fā)起ajax請求的參數(shù),但是對用戶是不可見的。
HTML代碼很簡單,就4行:
復(fù)制代碼 代碼如下:
<body>
<ul>
</ul>
</body>
首先需要初始化一個根目錄,例如D:,代碼如下:
復(fù)制代碼 代碼如下:
$(function() {
$('<li class="folder">D:\\<span class="fullpath">D:\\</span></li>').appendTo('ul');
$('li').hover(function() {
$(this).css('cursor', 'pointer');
},
function() { $(this).css('cursor', 'default'); });
$('li.folder').toggle(LoadFile, CloseFolder);
});
構(gòu)造好一個li結(jié)點,添加到ul中去,然后設(shè)置下鼠標動作的樣式,最后為其綁定事件處理程序,LoadFile和CloseFolder。
復(fù)制代碼 代碼如下:
function LoadFile(event) {
if (this == event.target) {
var path = $(this).find('span').html();
var node = $('<ul>');
$(this).append(node);
$.getJSON('filelist.ashx', { path: path }, function(data) {
$.each(data, function() {
if (this.IsFolder) {
node.append($('<li>').addClass('folder').html(this.Name).append($('<span>').addClass('fullpath').html(this.FullPath)));
}
else {
node.append($('<li>').addClass('file').html(this.Name).append($('<span>').addClass('fullpath').html(this.FullPath)));
}
});
node.find('li.folder').toggle(LoadFile, CloseFolder);
});
}
}
首先要判斷event的target和this是否是同一個對象,以避免點擊子節(jié)點事件浮升的時候造成多次觸發(fā)。首先利用find和html函數(shù)獲得完整的路徑。構(gòu)造好一個ul節(jié)點并把它添加到當前的li中。此時ul是空的,接下來發(fā)起ajax請求,獲得服務(wù)器端的數(shù)據(jù)。對每條數(shù)據(jù)生成一個li,其中對于是否是目錄加以判斷,生成帶有不同class的li,再加到node中。最后,不要忘記為新增的節(jié)點也綁定事件處理程序。代碼還是比較簡單的,至于關(guān)閉目錄節(jié)點的代碼就更加簡單了,
復(fù)制代碼 代碼如下:
function CloseFolder(event) {
if (this == event.target)
$(this).find('ul').remove();
}
至此此范例已經(jīng)完成了。還少了幾句css,不再列出。
這個例子實現(xiàn)的功能和樣式都比較粗糙,不過在此基礎(chǔ)上做更多的擴展和美化已經(jīng)不是難事。例如可以加上一點現(xiàn)成的動畫效果:
復(fù)制代碼 代碼如下:
function CloseFolder(event) {
if (this == event.target) {
var node = $(this).find('ul');
node.hide('slow', function() { $(this).find('ul').remove(); });
}
}
先隱藏,再刪除。類似地,可以加載完畢后立刻隱藏,再淡出。
您可能感興趣的文章:
- 基于MVC5和Bootstrap的jQuery TreeView樹形控件(二)之數(shù)據(jù)支持json字符串、list集合
- 基于MVC5和Bootstrap的jQuery TreeView樹形控件(一)之數(shù)據(jù)支持json字符串、list集合
- jquery實現(xiàn)點擊TreeView文本父節(jié)點展開/折疊子節(jié)點
- ASP.NET中基于JQUERY的高性能的TreeView補充
- 打造基于jQuery的高性能TreeView(asp.net)
- 為jQuery.Treeview添加右鍵菜單的實現(xiàn)代碼
- Jquery.TreeView結(jié)合ASP.Net和數(shù)據(jù)庫生成菜單導(dǎo)航條
- 選擇TreeView控件的樹狀數(shù)據(jù)節(jié)點的JS方法(jquery)
- jQuery 樹形結(jié)構(gòu)的選擇器
- jQuery treeview樹形結(jié)構(gòu)應(yīng)用
相關(guān)文章
jquery api參考 visualjquery 中國線路 速度快
jquery api參考 visualjquery 中國線路 速度快...2007-11-11
jQuery實現(xiàn)仿百度帖吧頭部固定導(dǎo)航效果
這篇文章主要介紹了jQuery實現(xiàn)仿百度帖吧頭部固定導(dǎo)航效果,涉及jquery針對頁面高度計算與樣式的動態(tài)添加及刪除技巧,非常簡單實用,需要的朋友可以參考下2015-08-08
分享10篇優(yōu)秀的jQuery幻燈片制作教程及應(yīng)用案例
jQuery 是一個非常優(yōu)秀的 JavaScript 框架,使用簡單靈活,同時還有許多成熟的插件可供選擇,它可以幫助你在項目中加入一些非常好的效果。2011-04-04
jquery數(shù)組之存放checkbox全選值示例代碼
使用jquery數(shù)組可以存放checkbox全選值,下面有個不錯的示例,感興趣的朋友可以參考下2013-12-12

