自定义控件之 Combobox
var ComboboxObj = function (id, url) {
this.URL = url; //Ajax url
this.ID = id; //combobox id
this.method = 'get'; //Ajax type "POST" or "GET"
this.width = 250; //combobox width
this.height = 22; //combobox height
this.selectValue = ""; //combobox selected value
this.selectText = "";//combobox selected text
this.selectIndex = 0;//combobox selected index
this.initValue = ""; //combbobox initial value
this.valueField = "ID"; //combobox value field
this.textField = "Name"; //combobox text field
this.enable = true; //combobox enable or disable
this.source = new Object();
}
//Set combobox width
ComboboxObj.prototype.setWidth = function (wid) { this.width = wid; };
//set combobox height
ComboboxObj.prototype.setHeight = function (hei) { this.Height = hei; };
//initial combobox data and load data
ComboboxObj.prototype.loadData = function () {
var context = this;
$.ajax({
url: context.URL,
type: context.method,
dataType: "json",
contentType: "application/json;charset=utf-8",
beforeSend: function () {
//var disabled = context.enable ? "" : "disabled='disabled'";
var $selector = $("<select id='" + context.ID + "_sel' style='width:" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;'/>");
$("#" + context.ID).append($selector);
context.enable ? "" : context.setDisabled();
$selector.on('change', function (evt) {
context.selectValue = $(this).val();
context.selectText = $(this).find("option:selected").text();
context.selectIndex = $(this).find("option:selected").index();
context.onSelect($(this).val());
});
},
success: function (data) {
context.source = data;
//Tip: add an empty value is for triggering the onSelect event
$("#" + context.ID + "_sel").append("<option value=''>Make Selection...</option>");
$(data).each(function (i, item) {
var val = item[context.valueField];
var txt = item[context.textField];
$("#" + context.ID + "_sel").append("<option value='" + val + "'>" + txt + "</option>");
});
if (data != null&&context.initValue != "") {
$("#" + context.ID + "_sel").val(context.initValue);
context.selectValue = context.initValue;
context.selectText = $("#" + context.ID + "_sel").find("option:selected").text();
context.selectIndex = $("#" + context.ID + "_sel").find("option:selected").index();
}
context.setEnabled();
},
error: function (e) {
console.log(e);
},
headers: {
'Token': $("#_requiredToken").val()
}
});
}
ComboboxObj.prototype.loadDataWithoutURL = function () {
var context = this;
var disabled = context.enable ? "" : "disabled='disabled'";
var $selector = $("<select id='" + context.ID + "_sel' style='width:" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;' " + disabled + "/>");
$("#" + context.ID).append($selector);
$selector.on('change', function (evt) {
context.selectValue = $(this).val();
context.selectText = $(this).find("option:selected").text();
context.selectIndex = $(this).find("option:selected").index();
context.onSelect($(this).val());
});
}
ComboboxObj.prototype.setData = function (json) {
var context = this;
//first: delete combobox data
$("#" + context.ID + "_sel").empty();
//second: bind the json to the combobox
//Tip: add an empty value is for triggering the onSelect event
$("#" + context.ID + "_sel").append("<option value=''>Make Selection...</option>");
$(json).each(function (i, item) {
var val = item[context.valueField];
var txt = item[context.textField];
$("#" + context.ID + "_sel").append("<option value='" + val + "'>" + txt + "</option>");
});
}
//function if the combobox is selected,we can rewrite it.
ComboboxObj.prototype.onSelect = function (selected) {
alert(selected);
}
//set combobox value
ComboboxObj.prototype.setValue = function (value) {
$("#" + this.ID + "_sel").val(value);
this.selectValue = value;
this.selectText = $(this).find("option:selected").text();
this.selectIndex = $(this).find("option:selected").index();
}
//set combobox is enable
ComboboxObj.prototype.setEnabled = function () {
$("#" + this.ID + "_sel").attr("style", "width:" + this.width + "px; height:" + this.height + "px;border: 1px solid #95B8E7; ");
$("#" + this.ID + "_sel").removeAttr("disabled");
}
//set combobox is disabled
ComboboxObj.prototype.setDisabled = function () {
$("#" + this.ID + "_sel").attr("disabled", "disabled");
$("#" + this.ID + "_sel").attr("style", "width:" + this.width + "px; height:" + this.height + "px;border: 1px solid #95B8E7;background-color: #DFDFDF; ");
}
//delete all combobox items in the combobox
ComboboxObj.prototype.clear = function () {
$("#" + this.ID+"_sel").empty();
}
//Get the combobox datasource
ComboboxObj.prototype.getData = function () {
return this.source;
}
//This code is for conbobox test
//$(function () {
// var o = new ComboboxObj('cc2', '/ResourceAPI/api/Resource/GetWorlds');
// o.loadData();
// o.initValue = 0;
// o.onSelect = function (select) {
// p.setValue(select);
// //p.setDisabled();
// p.clear();
// }
// var p = new ComboboxObj('cc1', '/ResourceAPI/api/Resource/GetWorlds');
// p.loadData();
// p.onSelect = function (select) {
// }
//});
自定义控件之 Combobox的更多相关文章
- c# 自定义控件之 ComboBox
winform 自带的 combobox 无法支持根据输入文本匹配列表中的项目,需要使用自定义控件. public class MyCombobox : ComboBox { //初始化数据项 pri ...
- WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...
- 【转】WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 下拉选择控件ComboBox的自定义样式及扩展: 自定义多选控件Mul ...
- 如何开发FineReport的自定义控件?
FineReport作为插件化开发的报表软件,有些特殊需求的功能需要自己开发,开发的插件包帆软官方有提提供,可以去帆软论坛上找,本文将主要介绍如何开发一个自定义控件,这里讲讲方法论. 第一步:实例化一 ...
- WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展
一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...
- WPF自定义控件与样式(1)-矢量字体图标(iconfont)
一.图标字体 图标字体在网页开发上运用非常广泛,具体可以网络搜索了解,网页上的运用有很多例子,如Bootstrap.但在C/S程序中使用还不多,字体图标其实就是把矢量图形打包到字体文件里,就像使用一般 ...
- WPF自定义控件与样式(2)-自定义按钮FButton
一.前言.效果图 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 还是先看看效果 ...
- WPF自定义控件与样式(15)-终结篇 & 系列文章索引 & 源码共享
系列文章目录 WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与样式(3)-TextBox & Ric ...
- WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 菜单M ...
随机推荐
- 匈牙利命名法、骆驼命名法、帕斯卡(pascal)命名法
(2008-05-24 13:37:55) 转载▼ 标签: 杂谈 分类: 编程杂文 一.匈牙利命名法: 广泛应用于象Microsoft Windows这样的环境中. Windows 编 ...
- pb数据窗口设置操作
1 使DataWindow列只能追加不能修改如何使DataWindow中的数据只能追加新记录而不能修改,利用 Column 的 Protect 属性可以很方便的做到这一点,方法如下:将每一列的 Pro ...
- PHP排序函数
/** * 对查询结果集进行排序 * http://www.onethink.cn * /Application/Common/Common/function.php * * @access publ ...
- 使用Redis分布式队列
1.这是处理异常的类 public class MyExceptionAttribute:HandleErrorAttribute { //public static Queue<Excepti ...
- 白话LINQ系列2---以代码演进方式学习LINQ必备条件
今天,我们通过一个简单的示例代码的演进过程,来学习LINQ必备条件:隐式类型局部变量:对象集合初始化器:委托:匿名函数:lambda表达式:扩展方法:匿名类型.废话不多说,我们直接进入主题. 一.实现 ...
- ListView的LayoutParams设置
// Temp is the root view that was found in the xml final View temp = createViewFromTag(root, name, a ...
- ELK修炼之道
看了ELK大半年了,现在就慢慢的总结一下对ELK的理解 参考资料 ELK stack中文指南 Elasticsearch权威指南 官方文档 Elasticsearch基础篇 此篇用于介绍Elastic ...
- echo(),print(),print_r(),var_dump的区别?
常见的输出语句 echo()可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用. print()函 ...
- css3属性之 box-sizing
w3c plus上有详细介绍及原理http://www.w3cplus.com/content/css3-box-sizing,这里摘录其中重要知识点部分. 本文重点考虑移动开发,所以默认移动端的浏览 ...
- :radio :checkbox
匹配所有单选按钮 示例 描述: 查找所有单选按钮 HTML 代码: <form> <input type="text" /> <input typ ...