开发kendo-ui弹窗组件
摘要:
kendo-ui中只是提供了windwo插件,并没有提供页内弹窗插件。现在分享项目中自己定制的基于window组件的弹窗插件,如果你的项目也是用的kendo-ui,只需要将组件代码引到项目中即可。
特点:
- 支持定时自动关闭弹窗
- 按钮自定义
- 支持最大化最小化

代码:
dialog.js
var dialog = kendo.ui.Window.extend({
// set options
options: {
name : 'GDialog', // widget name
onOk : $.noop,
okText : '确定',
cancelText : '取消',
defaultButtons : 'OK_CANCEL', //'OK' || 'OK_CANCEL' || 'CANCEL' || 'NULL'
extraButtons : [], //[ { text:'ok', className: '', click:function(){} }]
appendTo : 'body',
minWidth : 400,
minHeight : 100,
resizable : false,
actions : ['Close'], // ['Minimize', 'Maximize', 'Close']
autohide : false,
time : 1000
},
// Init method
init: function(element, options){
var me = this;
// Call super.init
kendo.ui.Window.fn.init.call(this, element, options);
// Add buttons
var $buttonsArea = this._addButtons(element, options);
// Set styles
this.wrapper.addClass('k-dialog');
$buttonsArea.addClass('k-button-area');
// Set the dialog's position by default
if (!options || !options.position){ this.center(); }
// if the autohide is setted true that aftering a time auto hide the dialog. default is 1s.
if(this.options.autohide) {
setTimeout(function() {
kendo.ui.Window.fn.close.call(me);
},this.options.time);
}
},
open: function(){
// Call super.open
kendo.ui.Window.fn.open.call(this);
},
minimize: function(){
// Call super.minimize
kendo.ui.Window.fn.minimize.call(this);
$(this.buttonsArea).hide();
this.wrapper.css('padding-bottom', '0');
},
restore: function(){
// Call super.restore
kendo.ui.Window.fn.restore.call(this);
$(this.buttonsArea).show();
this.wrapper.css('padding-bottom', '51px');
},
center: function(){
if (this.options.isMaximized){ return this; }
// Call super.center
kendo.ui.Window.fn.center.call(this);
var that = this,
position = that.options.position,
wrapper = that.wrapper,
documentWindow = $(window),
scrollTop = 0,
newTop;
if (!that.options.pinned){ scrollTop = documentWindow.scrollTop(); }
newTop = scrollTop + Math.max(0,
(documentWindow.height() - wrapper.height() - 50 - parseInt(wrapper.css("paddingTop"), 10)) / 2);
wrapper.css({ top: newTop });
position.top = newTop;
return that;
},
_onDocumentResize: function(){
if (!this.options.isMaximized){ return; }
// Call super._onDocumentResize
kendo.ui.Window.fn._onDocumentResize.call(this);
this._fixWrapperHeight();
},
_fixWrapperHeight: function(){
var height = (this.wrapper.height() - 51).toString() + 'px';
this.wrapper.css('height', height);
},
// Add buttons to the dialog
_addButtons: function(element, options){
var that = this,
buttons = this.options.extraButtons.slice(0);
var nullPattern = /NULL/gi, okPattern = /OK/gi, cancelPattern = /CANCEL/gi,
defaultButtons = {
buttonOk : {text: that.options.okText, className:'ok-btn' , click:function(e){
that.options.onOk.call(that, e);
return false;
}},
buttonCancel : {text: that.options.cancelText, className:'close-btn', click:function(e){
e.preventDefault(); that.close();
}}
};
// Append default buttons
if (!nullPattern.test(this.options.defaultButtons)){
okPattern.test(this.options.defaultButtons) &&
buttons.unshift(defaultButtons.buttonOk);
cancelPattern.test(this.options.defaultButtons) &&
buttons.unshift(defaultButtons.buttonCancel);
}
// Make button area
var buttonsArea = document.createElement('div'),
$buttonsArea = $(buttonsArea);
this.buttonsArea = buttonsArea;
// Make buttons and set buttons' attributes
for (var i = buttons.length - 1; i >= 0; --i){
var $aEl = $(document.createElement('a'));
/*eslint no-script-url: 0*/
$aEl.html(buttons[i].text)
.addClass('k-dialog-button')
.addClass(buttons[i].className)
.attr({href:'javascript:;'})
.on('click', buttons[i].click)
.kendoButton();
$buttonsArea.append($aEl);
}
this.wrapper.append($buttonsArea);
return $buttonsArea;
}
});
kendo.ui.plugin(dialog);
dialog.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/dialog.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="dialog"></div>
<script src="js/dialog.js"></script>
<script>
var dialog = $('#dialog').kendoGDialog({title:"弹窗"}).data('kendoGDialog');
</script>
</body>
</html>
附录:
属性:
| 含义 | 类型 | default | 例子 | |
| content | 弹出内容 | string,function | 宿主元素的内容 |
$("#dialog").kendoGDialog({ content: { url: "/userDetails", dataType: "json", template: "User name: #= data.username #" } }); |
| iframe | 是否已iframe方式显示弹窗 | bool | FALSE | |
| onOk | 点击确定执行的事件 | function |
var dialog = $('#dialog').kendoGDialog({ onOk: function() { dialog.close(); } }).data('kendoGDialog'); |
|
| okText | 确定按钮文本 | string | '确定' | |
| cancelText | 取消按钮文本 | string | '取消' | |
| defaultButtons | 默认显示按钮 | string('OK' || 'OK_CANCEL' || 'CANCEL' || 'NULL') | 'OK_CANCEL' | |
| extraButtons | 自定义的按钮 | array | 空 |
var dialog = $('#dialog').kendoGDialog({ extraButtons: [{ text:'按钮', className: '', click:function(){ } }] }).data('kendoGDialog'); |
| appendTo | 将弹窗插入到 | string | 'body' | |
| height | 弹窗高度 | number | ||
| width | 弹窗宽度 | number | ||
| minWidth | 弹窗最小宽度 | number | 500 | |
| maxWidth | 弹窗最大宽度 | number | ||
| minHeight | 弹窗最小高度 | number | 200 | |
| maxHeight | 弹窗最大高度 | number | ||
| visible | 弹窗是否可见 | bool | TRUE | |
| position | 弹窗位置 | Object |
$("#dialog").kendoGDialog({ position: { top: 100, left: 200 } }) |
|
| resizable | 重置弹窗大小 | bool | TRUE | |
| actions | 弹窗工具组件 | array(
"Close", "Refresh", "Minimize", and "Maximize" ) |
['Close'] | |
| title | 弹窗标题 | string | "" | |
| autohide | 自动关闭 | bool | FALSE | |
| time | 自动关闭时间 | number | 1000(ms) | |
| draggable | 是否可以拖动 | bool | TRUE | |
| resize | 当改变弹窗大小时触发事件 | function | ||
| refresh | 当内容加载完成或者点击刷新按钮内容加载完成时触发事件 | function | ||
| open | 打开弹窗触发事件 | function | ||
| error | 当异步加载内容出错时触发事件 | function | ||
| dragstart | 开始移动弹窗时触发事件 | function | ||
| dragend | 结束移动弹窗时触发事件 | function | ||
| deactivate | 当弹窗关闭结束之后执行事件 | function | ||
| close | 关闭弹窗时执行事件 | function | ||
| activate | 当弹窗打开之后执行的事件 | function |
方法:
| 方法 | 含义 |
例子dialog = $('#dialog').data('kendoGDialog') |
| open | 打开弹窗 | dialog.open(); |
| center | 设置弹窗居中 | dialog.center(); |
| close | 关闭弹窗 | dialog.close(); |
| destroy | 销毁弹窗 | dialog.destroy(); |
| refresh | 刷新弹窗内容 | dialog.refresh(); |
| setOptions | 设置弹窗参数 |
dialog.setOptions({ title: '标题' }); |
| content | 设置弹窗内容 | dialog.content('弹窗内容'); |
| maximize | 最大化 | dialog.maximize(); |
| minimize | 最小化 | dialog.minimize(); |
| title | 弹窗标题 | dialog.title(); |
注意:
工具栏的图片是我自己做的,制作了一个关闭按钮,刷新、最大化、最小化没有做。如果项目中引用了kendo-ui就不需要这个了。同一个弹窗不能多次创建,可以先销毁在创建。
开发kendo-ui弹窗组件的更多相关文章
- Web UI开发推荐!Kendo UI for jQuery自定义小部件——使用MVVM
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...
- Kendo UI 初始化 Data 属性
初始化 Data 属性 前面在介绍准备 Kendo UI 开发环境时我们使用 jQuery 的方法将一个 HTML 元素转换成一个 Kendo UI 控制项: $(“#datepicker”).ke ...
- HTML5 UI框架Kendo UI Web教程:创建自定义组件(三)
Kendo UI Web包 含数百个创建HTML5 web app的必备元素,包括UI组件.数据源.验证.一个MVVM框架.主题.模板等.在前面的2篇文章<HTML5 Web app开发工具Ke ...
- [置顶] Kendo UI开发教程: Kendo UI 示例及总结
前面基本介绍完Kendo UI开发的基本概念和开发步骤,Kendo UI的示例网站为http://demos.kendoui.com/ ,包含了三个部分 Web DemoMobile DemoData ...
- Kendo UI开发教程(27): 移动应用开发简介
Kendo UI 支持开发Web应用,前面介绍的SPA,也支持开发移动应用,至于使用 HTML5 + JavaScript + CSS开发移动是不是一个好的选择不在本文的讨论之中.Kendo UI M ...
- Kendo UI开发教程(9): Kendo UI Validator 概述
Kendo UI Validator 支持了客户端校验的便捷方法,它基于HTML 5 的表单校验功能,支持很多内置的校验规则,同时也提供了自定义规则的便捷方法. 完整的Kendo UI 的Valida ...
- 【Kendo UI系列开发使用笔记】01-简单介绍
ps:接触telerik出品的kendo ui系列已经快有一年了,使用过程中也在不断踩坑填坑.这套UI用起来还是非常爽的,尤其asp.net mvc版的配合lambda表达式来配置参数非常流畅.这次对 ...
- Kendo UI 移动应用开发简介
Kendo UI 移动应用开发简介 Kendo UI 支持开发 Web 应用,前面介绍的 SPA,也支持开发移动应用,至于使用 HTML5 + JavaScript + CSS 开发移动是不是一个好的 ...
- 准备Kendo UI 开发环境
准备 首先你需要从 Telerik 网站下载试用版开发包,注意需要注册后才能下载. 下载后直接解压后包含下面几个文件和目录: ./examples – 示例. /js – minified 化后的 J ...
随机推荐
- Java log4j slf4j 日志配置笔记
http://www.cnblogs.com/Scott007/p/3269018.html 日志的打印,在程序中是必不可少的,如果需要将不同的日志打印到不同的地方,则需要定义不同的Appender, ...
- PPP或PPPOE身份验证PAP和CHAP
PPP或PPPOE都支持身份验证,有两种验证方式:PAP和CHAP. PAP,Passwd Authentication Protocol,密码验证协议,以客户端明文方式传递用户名和密码,服务器和本省 ...
- 创建function实现hive表结果导出到mysql
1. 创建临时function (这里两个包都是hive自带的,不需要自己开发的,可以根据名称查找对应的版本) add jar /opt/local/hive/lib/hive-contrib-.ja ...
- OpenSceneGraph-3.2.0 源代码的编辑步骤
到osg官网去下载源代码 官网 再把资源包下载下来叫作3dpart资源包. 源代码下载下来之后依照这个步骤来. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- js类型判断的方法
var arr=[]; alert(Object.prototype.toString.call(arr)=='[object Array]');
- jQuery运行方式818
我们平时打开JQ源码就会看到这么一段代码 (function (window, undefined) { //JQ代码 })(window) 有一点经验的朋友会知道这是js自执行函数 它的好处主要作用 ...
- Sword redis数据结构
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) string 是 redi ...
- 使用 pv 命令监控 linux 命令的执行进度
如果你是一个 linux 系统管理员,那么毫无疑问你必须花费大量的工作时间在命令行上:安装和卸载软件,监视系统状态,复制.移动.删除文件,查错,等等.很多时候都是你输入一个命令,然后等待很长时间直到执 ...
- 在MySQL中创建cm-hive使用的数据库及账号
放上来做一个备份,免得忘了密码,囧~ mysql> create database metastore default character set utf8; Query OK, row aff ...
- C#或者.NET下的强制垃圾回收办法
转载 2011年03月16日 17:21:00 标签: c# / .net / button / object / stream / class 8185 今天来谈谈C#的GC,也就是垃圾回收机制,非 ...