写自用软件系统时查找到的树列表控件过于庸余,样式难调,故自写一套完整的简易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: "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp",/*区分不同层级的树节点的间隔符【必填】*/
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的更多相关文章

  1. AngularJS2+调用原有的js脚本(AngularJS脚本跟本地原有脚本之间的关系)

    昨天一个话题说关于AngularJS2以后版本的两个小技巧,不料引出了另外一个话题,话题起始很简单: "很多的前端框架并不复杂,比如JQuery,引入即用,实时看到效果,多好.到了Angul ...

  2. 深入浏览器工作原理和JS引擎(V8引擎为例)

    浏览器工作原理和JS引擎 1.浏览器工作原理 在浏览器中输入查找内容,浏览器是怎样将页面加载出来的?以及JavaScript代码在浏览器中是如何被执行的? 大概流程可观察以下图: 首先,用户在浏览器搜 ...

  3. 【原】js实现复制到剪贴板功能,兼容所有浏览器

    两天前听了一个H5的分享,会议上有一句话,非常有感触:不是你不能,而是你对自己的要求太低.很简单的一句话,相信很多事情不是大家做不到,真的是对自己的要求太低,如果对自己要求多一点,那么你取得的进步可能 ...

  4. 【原】js检测移动端横竖屏

    摘要:上周做了一个小项目,但是要放到我们的app上,然而需要横竖屏使用不同的样式.横屏一套,竖屏一套.调用了手机APP那里的api,可是他们那里ios和安卓返回的不一样. 各种头疼.于是用了css3的 ...

  5. 【原】js 签到用日历

    最近做的一个项目中,需要用到一个日历来记录你的签到,网上找了一些,感觉挺庞大的,所以就自己写了一个,记录一下自己写这个日历的经过 html代码: <table cellspacing=" ...

  6. 【原】js获取height为auto的高度问题

    今天用react写一个页面,需要获取一个img高度设置为auto的高度,可是一直获取到的要么是0,要么是null,因为页面加载完了图片不一定加载完. 当我把高度由 auto 设置为固定值之后,又可以获 ...

  7. 【原】JS原型的动态性及实例与原型的关系

    今天再读了<JS高程>的第六章,有了些深入的感悟和理解,总结分享一下. 创建对象的方式有很多,有一种是动态原型模式,最实用的是构造函数与原型组合的模式,原型的动态性在这两个模式里都有所体现 ...

  8. 【原】JS正则表达式里的控制符

    正则表达式易于使用而又让人费解,乍一看上去,就像是一行行的乱码,但是它的功能确实又不容小觑.今天整理正则时,纠正了自己的一个误解. 先缕一缕: 正则表达式的两种声明方式: 字面量.构造器 (RegEx ...

  9. 拖拽碰撞--原声js(自身理解上新的方法)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

随机推荐

  1. opengl的mipmap

    压缩纹理是不能调用glGenerateMipmap生成mipmap的. DDS和PVR都不行. 强行调用会产生GL_INVALID_OPERATION的错误. PNG格式试验了glGenerateMi ...

  2. bootstrap中实现外层DIV自适应,内层DIV宽度固定且居中的布局

    <!DOCTYPE html><html> <head> <link rel="stylesheet" href="css/bo ...

  3. Docker学习笔记整理

    Docker接触有一段时间了,但是对于Docker的使用可以说是一点不会.现在要在Docker上部署基于Angular开发的页面.只能一点点积累查找的资料,顺手整理一下,方便后面的回顾. 其中用到的资 ...

  4. 《JavaScript高级程序设计》学习笔记12篇

    写在前面: 这12篇博文不是给人看的,而是用来查的,忘记了什么基础知识,点开页面Ctrl + F关键字就好了 P.S.如果在对应分类里没有找到,麻烦告诉我,以便尽快添上.当然,我也会时不时地添点遗漏的 ...

  5. Reading Notes of Acceptance Test Engineering Guide

    The Acceptance Test Engineering Guide will provide guidance for technology stakeholders (developers, ...

  6. KALI LINUX WEB 渗透测试视频教程—第16课 BEEF基本使用

    Kali Linux Web 渗透测试视频教程—第16课  BeEF基本使用 文/玄魂 目录 Kali Linux Web 渗透测试视频教程—第16课  BeEF基本使用............... ...

  7. windows下python检查文件是否被其它文件打开.md

    有时候我们需要能够判断一个文件是否正在被其它文件访问,几乎不可避免的要调用操作系统接口 from ctypes import cdll import os _sopen = cdll.msvcrt._ ...

  8. atitit.团队建设总结o6o fix

    atitit.团队建设o6o fix #----- 无限放大梦想 2 要有自己的方向...主动添加自己的东东.. 3 有几个tech site,能晓得最新的的知识.. 3 有问题多交流, 3 失败的造 ...

  9. paip.enhes efis 自动获取文件的中文编码

    paip.enhes efis 自动获取文件的中文编码 ##为什么需要自动获取文件的中文编码 提高开发效率,自动获取文件的中文编码  .不需要手动设置编码...轻松的.. ##cpdetector 可 ...

  10. Yii2中系统定义的常用路径别名,如果获取web的url

    下面这些别名都是在Yii2里面系统定义的,可以直接拿来就用 '@yii' => '@yii/swiftmailer' => string 'C:\wamp\www\advanced\ven ...