这个问题还是挺经典的,后台只是负责查出所有的数据,前台js来处理数据展示给treeview;
show you the code below:
<script>
$(function () {
getData(); })
var displaySeach = function(){
if($("#search-page .x_content").is(":hidden"))
$('#search-page .x_content').slideDown(300);
else
$('#search-page .x_content').slideUp(300);
}
function getTree() {
//节点上的数据遵循如下的格式:
var tree = [{
text: "场地列表", //节点显示的文本值 string
icon: "glyphicon glyphicon-play-circle", //节点上显示的图标,支持bootstrap的图标 string
selectedIcon: "glyphicon glyphicon-ok", //节点被选中时显示的图标 string
color: "#ff0000", //节点的前景色 string
backColor: "#1606ec", //节点的背景色 string
selectable: true, //标记节点是否可以选择。false表示节点应该作为扩展标题,不会触发选择事件。 string
state: { //描述节点的初始状态 Object
checked: true, //是否选中节点
/*disabled: true,*/ //是否禁用节点
expanded: true, //是否展开节点
selected: true //是否选中节点
}
}]
return tree;
} function getData() {
$.ajax({
type: "GET",
url: "/serverPoint/index",
success: function (data) {
console.log("data", data);
for (var k = 0; k < data.length; k++) {
data[k]['text'] = data[k]['Name'];
}
var tree = [{
text: "场地列表", //节点显示的文本值 string
icon: "glyphicon glyphicon-play-circle", //节点上显示的图标,支持bootstrap的图标 string
selectedIcon: "glyphicon glyphicon-ok", //节点被选中时显示的图标 string
color: "#ff0000", //节点的前景色 string
backColor: "#1606ec", //节点的背景色 string
selectable: true, //标记节点是否可以选择。false表示节点应该作为扩展标题,不会触发选择事件。 string
state: { //描述节点的初始状态 Object
checked: true, //是否选中节点
/*disabled: true,*/ //是否禁用节点
expanded: true, //是否展开节点
selected: true //是否选中节点
},
nodes: toTree(data)
}] $('#tree').treeview({
data: tree,//节点数据
showBorder: true, //是否在节点周围显示边框
showCheckbox: true, //是否在节点上显示复选框
showIcon: true, //是否显示节点图标
highlightSelected: true,
levels: 2,
multiSelect: true, //是否可以同时选择多个节点
showTags: true
});
},
error: function () {
$.pnotify({
title: '失败提醒',
text: '请求服务器失败',
type: 'error',
nonblock: {
nonblock: false
},
});
}
}); } function toTree(data) {
// 删除 所有 children,以防止多次调用
data.forEach(function (item) {
delete item.nodes;
}); // 将数据存储为 以 id 为 KEY 的 map 索引数据列
var map = {};
data.forEach(function (item) {
map[item.ID] = item;
});
// console.log(map);
var val = [];
data.forEach(function (item) {
// 以当前遍历项,的pid,去map对象中找到索引的id
var parent = map[item.ParentID];
// 好绕啊,如果找到索引,那么说明此项不在顶级当中,那么需要把此项添加到,他对应的父级中
if (parent) {
(parent.nodes || (parent.nodes = [])).push(item);
} else {
//如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到 val结果集中,作为顶级
val.push(item);
}
});
return val;
} $('.place-choice').on('change', function () {
var cityNum = $(this).val();
console.log("cityNum:", cityNum);
addPlaceSelect($(this), cityNum);
}); var addPlaceSelect = function (obj, cityNum) {
console.log("addPlaceSelect....................");
obj.nextAll().remove();
$.ajax({
url: '/serverPoint/getChild/' + cityNum,
type: 'get',
data: {},
success: function (data) {
if (data) {
if (data.length > 0) {
console.log('data.length > 0');
var select = $('<select></select>');
select.addClass('select2_single form-control place-choice').css('margin-right', '5px').css('width', '100px').css('float', 'left');
$('.place-choice-td').append(select);
select.on('change', function () {
var cityNum = $(this).val();
addPlaceSelect($(this), cityNum);
});
var str = '<option value="">请选择</option>';
select.append(str);
for (var i = 0; i < data.length; i++) {
var str = '<option value="' + data[i]['ID'] + '">' + data[i]['Name'] + '</option>';
select.append(str);
}
}
} else {
$.pnotify({
title: '失败提醒',
text: '添加分类失败',
type: 'error',
nonblock: {
nonblock: false
},
});
}
},
error: function () {
$.pnotify({
title: '失败提醒',
text: '请求服务器失败',
type: 'error',
nonblock: {
nonblock: false
},
});
}
}); } function addNode(pid=null) {
var parentId='';
if (pid == -1) {
var text = $('#postition-name').val();
console.log('text:',text);
parentId = 0;
if (text == "" || text.length == 0) {
$.pnotify({
title: '温馨提醒',
text: '请先填写名称',
type: 'error',
nonblock: {
nonblock: false
},
});
return;
}
} else {
parentId = $('.place-choice-td select:last-child').val();
console.log(parentId);
var text = $('#sub-postition-name').val();
if (parentId == "" || parentId.length == 0) {
$.pnotify({
title: '温馨提醒',
text: '请先选择场景',
type: 'error',
nonblock: {
nonblock: false
},
});
return;
}
if (text == "" || text.length == 0) {
$.pnotify({
title: '温馨提醒',
text: '请先填写名称',
type: 'error',
nonblock: {
nonblock: false
},
});
return;
}
}
$.ajax({
url: '/serverPoint/add',
type: 'post',
data: {
'parentId': parentId,
'name': text
},
success: function (data) {
$.pnotify({
title: '成功提醒',
text: '添加成功',
type: 'success',
nonblock: {
nonblock: false
},
});
getData();
},
error: function () {
$.pnotify({
title: '失败提醒',
text: '请求服务器失败',
type: 'error',
nonblock: {
nonblock: false
},
});
}
}); }
/* function getTree() {
//节点上的数据遵循如下的格式:
var tree = [{
text: "Node 1", //节点显示的文本值 string
icon: "glyphicon glyphicon-play-circle", //节点上显示的图标,支持bootstrap的图标 string
selectedIcon: "glyphicon glyphicon-ok", //节点被选中时显示的图标 string
color: "#ff0000", //节点的前景色 string
backColor: "#1606ec", //节点的背景色 string
href: "#http://www.baidu.com", //节点上的超链接
selectable: true, //标记节点是否可以选择。false表示节点应该作为扩展标题,不会触发选择事件。 string
state: { //描述节点的初始状态 Object
checked: true, //是否选中节点
/!*disabled: true,*!/ //是否禁用节点
expanded: true, //是否展开节点
selected: true //是否选中节点
},
//tags: ['标签信息1', '标签信息2'], //向节点的右侧添加附加信息(类似与boostrap的徽章) Array of Strings
nodes: getData()
}]
return tree;
}*/
/*function getData(){
$.ajax({
type: "GET",
url: "/serverPoint/index",
success:function(data){
console.log(JSON.stringify(data));
return JSON.stringify(data);
},
error:function() {
}
}); }*/
</script>

bootstrap treeview 树形数据生成的更多相关文章

  1. 基于Bootstrap的JQuery TreeView树形控件,数据支持json字符串、list集合(MVC5)<二>

    上篇博客给大家介绍了基于Bootstrap的JQuery TreeView树形控件,数据支持json字符串.list集合(MVC5)<一>, 其中的两种方式都显得有些冗余.接着上篇博客继续 ...

  2. TreeView树形控件递归绑定数据库里的数据

    TreeView树形控件递归绑定数据库里的数据. 第一种:性能不好 第一步:数据库中查出来的表,字段名分别为UNAME(显示名称),DID(关联数据),UTYPE(类型) 第二步:前台代码 <% ...

  3. SharePoint2010沙盒解决方案基础开发——关于TreeView树形控件读取列表数据(树形导航)的webpart开发及问题

    转:http://blog.csdn.net/miragesky2049/article/details/7204882 SharePoint2010沙盒解决方案基础开发--关于TreeView树形控 ...

  4. Bootstrap treeview增加或者删除节点

    参考(AddNode: http://blog.csdn.net/qq_25628235/article/details/51719917,deleteNode:http://blog.csdn.ne ...

  5. 初始化bootstrap treeview树节点

    最近在做启明星图库时,使用了Jquery Bootstrap  Treeview插件.但是,遇到了一个初始化的问题.先看效果如下: 当用户打开图库时,左边分类第一个类别是“所有分类”,默认需要选中. ...

  6. Web中树形数据(层级关系数据)的实现—以行政区树为例

    在Web开发中常常遇到树形数据的操作,如菜单.组织机构.行政区(省.市.县)等具有层级关系的数据. 以下以行政区为例说明树形数据(层级关系数据)的存储以及实现,效果如图所看到的. 1 数据库表结构设计 ...

  7. 大数据技术之_25_手机APP信息统计系统项目_01_APP 数据生成模块 + 数据收集模块 + 数据处理模块框架搭建 + 业务需求处理 + 数据展示模块 +项目总结 + 问题总结

    一 项目概述1.1 角色1.2 业务术语1.3 项目效果展示二 项目需求三 项目概要3.1 项目技术架构3.2 项目目录结构3.3 项目技术选型3.4 项目整体集群规划3.5 创建项目工程四 APP ...

  8. html 报告页面 v1.2 批量数据生成表格

    html 报告页面 v1.2 批量数据生成表格 上代码: <!DOCTYPE html> <html lang="en"> <head> < ...

  9. Asp.Net Mvc自定义控件之树形结构数据生成表格 - WPF特工队内部资料

    最近项目中有一个需求,将树形结构的数据,以表格的形式展示在页面中,下图是最终呈现效果: 源码: @{ Layout = null; } <!DOCTYPE html> <html&g ...

随机推荐

  1. when to use reinterpret_cast

    写的太好了.. When you convert for example int(12) to unsigned float (12.0f) your processor needs to invok ...

  2. JavaScript代理模式

    代理模式的定义,代理是一个对象(proxy)用它来控制目标对象的访问.为此他要是先与目标对象相同的接口,但是他不同于装饰者模式,它对目标对象不进行任何修改,它的目的在于延缓"复杂" ...

  3. LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  4. MySQL一千行笔记

    /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */ mysq ...

  5. kettle删除移动文件

  6. 【转】【JAVA资料免费下载】158个JAVA免豆精品资料汇总——下载目录

    附件完整版下载地址: http://down.51cto.com/data/431561 附件部分预览~ java中国移动收费系统[源代码] http://down.51cto.com/data/70 ...

  7. jsonp现实跨域Ajax CORS

    浏览器有一个很重要的概念——同源策略(Same-Origin Policy).所谓同源是指,域名,协议,端口相同.不同源的客户端脚本(javascript.ActionScript)在没明确授权的情况 ...

  8. MySQL sql_mode 说明(及处理一起 sql_mode 引发的问题)(转)

    1. MySQL莫名变成了 Strict SQL Mode 最近测试组那边反应数据库部分写入失败,app层提示是插入成功,但表里面里面没有产生数据,而两个写入操作的另外一个表有数据.因为 insert ...

  9. Django10-Form组件

    一.Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来.与此同时我们在很多场景下都需要对用户的输入做校验,比如校验用户是 ...

  10. 004之MFCSocket同步编程(指针机制)

    异步与同步通信相比较,前者是非阻塞模式,后者是阻塞模式.有关两者差异在此博主中有详细讲解,推荐:https://www.cnblogs.com/wzsblogs/p/4671559.html. 采用同 ...