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下拉框的默认三角样式,因为它在不同浏览器的样式不同且有点丑,找找网上也没什么详细修改方法,我就总结一下自己的吧. 目标是做成下图效果: 图一:将默认小三角换成红圈的三角 ...
随机推荐
- MacBook如何用Parallels Desktop安装windows7/8
虽然MacBook真的很好用,不过在天朝的国情下,有很多软件还是仅支持IE和windows系统下才有.所以有必要为自己的MacBook装一个windows版本的系统,之前试过用Boot Camp来建立 ...
- Win10下打开chm文档提示无法显示该页的解决方法
一是检查chm文件属性里最下面是否有个“解除锁定”,如有,点击“解除锁定”按钮就可以了. 如果没有上面提到的“解除锁定”,检查chm文件存放的路径.本例中,由于chm文件的存放路径中不能带有特殊字符“ ...
- freemarker自己定义标签报错(二)
freemarker自己定义标签 1.错误描写叙述 freemarker.core.ParseException: Unexpected end of file reached. at freemar ...
- 详解Github的.gitignore忽略文件+.gitignore不生效解决方案+生产配置大奉送
详解Github的.gitignore忽略文件+.gitignore不生效解决方案+生产配置大奉送 Git中有一个非常重要的一个文件-----.gitignore 今天给大家免费送一个.gitigno ...
- 获取checkbox的选中的值
var arr = new Array(); var idsObj = $("input [name='ids']"); for(var i=0;i<idsObj.lengt ...
- 微信小程序——倒计时功能
做小程序项目中,需要做一个倒计时功能,如下图: 记录一下实现步骤: 1.考虑到这个功能可能会有多处用到,所以把倒计时的函数写在utils.js里面: const formatNumber = n =& ...
- android适配pad和部分手机底部虚拟按键+沉浸式状态栏
在使用沉浸式状态栏设置界面全屏时发现pad和部分手机(华为和魅族系统自带)屏幕底部会带有虚拟按键,遮挡住界面本身的一部分. 为了设置隐藏,在网上找了一些方法,设置Activity主题再在布局加fits ...
- R语言比较运算符和逻辑运算符
比较运算:>,<,>=,<=,==,!=.(大于,小于,大于等于,小于等于,等于,不等于.) 逻辑运算:&,|,!.(与,或,非.)
- Android学习之SharedPreferences
SharedPreferences使用键值对的方式来存储数据,并支持多种不同类型的数据存储. 1.界面布局 <TableLayout xmlns:android="http://sch ...
- Qt中如何根据类名来实例化对象
对于Qt 来说,是可以做到运行时,根据对象的类名字(字符串)来获得对象的实例的,这点和一些语言的反射机制是一样的. 但是在Qt中,我们需要所额外的一步,就是注册.只要做到了注册,我们就可以 自由的创建 ...