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) {

// }
//});

最新文章

  1. Java学习2 - JDK和JRE和JVM的区别_JDK的下载安装_环境变量配置
  2. 真正的mybatis_redis二级缓存
  3. Excel表格常用的函数,留着备用
  4. web面试题大全
  5. HDU 4292 Food 最大流
  6. BZOJ3772: 精神污染
  7. redis 和 bloom filter
  8. 11、四大组件之二-Service高级(二)Native Service
  9. AutoLayout学习之理解intrinsicContentSize,Content Hugging Priority,Content Compression Resistance Priority
  10. springboot 打包
  11. Lesser known purrr tricks
  12. 改变图像,运用match方法判断
  13. Gazebo機器人仿真學習探索筆記(五)環境模型
  14. fpga xilink 电平
  15. Java多线程之synchronized线程锁
  16. sqlserver 日志传送
  17. HDU_5527_Too Rich
  18. 第一百五十三节,封装库--JavaScript,表单验证--备注字数验证
  19. java IO 对象流 反序列化和序列化
  20. Ubuntu截图工具gnome-screenshot使用教程

热门文章

  1. 夺命雷公狗-----React---8--react官方提供的组建实现双向绑定
  2. 【final】站立会议---11.27
  3. sql join 优化
  4. Javascript图片无缝滚动
  5. enbale blakboxing
  6. Linq join
  7. :selected
  8. 使用 supervisor 管理进程
  9. s3c2440 移值u-boot-2016.03 第5篇 支持dm9000 识别
  10. 关于ajax伪实时动态下拉显示最新数据