kendo-ui下拉树形选择(DropDownTreeView)
摘要:
最近项目中有一个特殊的需求,下拉选择部门,但是部门的层级关系要在下来框中体现出来,如下图
下面我就把实现的过程分享给大家,代码如下:
dropdowntreeview.js
/*
*
* DropDownTreeView
* https://github.com/jsExtensions/kendoui-extended-api
*
*/
var DropDownTreeView = kendo.ui.Widget.extend({
_uid: null,
_treeview: null,
_dropdown: null,
_v: null, init: function (element, options) {
var that = this; kendo.ui.Widget.fn.init.call(that, element, options); that._uid = new Date().getTime(); $(element).append(kendo.format("<input id='extDropDown{0}' class='k-ext-dropdown'/>", that._uid));
$(element).append(kendo.format("<div id='extTreeView{0}' class='k-ext-treeview' style='z-index:1;'/>", that._uid)); // Create the dropdown.
that._dropdown = $(kendo.format("#extDropDown{0}", that._uid)).kendoDropDownList({
dataSource: [{ text: "", value: "" }],
dataTextField: "text",
dataValueField: "value",
open: function (e) {
//to prevent the dropdown from opening or closing. A bug was found when clicking on the dropdown to
//"close" it. The default dropdown was visible after the treeview had closed.
e.preventDefault();
// If the treeview is not visible, then make it visible.
if (!$treeviewRootElem.hasClass("k-custom-visible")) {
// Position the treeview so that it is below the dropdown.
$treeviewRootElem.css({
"top": $dropdownRootElem.position().top + $dropdownRootElem.height(),
"left": $dropdownRootElem.position().left
});
// Display the treeview.
$treeviewRootElem.slideToggle('fast', function () {
that._dropdown.close();
$treeviewRootElem.addClass("k-custom-visible");
});
}
}
}).data("kendoDropDownList"); if (options.dropDownWidth) {
that._dropdown._inputWrapper.width(options.dropDownWidth);
} var $dropdownRootElem = $(that._dropdown.element).closest("span.k-dropdown"); // Create the treeview.
that._treeview = $(kendo.format("#extTreeView{0}", that._uid)).kendoTreeView(options.treeview).data("kendoTreeView");
that._treeview.bind("select", function (e) {
// When a node is selected, display the text for the node in the dropdown and hide the treeview.
$dropdownRootElem.find("span.k-input").text($(e.node).children("div").text());
$treeviewRootElem.slideToggle('fast', function () {
$treeviewRootElem.removeClass("k-custom-visible");
that.trigger("select", e);
});
var treeValue = this.dataItem(e.node);
that._v = treeValue[options.valueField];
}); var $treeviewRootElem = $(that._treeview.element).closest("div.k-treeview"); // Hide the treeview.
$treeviewRootElem
.css({
"border" : "1px solid #dbdbdb",
"display" : "none",
"position" : "absolute",
"background-color": that._dropdown.list.css("background-color")
}); $(document).click(function (e) {
// Ignore clicks on the treetriew.
if ($(e.target).closest("div.k-treeview").length === 0) {
// If visible, then close the treeview.
if ($treeviewRootElem.hasClass("k-custom-visible")) {
$treeviewRootElem.slideToggle('fast', function () {
$treeviewRootElem.removeClass("k-custom-visible");
});
}
}
});
},
value: function(v){
var that = this;
if(v !== undefined){
var n = that._treeview.dataSource.get(v);
var selectNode = that._treeview.findByUid(n.uid);
that._treeview.trigger('select',{node: selectNode});
var $treeviewRootElem = $(that._treeview.element).closest("div.k-treeview");
$treeviewRootElem.hide();
}
else{
return that._v;
}
}, dropDownList: function () {
return this._dropdown;
}, treeview: function () {
return this._treeview;
},
refresh: function () {
return this._treeview.dataSource.read();
},
options: {
name: "DropDownTreeView"
}
});
kendo.ui.plugin(DropDownTreeView);
index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link rel="stylesheet" type="text/css" href="kendo/styles/kendo.common.min.css" />
<link rel="stylesheet" type="text/css" href="style/dropdowntreeview.css" />
</head>
<body>
<script type="text/javascript" charset="utf-8" src="http://cdn.staticfile.org/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="kendo/js/kendo.chopper.min.js"></script>
<script type="text/javascript" charset="utf-8" src="kendo/js/kendo.chopper.min.js"></script>
<script type="text/javascript" charset="utf-8" src="kendo/js/cultures/kendo.messages.zh-CN.js"></script>
<script type="text/javascript" charset="utf-8" src="kendo/js/cultures/kendo.culture.zh-CN.min.js"></script> <div id="DropDownTreeView"></div> <script src="js/dropdowntreeview.js"></script>
<script>
var datasource = [{
"text": "\u90e8\u95e8",
"expanded": true,
"id": 0,
"items": [
{
"text": "\u8fd0\u8425\u90e8",
"expanded": true,
"pid": 0,
"comments": "20140821-1",
"items": [
{
"text": "\u4ea7\u54c1\u7ec4",
"expanded": true,
"pid": 1,
"comments": "20140821-7",
"items": [],
"id": 7
}
],
"id": 1
},
{
"text": "\u8d22\u52a1\u90e8",
"expanded": true,
"pid": 0,
"comments": "20140821-2",
"items": [],
"id": 2
},
{
"text": "\u884c\u653f\u90e8",
"expanded": true,
"pid": 0,
"comments": "20140821-3",
"items": [],
"id": 3
},
{
"text": "\u7814\u53d1\u90e8",
"expanded": true,
"pid": 0,
"comments": "20140821-4",
"items": [
{
"text": "\u5f00\u53d1\u7ec4",
"expanded": true,
"pid": 4,
"comments": "20140821-8",
"items": [],
"id": 8
},
{
"text": "\u6d4b\u8bd5\u7ec4",
"expanded": true,
"pid": 4,
"comments": "20140821-9",
"items": [],
"id": 9
}
],
"id": 4
},
{
"text": "\u8fd0\u7ef4\u90e8",
"expanded": true,
"pid": 0,
"comments": "20140821-5",
"items": [],
"id": 5
},
{
"text": "\u9500\u552e\u90e8",
"expanded": true,
"pid": 0,
"comments": "20140821-6",
"items": [
{
"text": "\u552e\u524d\u7ec4",
"expanded": true,
"pid": 6,
"comments": "20140821-10",
"items": [],
"id": 10
},
{
"text": "\u552e\u540e\u7ec4",
"expanded": true,
"pid": 6,
"comments": "20140821-11",
"items": [],
"id": 11
}
],
"id": 6
}
]
}];
var DropDownTreeView = $('#DropDownTreeView').kendoDropDownTreeView({
dropDownWidth : '177px',
valueField : 'id',
treeview : {
dataSource: datasource
}
}).data("kendoDropDownTreeView"); console.log(DropDownTreeView.value()); // 如果数据是异步获取的,可以这样设置dataSource
// var treeDataSource = new kendo.data.HierarchicalDataSource({
// transport: {
// read: {
// url: '',
// dataType: "json"
// }
// },
// schema: {
// data: 'data'
// },
// requestEnd: function(e){
// DropDownTreeView.treeview().setDataSource(e.response.data)
// }
// });
// treeDataSource.read();
</script>
</body>
</html>
配置参数:
属性
类型 | 意义 | |
dataSource | Array | 设置数据 |
dataTextField | String | 显示文本字段 |
dataValueField | String | value字段 |
dropDownWidth | String | 设置下拉输入框宽度 |
treeview | Object | 设置树形结构选项,如datasource |
方法
| 意义 |
open | 打开下拉框 |
close | 关闭下拉框 |
value | 获取选中元素的值 |
dropDownList | 获取下拉选项 |
treeview | 获取树形结构选项 |
refresh | 刷新下拉框 |
附录:
kendo-ui下拉树形选择(DropDownTreeView)的更多相关文章
- Atitit.ui控件---下拉菜单选择控件的实现select html
Atitit.ui控件---下拉菜单选择控件的实现select html 1. 调用& model的实现 1 2. -----select.jsp------ 1 1. 调用& m ...
- select下拉框选择触发事件
我一直以来都认为,select 下拉框选择对选项 options 使用 onclick 注册事件即可,如下: <select> <option value="0" ...
- PHP下拉框选择的实现方法
实现 第一种PHP下拉框实现方法: < ?php //提交下拉框; //直接饱触发onchange事件的结果 $id=$_GET['myselect']; // myselect 为locati ...
- 仿网易新闻app下拉标签选择菜单
仿网易新闻app下拉标签选择菜单 仿网易新闻app下拉标签选择菜单,长按拖动排序,点击增删标签控件 ##示例 ##EasyTagDragView的使用 在layout布局里添加:
- Selenium 3----警告框处理+下拉框选择
警告框处理 在WebDriver中处理JavaScript所生成的alert.confirm以及prompt十分简单,具体做法是使用 switch_to.alert 方法定位到 alert/confi ...
- 下拉框选择blur与click冲突问题
缘由:今天在做下拉框选择时,遇到click和blur冲突问题:具体现象如下 1.intput框获取焦点(focus事件)时显示隐藏的下拉框,失去焦点(blur事件)则隐藏下拉框 2.点击选择(clic ...
- 前端下拉框选择和动态生成调用div
进入到一个项目期中,一边做项目,一边学习其中用到的知识.这些知识都是零碎的,有数据库,有html,有js,还有django.趁周末时间,整理前面遇到过的前端相关的知识点. 下拉框选择 <html ...
- excel选择元角分下拉菜单选择框自动变更数字
excel选择元角分下拉菜单选择框自动变更数字 (M2列),数据-->数据有效性-->在“允许”栏中选择序列-->在“来源”栏中输入:分,角,元单位倍数公式(M4列):=IF(M2= ...
- 将select的默认小三角替换成别的图片,且实现点击图片出现下拉框选择option
最近做项目,要求修改select下拉框的默认三角样式,因为它在不同浏览器的样式不同且有点丑,找找网上也没什么详细修改方法,我就总结一下自己的吧. 目标是做成下图效果: 图一:将默认小三角换成红圈的三角 ...
随机推荐
- OKR
不得不佩服老外对概念的提炼能力.一套一套的. Mission Vision Strategic Objectives Key Results Tasks
- mypy 支持静态类型编程的python变种
每种编程语言都有一群固定的用户,对于那些习惯将不同编程语言用成同样的感觉的人来说,最是难受.因为每种语言都有它独特的设计『哲学』和擅长的应用领域. PHP给大家的一贯的印象都是动态弱类型语言,Pyth ...
- Web前端开发测试题阅读笔记
引自: http://www.w3cplus.com/css/front-end-web-development-quiz.html Q7:下面代码弹出值是什么? x = 1; function ba ...
- 如何将mysql表结构导出成Excel格式的(并带备注)另附转为word表格的方法
方法一: 1.使用一个MySQL管理工具:SQLyog,点击菜单栏“数据库”下拉的最后一项: 导出的格式如下: 2.要想转成Excel格式的只需手动将该表复制到Excel中去. 方法二: 1.以下用的 ...
- JSP字符集编码集合
在这里,我们先说说JSP/Servlet中的几个编码的作用. 在JSP/Servlet 中主要有以下几个地方可以设置编码,pageEncoding="UTF-8".contentT ...
- Java关键字instanceof
深入Java关键字instanceof instanceof关键字用于判断一个引用类型变量所指向的对象是否是一个类(或接口.抽象类.父类)的实例. 举个例子: public interfa ...
- R语言 使用命令行参数运行R程序
args_test.R 代码如下: Args <- commandArgs()cat("Args[1]=",Args[1],"\n")cat(" ...
- 25个顶级的jQuery表格插件
jQuery 表格插件可以让你创建各种各样的表格布局,表格布局是报纸和杂志中最常见的布局,现在的网站中也很常见,在这篇文章中,我向大家推荐25个jQuery 的表格插件,你可以任意控制表格的行和列,用 ...
- http://www.gasi.ch/blog/inside-deep-zoom-1/
In March 2007 Blaise Aguera y Arcas presented Seadragon & Photosynth at TED that created quite s ...
- ubuntu lapack安装与使用
https://blog.csdn.net/zouyu1746430162/article/details/53374693 https://blog.csdn.net/mlnotes/article ...