一個很簡單的jquery+xml+ajax的無重新整理樹結構(無css,後台是c#)

來源:互聯網
上載者:User

複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
using System.Xml;
using System.Xml.Linq;
namespace WebApplication3 {
public partial class WebForm1: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
int id = Convert.ToInt32(Request["parentID"]);
GetXML(id);
}
public IList < Product > GetList() {
return new List < Product > () {
new Product() {
Id = 1,
ParentId = 0,
HasChild = 1,
Name = "aaaaa"
},
new Product() {
Id = 2,
ParentId = 1,
HasChild = 1,
Name = "bbbb1"
},
new Product() {
Id = 3,
ParentId = 2,
HasChild = 0,
Name = "ccccc2"
},
new Product() {
Id = 4,
ParentId = 2,
HasChild = 0,
Name = "ddddd3"
},
new Product() {
Id = 5,
ParentId = 1,
HasChild = 0,
Name = "eeeeee4"
},
new Product() {
Id = 6,
ParentId = 3,
HasChild = 0,
Name = "ffffff5"
},
new Product() {
Id = 7,
ParentId = 4,
HasChild = 0,
Name = "ggggggg6"
},
new Product() {
Id = 8,
ParentId = 7,
HasChild = 0,
Name = "hhhhhhh7"
},
new Product() {
Id = 9,
ParentId = 0,
HasChild = 0,
Name = "jjjjjjj8"
},
new Product() {
Id = 10,
ParentId = 0,
HasChild = 0,
Name = "yyyyyyyy9"
}
};
} /// <summary>
/// 通過父節點讀取子節點並且拼接成xml給前台
/// </summary>
/// <param name="parentId"></param>
public void GetXML(int parentId) {
List<Product> list = GetList().Where(x => x.ParentId == parentId).ToList();
XElement xElement = new XElement("textTree");
foreach (Product p in list) {
xElement.Add(new XElement("value", new XAttribute("id", p.Id),p.Name));
}
xElement.Save("d:\\kissnana.xml");
XmlDocument xdocument = new XmlDocument();
xdocument.Load("d:\\kissnana.xml");
Response.ContentType = "text/xml";
xdocument.Save(Response.OutputStream);
Response.End();
}
}
public class Product {
public int Id{set;get;}
public int ParentId{set;get;}
public int HasChild{set;get;}
public string Name{set;get;}
}}
思路很簡單,後台利用xml送往前台通過jquery接收處理拼接ul,
li原理(利用 < li > 中嵌套 < ul > 的方式,局部讀取一節點下的所有直屬子節點,每次點擊讀取,讀取過的話,則進入GetDisplayOrNot()方法判斷顯示和隱藏節點)html代碼: < body > <form id = "form1"
runat = "server" > <input type = "button"
value = "text"
onclick = "LoadXml(0)" / ><div id = "root" > </div>
</form >
</body>

前台代碼: 複製代碼 代碼如下:<script type="text/javascript">
var object1 = null;
function LoadXml(id) {
$.ajax({
type: "
post ",
url:"
WebForm1.aspx ? parentID = "+id,
dataType:"
xml ",
success: createTree
});
}
// 思路是每個父節點產生一個ul孩子並且ajax讀取該父親的直接兒子,拼接樹
function createTree(xml) {
var obj = object1 == null ? ("#root ") : (object1);//判斷是不是第一次載入,如果第一次載入則是最外的div的id,否則是父節點
$(obj).append(" < UL class = 'ULfather' > ");//添加ul對象
$(xml).find("
value ").each(function() {//從xml裡讀出所有value節點資訊,得到當前層次節點
//拼接<li>和<a>,屬性通過xml中的value節點的屬性id和節點文本進行拼接
$(obj).children(".ULfather ").append(" < li > <a id = " + $(this).attr("
id ") + " > " + $(this).text() + " < /a></li > ");
var alink = "#" + $(this).attr("
id "); //得到當前超連結對象
$(alink).bind("
click ", function() { //超串連綁定點擊事件
if ($(alink + " + ul ").size() <= 0) {//如果資料已經綁定則無需再次綁定,(如果超連結下個元素是ul的話,說明資料已經綁過)
object1 = $(alink).parent("
li ");
LoadXml($(this).attr("
id "))
}
else {
GetDisplayOrNot($(alink));
}
});
});
}
//節點顯示或影藏
function GetDisplayOrNot(obj) {
if ($(obj).parent("
li ").children("
ul ").is(": hidden ")) {
$(obj).parent("
li ").children("
ul ").css("
display ", "
block ");
}
else {
$(obj).parent("
li ").children("
ul ").css("
display ", "
none ");
}
}
</script>

後台: 複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
using System.Xml;
using System.Xml.Linq;
namespace WebApplication3
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int id =Convert.ToInt32(Request["parentID"]);
GetXML(id);
}
public IList<Product> GetList()
{
return new List<Product>()
{
new Product(){ Id=1, ParentId=0, HasChild=1, Name="aaaaa" },
new Product(){ Id=2, ParentId=1, HasChild=1, Name="bbbb1" },
new Product(){ Id=3, ParentId=2, HasChild=0, Name="ccccc2" },
new Product(){ Id=4, ParentId=2, HasChild=0, Name="ddddd3" },
new Product(){ Id=5, ParentId=1, HasChild=0, Name="eeeeee4" },
new Product(){ Id=6, ParentId=3, HasChild=0, Name="ffffff5" },
new Product(){ Id=7, ParentId=4, HasChild=0, Name="ggggggg6" },
new Product(){ Id=8, ParentId=7, HasChild=0, Name="hhhhhhh7" },
new Product(){ Id=9, ParentId=0, HasChild=0, Name="jjjjjjj8" },
new Product(){ Id=10,ParentId=0, HasChild=0, Name="yyyyyyyy9" }
};
}

/// <summary>
/// 通過父節點讀取子節點並且拼接成xml給前台
/// </summary>
/// <param name="parentId"></param>
public void GetXML(int parentId)
{
List<Product> list = GetList().Where(x => x.ParentId == parentId).ToList();
XElement xElement = new XElement("textTree");
foreach (Product p in list)
{
xElement.Add(new XElement("value", new XAttribute("id", p.Id),p.Name));
}
xElement.Save("d:\\kissnana.xml");
XmlDocument xdocument = new XmlDocument();
xdocument.Load("d:\\kissnana.xml");
Response.ContentType = "text/xml";
xdocument.Save(Response.OutputStream);
Response.End();
}
}
public class Product
{
public int Id{set;get;}
public int ParentId{set;get;}
public int HasChild{set;get;}
public string Name{set;get;}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.