摘要:

  最近项目中有一个特殊的需求,下拉选择部门,但是部门的层级关系要在下来框中体现出来,如下图

下面我就把实现的过程分享给大家,代码如下:

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)的更多相关文章

  1. Atitit.ui控件---下拉菜单选择控件的实现select html

    Atitit.ui控件---下拉菜单选择控件的实现select   html 1. 调用& model的实现 1 2. -----select.jsp------ 1 1. 调用& m ...

  2. select下拉框选择触发事件

    我一直以来都认为,select 下拉框选择对选项 options 使用 onclick 注册事件即可,如下: <select> <option value="0" ...

  3. PHP下拉框选择的实现方法

    实现 第一种PHP下拉框实现方法: < ?php //提交下拉框; //直接饱触发onchange事件的结果 $id=$_GET['myselect']; // myselect 为locati ...

  4. 仿网易新闻app下拉标签选择菜单

    仿网易新闻app下拉标签选择菜单 仿网易新闻app下拉标签选择菜单,长按拖动排序,点击增删标签控件 ##示例  ##EasyTagDragView的使用 在layout布局里添加:  

  5. Selenium 3----警告框处理+下拉框选择

    警告框处理 在WebDriver中处理JavaScript所生成的alert.confirm以及prompt十分简单,具体做法是使用 switch_to.alert 方法定位到 alert/confi ...

  6. 下拉框选择blur与click冲突问题

    缘由:今天在做下拉框选择时,遇到click和blur冲突问题:具体现象如下 1.intput框获取焦点(focus事件)时显示隐藏的下拉框,失去焦点(blur事件)则隐藏下拉框 2.点击选择(clic ...

  7. 前端下拉框选择和动态生成调用div

    进入到一个项目期中,一边做项目,一边学习其中用到的知识.这些知识都是零碎的,有数据库,有html,有js,还有django.趁周末时间,整理前面遇到过的前端相关的知识点. 下拉框选择 <html ...

  8. excel选择元角分下拉菜单选择框自动变更数字

    excel选择元角分下拉菜单选择框自动变更数字 (M2列),数据-->数据有效性-->在“允许”栏中选择序列-->在“来源”栏中输入:分,角,元单位倍数公式(M4列):=IF(M2= ...

  9. 将select的默认小三角替换成别的图片,且实现点击图片出现下拉框选择option

    最近做项目,要求修改select下拉框的默认三角样式,因为它在不同浏览器的样式不同且有点丑,找找网上也没什么详细修改方法,我就总结一下自己的吧. 目标是做成下图效果: 图一:将默认小三角换成红圈的三角 ...

随机推荐

  1. redis+nginx+tomcat故障大全

    tomcat+nginx+redis中 redis死掉了???确实是防火墙问题centos7用的firewalldfirewalld默认已经安装并启用了,如果需要nginx可以访问需要执行以下命令:# ...

  2. PowerDesigner使用教程【转】

    PowerDesigner使用教程 —— 概念数据模型   一.概念数据模型概述    概念数据模型也称信息模型,它以实体-联系(Entity-RelationShip,简称E-R)理论为基础,并对这 ...

  3. Java编程的逻辑 (40) - 剖析HashMap

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  4. 【linux】——FTP出现500 OOPS: cannot change directory的解决方法

    cannot change directory:/home/*** ftp服务器连接失败,错误提示: 500 OOPS: cannot change directory:/home/******* 5 ...

  5. Gojs简单例子

    前台代码: <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> ...

  6. Web服务端开发需要考虑的问题(续)

    方案汇总API设计应用架构代码库管理工具链工作计划目标预期关键过程service-driver接口定义及实现web样例 方案汇总 API设计 基于https. 只提供纯数据. 基于一开始提出的rest ...

  7. jQuery验证插件使用初步

    validate是一款优秀的表单验证插件,其初步使用方法如下: (1)前台页面代码: <form id="form1" runat="server"> ...

  8. tensorflow里面共享变量、name_scope, variable_scope等如何理解

    tensorflow里面共享变量.name_scope, variable_scope等如何理解 name_scope, variable_scope目的:1 减少训练参数的个数. 2 区别同名变量 ...

  9. 【转】【Python】Python发送邮件(常见四种邮件内容)

    在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下,Shell脚本发送邮件告警是件很简单的事,有现成的邮件服务软件或者调 ...

  10. 【WPF】鼠标拖拽功能DragOver和Drop

    在Winform里面实现拖入功能只要设置控件AllowDrop=true; 然后实现方法 //拖入 private void txtInputPath_DragOver(object sender, ...