推薦個不錯的表單Input的高級用法11例
536
2024-10-20
本文支持兩種方式的數據,一種為List集合,一種為json字符串。
先來介紹一下后臺返回list集合(推薦使用此方法):
控制器代碼如下:
public static List<TC_DictionaryInfo> DInfo = new List<TC_DictionaryInfo>(); /// <summary> /// TreeView視圖 /// </summary> /// <returns></returns> public ActionResult May(string TypeCode,int parentId) { ViewBag.TypeCode = TypeCode; ViewBag.ParentId = parentId; return View(); } [HttpPost] public ActionResult GetTreeData(string TypeCode,int parentId) { List<TC_DictionaryInfo> DInfo = dbll.GetModelList("TypeCode="+TypeCode); return Json(GetChildNodes(0,new NodeModel(){}, DInfo).nodes); } ///<summary> /// GetChildNodes方法,此方法使用遞歸 /// </summary> /// <param name="parentId"></param> /// <returns></returns> public NodeModel GetChildNodes(int parentId,NodeModel childnodestr,List<TC_DictionaryInfo> DInfo) { List<TC_DictionaryInfo> DictionaryList = DInfo.Where(e => Convert.ToInt32(e.ParentId) == parentId).ToList(); for (int i = 0; i < DictionaryList.Count; i++) { NodeModel NewNode = new NodeModel(); NewNode.DicId = DictionaryList[i].DicId; NewNode.text = DictionaryList[i].DICName; NewNode.ParentId = DictionaryList[i].ParentId; childnodestr.nodes.Add(NewNode); GetChildNodes(NewNode.DicId, NewNode, DInfo); } return childnodestr; }
視圖代碼如下:
<script type="text/javascript"> var typecode = @ViewBag.TypeCode; var parentid = @ViewBag.ParentId; $(function() { $.ajax({ type: 'Post', url: '/Type/GetTreeData', data:{ TypeCode:typecode, ParentId:parentid, }, //data: para, dataType: 'json', async: false, success: function (data) { var defaultData = eval(data); //var defaultData = data; $('#treeview4').treeview({ color: "#428bca", data: defaultData }); }, error: function (err) { alert('不好意思,數據忘記帶上了。。。'); } }); </scipt>
第二種方式為后臺返回json字符串這種方式(此方式為后臺拼接json字符串傳給前臺):
不建議大家采用這種方式,比較容易出錯。
public ActionResult May(string TypeCode,int parentId) { ViewBag.TypeCode = TypeCode; ViewBag.ParentId = parentId; return View(); } public ActionResult GetTreeData() { //創建jsondata對象 StringBuilder jsonData = new StringBuilder(); //拼接json字符串 開始{ jsonData.Append("["); //調用GetChildNodes方法,默認傳參試為0(0表示根節點菜單選項) jsonData.Append(GetChildNodes(0)); //閉合Node子類數組 ] jsonData.Append("]"); //返回json字符串 return Json(jsonData.ToString()); } /// <summary> /// GetChildNodes方法,此方法使用遞歸 /// </summary> /// <param name = "parentId" ></ param > /// < returns ></ returns > public string GetChildNodes(int parentId) { //為DInfo賦值(DInfo承載頁面所需的值(間接將值傳給頁面)),查詢所有的數據 List<TC_DictionaryInfo> DInfo = dbll.GetModelList(""); //創建ChiidNodeStr變量 StringBuilder ChildNodeStr = new StringBuilder(); //查詢符合條件的數據(ParentId=0),DictionaryList接收數據 List<TC_DictionaryInfo> DictionaryList = DInfo.Where(e => Convert.ToInt32(e.ParentId) == parentId).ToList(); //循環DictionaryList為TreeView所需數據分層級(即子類、父類等節點分開) for (int i = 0; i < DictionaryList.Count; i++) { //Nodes數組開始 { ChildNodeStr.Append("{"); //實例化NewNode NodeModel NewNode = new NodeModel(); //分別為字段賦值 NewNode.DicId = DictionaryList[i].DicId; NewNode.text = DictionaryList[i].DICName; NewNode.ParentId = DictionaryList[i].ParentId; //將要顯示的字段拼接 ChildNodeStr.Append("text:'" + NewNode.text + "',"); //超鏈接地址(此處設置為空鏈接#) ChildNodeStr.Append("href:'#parent1',"); ChildNodeStr.Append("tags:['0']"); //拼接完畢子類分層,去掉最后多余的符號(,) string ChildNodes = GetChildNodes(NewNode.DicId).Trim(','); //判斷父類下是否有子類,如果有子類放到Nodes里,如果沒有不讓他顯示為數組形式“[]” if (ChildNodes != string.Empty) { //拼接Json字符串格式 ChildNodeStr.Append(","); ChildNodeStr.Append("nodes:["); ChildNodeStr.Append(ChildNodes); ChildNodeStr.Append("]"); } //結尾加上}, ChildNodeStr.Append("},"); } //返回Json字符串,并將,去掉 return ChildNodeStr.ToString().Trim(','); }
前臺代碼和上面基本一致,唯一的差別在于
var defaultData = eval(data);
因為我們后臺是拼接的json字符串的緣故,我們需要將json字符串轉化為json數組(網上下載的基于Bootstrap的JQuery TreeView樹形控件僅僅支持json數組),我也是費了很大的勁才找到的。使用MVC+Bootstrap開發TreeView的同學要注意!!!
下面接著給大家介紹基于MVC5和Bootstrap的jQuery TreeView樹形控件(二)之數據支持json字符串、list集合
以上所述是小編給大家介紹的基于MVC5和Bootstrap的jQuery TreeView樹形控件(一)之數據支持json字符串、list集合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對綠夏網網站的支持!
#免責聲明#
本站[綠夏技術導航]提供的一切軟件、教程和內容信息僅限用于學習和研究目的;不得將上述內容用于商業或者非法用途,否則,一切后果請用戶自負。本站信息來自網絡收集整理,版權爭議與本站無關。您必須在下載后的24個小時之內,從您的電腦或手機中徹底刪除上述內容。如果您喜歡該程序或內容,請支持正版,購買注冊,得到更好的正版服務。我們非常重視版權問題,如有侵權請郵件[admin@lxwl520.com]與我們聯系進行刪除處理。敬請諒解!