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

  1. c# 自定义控件之 ComboBox

    winform 自带的 combobox 无法支持根据输入文本匹配列表中的项目,需要使用自定义控件. public class MyCombobox : ComboBox { //初始化数据项 pri ...

  2. WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...

  3. 【转】WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 下拉选择控件ComboBox的自定义样式及扩展: 自定义多选控件Mul ...

  4. 如何开发FineReport的自定义控件?

    FineReport作为插件化开发的报表软件,有些特殊需求的功能需要自己开发,开发的插件包帆软官方有提提供,可以去帆软论坛上找,本文将主要介绍如何开发一个自定义控件,这里讲讲方法论. 第一步:实例化一 ...

  5. WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...

  6. WPF自定义控件与样式(1)-矢量字体图标(iconfont)

    一.图标字体 图标字体在网页开发上运用非常广泛,具体可以网络搜索了解,网页上的运用有很多例子,如Bootstrap.但在C/S程序中使用还不多,字体图标其实就是把矢量图形打包到字体文件里,就像使用一般 ...

  7. WPF自定义控件与样式(2)-自定义按钮FButton

    一.前言.效果图 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 还是先看看效果 ...

  8. WPF自定义控件与样式(15)-终结篇 & 系列文章索引 & 源码共享

    系列文章目录  WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与样式(3)-TextBox & Ric ...

  9. WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 菜单M ...

随机推荐

  1. Handle源码分析,深入群内了解风骚的Handle机制

    Hanlder的使用方式一: private static Handler mHandler = new Handler(){ public void handleMessage(android.os ...

  2. JS中的prototype///////////////////////////z

    JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个 ...

  3. 看门外汉如何实现:C#操作 MongoDB基本CURD的事务控制之 第二部分

    第二部分 尝试解决BulkWrite(List<WriteModel<T>>)问题 在上次发表的文章中,得到了一些很好的反馈,真切体会到写博文的好处,有高人指出两大问题,具体可 ...

  4. 设置程序集(dll)引用路径,整洁美观

    static class Program { //设置引用程序集路径 static Program() { AppDomain.CurrentDomain.SetData("PRIVATE_ ...

  5. IE7 float:left失效的解决方法

    <div id="a" style="width:500px"> <div id="b" style="widt ...

  6. 初识Python第三天(二)

    2.2 OrderedDict有序字典 import collections dic = collections.OrderedDict() dic['k1'] = 'v1' dic['k2'] = ...

  7. fork &vfork --陈皓

    http://coolshell.cn/articles/7965.html http://coolshell.cn/articles/12103.html#more-12103 前两天有人问了个关于 ...

  8. noi 4977 怪盗基德的滑翔翼

    题目链接: http://noi.openjudge.cn/ch0206/4977/ LIS http://paste.ubuntu.com/23406594/

  9. iptables交互配置shell脚本

    #!/bin/bash while true do clear echo "———————-menu————————" echo -e "\033[49;32;1m(1) ...

  10. ggplot2.multiplot:将多个图形使用GGPLOT2在同一页上

    一页多图 介绍 ggplot2.multiplot是一个易于使用的功能,将多个图形在同一页面上使用R统计软件和GGPLOT2绘图方法.这个功能是从easyGgplot2包. 安装并加载easyGgpl ...