自定义控件之 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 ...
随机推荐
- Linux系统巡检常用命令
# uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostn ...
- List,Set,Map用法以及区别(转)
Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素.一些Collection允许相同的元素而另一些不行.一些能排序而另一些不行.Java ...
- C#计算器代码
在刚刚接触c#的时候,就想做一个简单加减乘除计算器.这就是目标,可惜一直没有动手去做,今天特意把它简单做了.很简单,很简单,了却一个心愿了. 代码: using System; using Syste ...
- em
macro jumptocaller(){ JumpToLocation(GetSymbolLocation((GetCurSymbol ())))}
- 05-IP核应用之计数器——小梅哥FPGA设计思想与验证方法视频教程配套文档
芯航线--普利斯顿队长精心奉献 实验目的:了解FPGA的IP核相关知识并以计数器IP核为例学会基本IP使用的流程 实验平台:无 实验原理: IP核(Intellectual Propert ...
- 实用Redis操作类
<?php /** * ------------------------------------------ * 统一redis的配置与数据存储规范,便于扩展与修改 * # redis通常用于热 ...
- JAVA基础知识之JDBC——ResultSet的滚动和更新(statement的额外参数)
ResultSet不仅可以内存中的一张二维表一样保存statement执行SQL的结果集,还能通过结果集修改DB的数据.ResultSetMetaData则可以用来获得ResultSet对象的相关信息 ...
- 从yum源下载安装包及依赖包
局域网内所有linux都用yum从外网源安装软件有点浪费,尤其遇到下载慢的情况: 所以考虑下载后传到其他机器安装,还可以保证版本一致(创建一个本地仓库更好,这个后面研究了再记录): 首先安装yum工具 ...
- 配置cas
在给tomcat配置好证书的基础上做一下操作(可以根据上一篇博客进行配置) 1.cas服务端配置(最后更改一下服务器tomcat的端口号) 第一步:下载cas-server-3.4.2.1-relea ...
- Windows server 2012 AD DS 搭建步骤
服务器版本:Windows server 2012 1. 配置网络,由于本机会搭建DNS服务器,因此首选DNS服务器设置为127.0.0.1 2. 打开服务器管理器 3. 点击添加角色和功能,下 ...