[原] JsTree.js
写自用软件系统时查找到的树列表控件过于庸余,样式难调,故自写一套完整的简易js_TreeTable控件,使用时简单的添加自定义的样式效果即可,特此发布第一个版本。
源码如下:
/*
* HuashanlinJsTree 1.0.1
*
* Copyright (c) 2015 Huashanlin (huashanlin.cnblogs.com)
*
* GPL (GPL-LICENSE.txt) licenses.
*
* ¥Date: 2015-08-28 15:14:29 -0400 (Fri, 28 Aug 2015)¥
*
* 本TreeTable控件基于jQuery1.4.2以上版本,兼容IE10.0.18、Chrome41.0.2272.118
*
* 转载请注明出处
*/
var HuashanlinJsTree = {
ConfigParame: {
LocationDivID: "",/*Tree生成页面位置的Div的ID【必填】*/
KeyFieldName: "",/*树节点值的字段名称(主键,与ParentKeyFieldName数据对应)【必填】*/
ValueFieldName: "",/*树节点名称的字段【必填】*/
ParentKeyFieldName: "",/*父节点值字段名称【必填】*/
ExtendValueFieldName: "",/*除树节点名称字段外扩展显示的字段,即可显示多个字段,格式如"Cu1,Cu2,Re1"【可选】*/
OnSelectFuncionSignature: "",/*选中树节点事件【可选】*/
TreeTableTitle: "",/*树表格的标题,为空则不显示标题【可选】*/
SpaceMark: "          ",/*区分不同层级的树节点的间隔符【必填】*/
TagMark: "|--",/*树表格节点头部字符或图片<img src=''>【可选】*/
IsFirstLevelHaveTagMark: false,/*树第一级节点名称是否显示TagMark*/
},
DataSource: [],/*树的数据源 Json格式*/
SelectedKeyFieldValue: "",/*选中行节点的值*/
_GetHtml: "",/*自用参数*/
_ID: "",/*自用参数*/
DataBind: function () {
document.write
if (this.ConfigParame.LocationDivID.length == 0) { alert("LocationDivID属性不能为空!表示树所在的Div的位置!"); return; }
if (this.ConfigParame.KeyFieldName.length == 0) { alert("KeyFieldName属性不能为空!表示树的节点值所用的字段名!"); return; }
if (this.ConfigParame.ValueFieldName.length == 0) { alert("ValueFieldName属性不能为空!表述树的节点名称所用的字段名!"); return; }
if (this.ConfigParame.ParentKeyFieldName.length == 0) { alert("ParentKeyFieldName属性不能为空!表示树的父节点值所用的字段名!"); return; }
var _date = new Date();
var _HeadChar = "";
this._ID = "HuashanlinJsTree" + String(_date.getFullYear()) + String(_date.getMonth()) + String(_date.getDate())
+ String(_date.getHours()) + String(_date.getMinutes()) + String(_date.getSeconds());
this._GetHtml += "<table id=\"" + this._ID + "\" class=\"HLJsTree\">";
var FiledNum = 1;
var ExtendArray = new Array();
if (this.ConfigParame.ExtendValueFieldName.length > 0) {
ExtendArray = this.ConfigParame.ExtendValueFieldName.split(',');
FiledNum = FiledNum + ExtendArray.length;
}
if (this.ConfigParame.TreeTableTitle.length > 0) {
this._GetHtml += "<tr class=\"HLJsTreeTableTitle\"><td colspan=\"" + FiledNum + "\">" + this.ConfigParame.TreeTableTitle + "</td></tr>";
}
for (var i = 0; i < this.DataSource.length; i++) {
if (this.DataSource[i].ParentID == 0 || this.DataSource[i].ParentID == null) {
if (this.ConfigParame.IsFirstLevelHaveTagMark) {
_HeadChar = this.ConfigParame.TagMark;
}
var ExtendField = "";
for (var m = 0; m < ExtendArray.length; m++) {
ExtendField += "<td class=\"HLJsTreeExtendField\">" + eval("this.DataSource[i]." + ExtendArray[m]) + "</td>";
}
this._GetHtml += "<tr onclick=\"return HuashanlinJsTree._SV(this); \" "+
"data-key='" + eval("this.DataSource[i]." + this.ConfigParame.KeyFieldName) + "'>" +
"<td>" + _HeadChar + eval("this.DataSource[i]." + this.ConfigParame.ValueFieldName) + "</td>" + ExtendField + "</tr>"; this._RCS(eval("this.DataSource[i]." + this.ConfigParame.KeyFieldName), "");
}
}
this._GetHtml += "</table>";
$("#" + this.ConfigParame.LocationDivID).html(this._GetHtml);
},
_RCS: function (KeyFieldValue, HeadChar) {
var IsHaveChild = false;
var TempHeadChar = "";
var ExtendArray = new Array();
if (this.ConfigParame.ExtendValueFieldName.length > 0) {
ExtendArray = this.ConfigParame.ExtendValueFieldName.split(',');
}
for (var i = 0; i < this.DataSource.length; i++) {
if (eval("this.DataSource[i]." + this.ConfigParame.ParentKeyFieldName) == KeyFieldValue) {
if (!IsHaveChild) {
IsHaveChild = true;
TempHeadChar = HeadChar + this.ConfigParame.SpaceMark + this.ConfigParame.TagMark;
}
var ExtendField = "";
for (var m = 0; m < ExtendArray.length; m++) {
ExtendField += "<td class=\"HLJsTreeExtendField\">" + eval("this.DataSource[i]." + ExtendArray[m]) + "</td>";
}
this._GetHtml += "<tr onclick=\"return HuashanlinJsTree._SV(this); \" " +
"data-key='" + eval("this.DataSource[i]." + this.ConfigParame.KeyFieldName) + "' >" +
"<td>" + TempHeadChar + eval("this.DataSource[i]." + this.ConfigParame.ValueFieldName) + "</td>" + ExtendField + "</tr>"; this._RCS(eval("this.DataSource[i]." + this.ConfigParame.KeyFieldName), HeadChar + this.ConfigParame.SpaceMark);
}
}
},
IsHaveChild: function (CurentKeyValue) {
var IsHave = false;
for (var i = 0; i < this.DataSource.length; i++) {
if (eval("this.DataSource[i]." + this.ParentKeyName) == CurentKeyValue) {
IsHave = true;
break;
}
}
return IsHave;
},
_SV: function (arg) {
this.SelectedKeyFieldValue = $(arg).attr("data-key"); $("#" + this._ID).find("tr").each(function () {
$(this).removeClass("HLJsTreeRowSelected");
});
$(arg).addClass("HLJsTreeRowSelected"); if (this.ConfigParame.OnSelectFuncionSignature != null) {
if ((typeof (this.ConfigParame.OnSelectFuncionSignature) == "function")) {
this.ConfigParame.OnSelectFuncionSignature();
}
};
},
GetSelectedKeyFieldValue: function () {
return this.SelectedKeyFieldValue;
},
GetSelected: function () {
for (var i = 0; i < this.DataSource.length; i++) {
if (eval("this.DataSource[i]." + this.ConfigParame.KeyFieldName) == this.SelectedKeyFieldValue)
return this.DataSource[i];
}
}
};
.HLJsTree {
width: 100%;
table-layout:fixed;
font-family:Verdana;
font-size:12px;
}
.HLJsTree tr {
cursor: pointer;
}
.HLJsTree tr:hover {
background-color: lightgray;/*鼠标滑过背景色,自定义*/
}
.HLJsTree td {
border-bottom: 1px solid;
width:200px;
}
.HLJsTreeRowSelected {
background-color: lightgray;/*选中行背景色,自定义*/
}
.HLJsTreeExtendField {
border-left: 1px solid;
}
.HLJsTreeTableTitle {
background-color:#0528fa;
font-size:14px;
font-weight:bold;
color:white;
padding-left:10px;
}
以上为源码和样式,调用示例如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>HuashanlinJsTree树控件使用Demo</title>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.4.2/jquery.min.js"></script>
<link href="HuashanlinJsTree/HuashanlinJsTree.css" rel="stylesheet" />
<script src="HuashanlinJsTree/HuashanlinJsTree.js"></script>
<script type="text/javascript">
//用法示例
jQuery(document).ready(function () { //可从数据库读取到的数据
var GetJsonDataFromDB = [
{ "SortID": 1, "SortName": "新闻", "ParentID": null, "Remark": "这是新闻频道", "CreateDate": "2015-08-21" },
{ "SortID": 2, "SortName": "视频", "ParentID": 0, "Remark": "这是视频频道", "CreateDate": "2015-08-22" },
{ "SortID": 3, "SortName": "国内新闻", "ParentID": 1, "Remark": "这是国内新闻频道", "CreateDate": "2015-08-23" },
{ "SortID": 4, "SortName": "国际新闻", "ParentID": 1, "Remark": "这是国际新闻频道", "CreateDate": "2015-08-24" },
{ "SortID": 5, "SortName": "直播", "ParentID": 2, "Remark": "这是直播频道", "CreateDate": "2015-08-25" },
{ "SortID": 6, "SortName": "社会新闻", "ParentID": 3, "Remark": "这是社会新闻频道", "CreateDate": "2015-08-26" },
{ "SortID": 7, "SortName": "奥运2016", "ParentID": 5, "Remark": "这是奥运2016频道", "CreateDate": "2015-08-27" },
{ "SortID": 8, "SortName": "田径", "ParentID": 7, "Remark": "这是田径频道", "CreateDate": "2015-08-28" },
{ "SortID": 9, "SortName": "游泳", "ParentID": 7, "Remark": "这是游泳频道", "CreateDate": "2015-08-29" },
{ "SortID": 10, "SortName": "自行车公路赛", "ParentID": 7, "Remark": "这是自行车公路赛频道", "CreateDate": "2015-08-30" },
{ "SortID": 11, "SortName": "体育新闻", "ParentID": 3, "Remark": "这是体育新闻频道", "CreateDate": "2015-08-31" },
]; HuashanlinJsTree.ConfigParame.LocationDivID = "newTree";
HuashanlinJsTree.ConfigParame.KeyFieldName = "SortID";
HuashanlinJsTree.ConfigParame.ValueFieldName = "SortName";
HuashanlinJsTree.ConfigParame.ParentKeyFieldName = "ParentID";
HuashanlinJsTree.ConfigParame.TreeTableTitle = "网站目录";
HuashanlinJsTree.ConfigParame.OnSelectFuncionSignature = function () {
var CurrentRowData = HuashanlinJsTree.GetSelected();
if (CurrentRowData != null) {
alert(CurrentRowData.Remark);
}
};
HuashanlinJsTree.ConfigParame.ExtendValueFieldName = "Remark,CreateDate";
HuashanlinJsTree.DataSource = GetJsonDataFromDB;
HuashanlinJsTree.DataBind(); });
</script>
</head>
<body>
<div id="newTree" ></div>
</body>
</html>
效果如下:
[原] JsTree.js的更多相关文章
- AngularJS2+调用原有的js脚本(AngularJS脚本跟本地原有脚本之间的关系)
昨天一个话题说关于AngularJS2以后版本的两个小技巧,不料引出了另外一个话题,话题起始很简单: "很多的前端框架并不复杂,比如JQuery,引入即用,实时看到效果,多好.到了Angul ...
- 深入浏览器工作原理和JS引擎(V8引擎为例)
浏览器工作原理和JS引擎 1.浏览器工作原理 在浏览器中输入查找内容,浏览器是怎样将页面加载出来的?以及JavaScript代码在浏览器中是如何被执行的? 大概流程可观察以下图: 首先,用户在浏览器搜 ...
- 【原】js实现复制到剪贴板功能,兼容所有浏览器
两天前听了一个H5的分享,会议上有一句话,非常有感触:不是你不能,而是你对自己的要求太低.很简单的一句话,相信很多事情不是大家做不到,真的是对自己的要求太低,如果对自己要求多一点,那么你取得的进步可能 ...
- 【原】js检测移动端横竖屏
摘要:上周做了一个小项目,但是要放到我们的app上,然而需要横竖屏使用不同的样式.横屏一套,竖屏一套.调用了手机APP那里的api,可是他们那里ios和安卓返回的不一样. 各种头疼.于是用了css3的 ...
- 【原】js 签到用日历
最近做的一个项目中,需要用到一个日历来记录你的签到,网上找了一些,感觉挺庞大的,所以就自己写了一个,记录一下自己写这个日历的经过 html代码: <table cellspacing=" ...
- 【原】js获取height为auto的高度问题
今天用react写一个页面,需要获取一个img高度设置为auto的高度,可是一直获取到的要么是0,要么是null,因为页面加载完了图片不一定加载完. 当我把高度由 auto 设置为固定值之后,又可以获 ...
- 【原】JS原型的动态性及实例与原型的关系
今天再读了<JS高程>的第六章,有了些深入的感悟和理解,总结分享一下. 创建对象的方式有很多,有一种是动态原型模式,最实用的是构造函数与原型组合的模式,原型的动态性在这两个模式里都有所体现 ...
- 【原】JS正则表达式里的控制符
正则表达式易于使用而又让人费解,乍一看上去,就像是一行行的乱码,但是它的功能确实又不容小觑.今天整理正则时,纠正了自己的一个误解. 先缕一缕: 正则表达式的两种声明方式: 字面量.构造器 (RegEx ...
- 拖拽碰撞--原声js(自身理解上新的方法)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
随机推荐
- codeforces 429E
题意:给定n<=100000线段[l,r],然后给这些线段染色(red or blue),求最后平面上任意一个点被蓝色及红色覆盖次数只差的绝对值不大于1 思路:把每条线段拆成2个点[l<& ...
- mteclipse中运行的分页,搜索,列表批量删除的界面,它的源代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- elixir 高可用系列(四) Task
概述 之前学习的 Agent,GenSever以及GenEvent,都是用来管理状态或者处理消息的. 但是在很多时候,我们需要的是执行某个任务,这时如果使用 GenSever 或者 GenEvent, ...
- 基于Qt的流程设计器(一)
一: 先来看一下界面的截图: 说明: 拖动节点的时候,与该节点相关的箭头连线也会跟着调整: 用户可以使用鼠标从一个节点拖出一个箭头到另一个节点(鼠标在空白区域点击一下,拖出的箭头消失) 这三个 ...
- windows下使用批处理脚本实现多个版本的JDK切换
一.JDK版本切换批处理脚本 我们平时在window上做开发的时候,可能需要同时开发两个甚至多个项目,有时不同的项目对JDK的版本要求有区别,这时候我们可能会在一台电脑上安装多个版本的JDK,如下图所 ...
- Mac OS X 安装ruby环境
1.查看版本 $ ruby -v ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14] 2.查看源 $ gem ...
- js获取url传递参数
<head> <meta charset="UTF-8"> <title></title> <script type=&quo ...
- MATLAB实现将图像转换为素描(简笔画)风格
代码: colorgrad.m function [VG, A, PPG] = colorgrad(f, T) ) || (size(f,)~=) error('Input image must be ...
- 使用Python的yield实现流计算模式
首先先提一下上一篇<如何猜出Y combinator>中用的方法太复杂了.其实在Lambda演算中实现递归的思想很简单,就是函数把自己作为第一个参数传入函数,然后后面就是简单的Lambda ...
- WindowsPhone App如何扩展能够使用的内存
目前手机系统中对App的内存使用都是有限制的,尤其是对于Android和WindowsPhone这样的平台,因为机型很多,配置高低不同因此对于同一个App在不同的手机上运行的效果也不同. WP上通常对 ...