当前位置: 首页 >前端技术 > bootstrap的treeview使用方法

bootstrap的treeview使用方法

首先引入文件:

<link href="./css/bootstrap.css" rel="stylesheet"><script src="./js/jquery.js"></script><script src="./js/bootstrap-treeview.js"></script>  

HTML 结构:

<div id="tree"></div> //不一定是div元素  其他也可以。在这我用的div演示 
调用插件Treeview方法:

<script>
 $(function () {
$('#tree').treeview({
color: "#428bca",
data: getDept(), //Treeview的数据源 返回json
levels: 4,
onNodeSelected: function (e, m) { //Treeview 被选中事件
var id=m.tags[0]; 
var remark=m.text; 
},
onNodeUnselected: function (e, m) { //Treeview 取消选中事件
}
})
});

</script>

//Treeview数据源方法
function getDept() {

var exc = "";
$.ajax({
type: "post",
url: "@Url.Action("getList", "Home")",
async: false,
datatype: "json",
success: function (data) {
if (!data.result) {

 alert("出现异常");
retu;
}
exc = data.data;
}
});
retu exc;
}

 

Controllers层:

  public JsonResult getList(){////查询列表  IList<TreeView> List = TreeViewService.GetDeptList(-1);  //最高部门的父id是-1var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; //忽略空值string json = JsonConvert.SerializeObject(List, Formatting.Indented,jSetting);retu Json(new { result = true, data = json });}

Dal层:

 public static IList<TreeView> GetDeptList(int SuperiorID = 0){DataTable dt = SqlHelper.FillSqlDataTable("SELECT DeptName AS text,CONVERT(VARCHAR(10),ID)+','+DeptCode+','+ISNULL(Remark,'') AS tags,'' AS href FROM  dbo.Sys_Dept WHERE State=0 and  SuperiorID=" + SuperiorID);retu GetListByTable(dt);}
//将Datable转成list
private static IList<TreeView> GetListByTable(DataTable dt, int kind = 0){IList<TreeView> list = new List<TreeView>();if (dt != null && dt.Rows.Count > 0){foreach (DataRow row in dt.Rows){TreeView model = new TreeView();if (row["tags"] != null){model.tags = row["tags"].ToString().Split(',');}if (row["text"] != null){model.text = row["text"].ToString();}if (dt.Columns.Contains("href") && row["href"].ToString().Trim() != null){model.href = row["href"].ToString();}if (kind == 0){//部门的Treeviewmodel.nodes = GetDeptList(int.Parse(model.tags[0]));}if (kind == 1){//菜单的Treeviewmodel.nodes = GetMenusList(int.Parse(model.tags[0]));}list.Add(model);}retu list;}retu null;}

Model 层:

 
//bootstrap的treeview插件返回数据源json格式必须是下面这样的,所以在写sql语句时就要用 as 重命名字段,往上翻看我的sql语句就会明白。
public class TreeView{public string icon { get; set; }public string text { get; set; }public string[] tags { get; set; }public string href { get; set; }public IList<TreeView> nodes { get; set; }}

 

作者:.NET_海
来源链接:https://www.cnblogs.com/bin521/p/8403588.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/front/118528.html

标签:Bootstrap
分享给朋友:

“bootstrap的treeview使用方法” 的相关文章

Markdown中使用html标签 2022年05月17日 21:01:32
HTML5 速查列表 2022年06月04日 23:21:45
HTML 5 video 视频标签全属性详解 2022年06月06日 11:59:42
HTML 标签属性列表 2022年06月10日 23:21:55
标签 align 属性 2022年06月11日 15:05:54
html5 crossorigin属性 2022年06月12日 19:49:13
Input标签type属性 2022年06月13日 15:06:04