bootstrap treeview 树形数据生成
这个问题还是挺经典的,后台只是负责查出所有的数据,前台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 树形数据生成的更多相关文章
- 基于Bootstrap的JQuery TreeView树形控件,数据支持json字符串、list集合(MVC5)<二>
上篇博客给大家介绍了基于Bootstrap的JQuery TreeView树形控件,数据支持json字符串.list集合(MVC5)<一>, 其中的两种方式都显得有些冗余.接着上篇博客继续 ...
- TreeView树形控件递归绑定数据库里的数据
TreeView树形控件递归绑定数据库里的数据. 第一种:性能不好 第一步:数据库中查出来的表,字段名分别为UNAME(显示名称),DID(关联数据),UTYPE(类型) 第二步:前台代码 <% ...
- SharePoint2010沙盒解决方案基础开发——关于TreeView树形控件读取列表数据(树形导航)的webpart开发及问题
转:http://blog.csdn.net/miragesky2049/article/details/7204882 SharePoint2010沙盒解决方案基础开发--关于TreeView树形控 ...
- Bootstrap treeview增加或者删除节点
参考(AddNode: http://blog.csdn.net/qq_25628235/article/details/51719917,deleteNode:http://blog.csdn.ne ...
- 初始化bootstrap treeview树节点
最近在做启明星图库时,使用了Jquery Bootstrap Treeview插件.但是,遇到了一个初始化的问题.先看效果如下: 当用户打开图库时,左边分类第一个类别是“所有分类”,默认需要选中. ...
- Web中树形数据(层级关系数据)的实现—以行政区树为例
在Web开发中常常遇到树形数据的操作,如菜单.组织机构.行政区(省.市.县)等具有层级关系的数据. 以下以行政区为例说明树形数据(层级关系数据)的存储以及实现,效果如图所看到的. 1 数据库表结构设计 ...
- 大数据技术之_25_手机APP信息统计系统项目_01_APP 数据生成模块 + 数据收集模块 + 数据处理模块框架搭建 + 业务需求处理 + 数据展示模块 +项目总结 + 问题总结
一 项目概述1.1 角色1.2 业务术语1.3 项目效果展示二 项目需求三 项目概要3.1 项目技术架构3.2 项目目录结构3.3 项目技术选型3.4 项目整体集群规划3.5 创建项目工程四 APP ...
- html 报告页面 v1.2 批量数据生成表格
html 报告页面 v1.2 批量数据生成表格 上代码: <!DOCTYPE html> <html lang="en"> <head> < ...
- Asp.Net Mvc自定义控件之树形结构数据生成表格 - WPF特工队内部资料
最近项目中有一个需求,将树形结构的数据,以表格的形式展示在页面中,下图是最终呈现效果: 源码: @{ Layout = null; } <!DOCTYPE html> <html&g ...
随机推荐
- python 二叉树实现
二叉树实现思想 1.把每个节点都看作是一个对象包含以下特征: 节点的当前值 节点的左孩子(存储比当前节点值小的节点对象) 节点右孩子(存储比当前节点值大的节点对象) 2.二叉树就是以根节点开始的连续的 ...
- 前端人员使用notepad++基本配置
截图说明: 以上截图说的很明确了,基本满足基本的开发. notepad++前端配置版支持Emmet快速完成:支持Explorer目录管理:支持cmd控制台命令:支持MarkDown编辑预览:其他想用的 ...
- 【学习】如何安装GraphLab Create 【转载】
前人走过的路不用再走,慢慢加上一些工具的链接 GraphLab Create安装,链接地址: http://www.imooc.com/article/18094?block_id=tuijian_w ...
- !!常用HTML代码
META: <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> “IE=e ...
- tornado框架设置
路由 import tornado.ioloop #开启循环 让服务器一直等待请求的到来 import tornado.web #框架基本功能封装在此模块 #例子 class MainHendler( ...
- @Data的注解使用以及在IDEA上安装
平时在开发过程中创建实体类的时候就经常的操作是:先写成员变量,然后再提供getXxx().setXxx()方法,然后看需要再提供toString等方法.这样一来不仅会发现每写一个实体类的话就会有很多相 ...
- 自定义编写js格式化数字的函数
在处理网页的时候,有时候会需要显示很长的数字,但是当数字的长度比较长的时候,就很难看一个数字到底是多大.这种情况下,一些网站在处理数字的时候,当数字的长度大于3个时,就用逗号把他们分开,这是一个比较常 ...
- F查询和Q查询,事务及其他
F查询和Q查询 F查询 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个我们自己设定的常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢? Django 提供 F() 来做这样的比较.F ...
- 初识Scratch 3.0
之前在帮朋友搜集少儿编程教育资料的时候,发现了麻省理工开发的积木式编程语言的Scratch,最近有空玩了下,感觉很惊艳,我能想象用它做一些有趣的事情,Scratch把编程元素变成像乐高积木一样,可以通 ...
- C语言顺序栈
10进制转任何进制 #include<stdio.h> #include<stdlib.h>#define maxSize 30typedef int DataType;typ ...