这个问题还是挺经典的,后台只是负责查出所有的数据,前台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. 网络通信协议tcp,udp区别

    1 网络通信协议 Tcp udp的区别 重点(*****) Tcp三次握手四次挥手(******) udp客户端多人聊天 import socket udp_client = socket.socke ...

  2. Python笔记:编码问题

    1. python2的编码: python2中使用的是ASCII码,所以不支持中文,如果要在python2中写入中文编码,需要在文件头编写: #-*- encoding:utf-8 -*- 2. 不同 ...

  3. How does the compilation and linking process work?

    The compilation of a C++ program involves three steps: Preprocessing: the preprocessor takes a C++ s ...

  4. mybatis 映射生成mapper和pojo ---逆向工程的使用过程

    使用逆向工程生成mapper和pojo 2. 新建一个项目,随便叫什么 3.导入mybatis-generator-core .mybatis.mybatis-spring.log4j等jar 4.在 ...

  5. JVM运行、类加载的全过程

    类加载机制:JVM把CLASS文件加载到内存,并对数据进行校验.解析和初始化,最终形成JVM可以直接使用的Java文件. 加载:把class文件字节码加载到内存中,并且将这些静态数据转换成方法区中的运 ...

  6. spring boot项目配置跨域

    1.在项目启动入口类实现 WebMvcConfigurer 接口: @SpringBootApplication public class Application implements WebMvcC ...

  7. 应用MySQL(开启远程登录权限)

    参考“开启MySQL远程访问权限 允许远程连接”.

  8. PHP PDF文件上传

    /** * 上传PDF文件 */ function UploadPdf(){ if(is_array($_FILES)){ $tmp_file = $_FILES['pdf'] ['tmp_name' ...

  9. 使用 webpack 搭建多入口项目

    闲来无事,学习一下怎么用 webpack 自定义多入口项目的打包 项目github地址:https://github.com/xiaoliwang2016/webpack-demo 先来看一下目录结构 ...

  10. pyqt5 -——菜单和工具栏

    一. 状态栏 # -*- coding: utf-8 -*-# @Time : 2018/12/22 12:37# @Author : Bo# @Email : mat_wu@163.com# @Fi ...