摘要:

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

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

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. for语句练习 阶乘

    4的阶乘:4!=1*2*3*4 public class g { /** * @param args */ public static void main(String[] args) { int n ...

  2. Redis初步认识

    官网:redis.io Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的 ...

  3. kafka集群中jmx端口设置

    jmx端口主要用来监控kafka集群的. 在启动kafka的脚本kafka-server-start.sh中找到堆设置,添加export JMX_PORT="9999" if [ ...

  4. drupal pathauto的配置

  5. Ubuntu下架设FTP服务器

    Linux下提供了很多的ftp服务器,这里我选用了安全,快速,简单的vsftpd作为FTP服务器.本文是我在自己的Ubuntu 10.10 -32 位系统下搭建的.搭建方法简单,按照本过程,您也可以完 ...

  6. Entity Framework应用:导航属性

    一.主键和外键 关系型数据库中的一条记录中有若干个属性,若其中某一个属性组是能唯一标识一条记录,该属性组就可以称为主键.例如: 学生版(学号.姓名.性别.班级) 其中每个学生的学号是唯一的,学号就是一 ...

  7. 基于Zookeeper的分步式队列系统集成案例

    基于Zookeeper的分步式队列系统集成案例 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, ...

  8. jQuery AJAX中文乱码处理

    最近工作中用jQuery ajax返回出现乱码,用的Notepad++编辑器,当JS部分传递中文时,另一页面接收的话会出现乱码,在网上找了很多方法,基本上没有很好的解决. 页面用GB2312编码,JS ...

  9. 【转】【CentOS】【Python】Centos7安装Python3的方法

    由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_centos Python-3.6.2]# ...

  10. (笔记)Linux下system()函数的深度理解(整理)

    注:从其它地方转的非常好的一篇文章,值得深究! 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数 ...