; (function ($, window, document, undefined) {
//定义Beautifier的构造函数
var createzeone = function (ele, opt) {
this.$element = ele,
this.defaults = {
'search': true,
'clear': true,
'link': false,
url: "",
searchEnd: undefined,
clearEnd: undefined,
isMulti: false
};
var options = $.extend(this.defaults, opt);
var id = "";
var html = '';
html += ' ';
html += ' ';
if (options.search) {
html += ' ';
}
if (options.link) {
html += ' ';
}
if (options.clear) {
html += ' ';
}
html += ' ';
this.$element.html(html);
this.$element.find("input[name='selecttext']").attr("ismult", options.isMulti);
// $(this).attr("ismult", options.isMulti);
// this.$element.find("button[name='onanedsads']")
this.$element.find("button[name='datasearchbtn']").bind("click", function () {
ToSearch(ele, options);
});
this.$element.find("button[name='datalinkbtn']").bind("click", function () {
// alert("点击了链接页面");
});
this.$element.find("button[name='dataclearbtn']").bind("click", function () {
ToClear(ele, options);
});
}
function ToSearch(obj, options) {
var txtonbj = obj.find("input[name='selecttext']");
var nval = txtonbj.attr("valcode");
var val = encodeURI(nval);
var cars = ["确认", "取消"];
var u = options.url;
if (u.indexOf('?') > -1) {
u = u + "&multiselect=" + options.isMulti + "&val=" + val;
}
else {
u = u + "?multiselect=" + options.isMulti + "&val=" + val;
}
dlog.open("内容选择", cars, "lg", u,
function (res) {
if (res == undefined) {
alert("无数据");
return false;
}
var isarea = jQuery.isArray(res);
if ((options.isMulti == "true") != isarea) {
alert("基础配置不正确,当前设置是否多选:" + options.isMulti + " 返回是否多选数据:" + jQuery.isArray(res));
return false;
}
if (options.isMulti != "true") {
if (res == undefined || res.lable == undefined) {
alert("无数据");
return false;
}
var chaneg = false;
if (ToGetVal(false, obj) != res.code) {
chaneg = true;
}
ToSetVal(false, obj, res.code, res.lable)
if (chaneg) {
//查询控件
if (typeof (options.searchEnd) === "function") {
options.searchEnd(obj);
}
else
{
if (options.searchEnd != undefined && options.searchEnd != "")
{
var objbtns = eval(options.searchEnd);
if (typeof (objbtns) === "function") {
objbtns(obj);
}
}
}
}
}
else {
ToSetVal(true, obj, res)
if (typeof (options.searchEnd) === "function") {
options.searchEnd(obj);
}
else {
if (options.searchEnd != undefined && options.searchEnd != "") {
var objbtns = eval(options.searchEnd);
if (typeof (objbtns) === "function") {
objbtns(obj);
}
}
}
}
//LoadGrid(res);
});
}
function ToClear(obj, options) {
var txtonbj = obj.find("input[name='selecttext']");
var nval = txtonbj.attr("valcode");
txtonbj.val("");
txtonbj.attr("valcode", "");
txtonbj.focus();
if (nval != undefined && nval != "") {
if (typeof (options.clearEnd) === "function") {
options.clearEnd(obj);
} else {
if (options.clearEnd != undefined && options.clearEnd != "") {
var objbtns = eval(options.clearEnd);
if (typeof (objbtns) === "function") {
objbtns(obj);
}
}
}
}
}
function ToGetVal(ismult, obj) {
var txtobj = obj.find("input[name='selecttext']");
//获取值
var va = txtobj.attr("valcode");
if (va == undefined || va == "") {
return "";
} else {
var JsonData = jQuery.parseJSON(va);
if (!ismult) {
return JsonData.code;
}
else {
//字符串编号列
var str = "";
$.each(JsonData, function (index, item) {
str += "," + item.code;
});
if (str == "") {
return "";
}
return str.substring(1);
}
}
//
}
//设置及获取值 _code//可能是jsong数组 必须是[{}] 格式复制
function ToSetVal(ismult, obj, _code, _lable) {
var txtobj = obj.find("input[name='selecttext']");
if (!ismult) {
var json = {};
if (_lable != undefined && _code != undefined) {
json = { code: _code, lable: _lable }
}
else {
if (_code == undefined) {
_code = _lable;
json = { code: _code, lable: _lable }
}
if(_code!=undefined&&_lable==undefined)
{
var isjson = typeof (_code) == "object" && Object.prototype.toString.call(_code).toLowerCase() == "[object object]" && !_code.length
if (isjson) {
json = _code;
_lable = _code.lable;
} else {
_lable = _code;
json = { code: _code, lable: _lable }
}
}
}
txtobj.attr("valcode", JSON.stringify(json));
txtobj.val(_lable);
return "";
}
else {
var Json = [];
if (_lable != undefined && _lable != null && _code.indexOf('code') == -1) {
var codesz = _code.split(",");
var lbsz = _lable.split(",");
for (var i = 0; i < codesz.length; i++) {
var cdstr = codesz[i];
var lbstr = "";
if (lbsz.length > i) {
lbstr = lbsz[i];
}
Json.push({ code: cdstr, lable: lbstr })
}
}
if (Json.length > 0) {
_code = Json;
}
// var josn = [{ code: "123", lable: "1124312" }]
txtobj.attr("valcode", JSON.stringify(_code));
var str = "";
$.each(_code, function (index, item) {
str += "," + item.lable;
});
if (str == "") {
txtobj.val("");
return "";
}
txtobj.val(str.substring(1));
return "";
}
}
$.fn.ival = function (code, lable) {
var ismut = false;
var b = this.find("input[name='selecttext']").attr("ismult");
if (b == "true") {
ismut = true;
}
if (code == undefined && lable == undefined) {
return ToGetVal(ismut, this);
}
else {
return ToSetVal(ismut, this, code, lable);
}
}
$.fn.disabled = function (isdisabled) {
if (isdisabled) {
this.find("button[name='datasearchbtn']").attr("disabled", true);
this.find("button[name='dataclearbtn']").attr("disabled", true);
}
else {
this.find("button[name='datasearchbtn']").removeAttr("disabled")
this.find("button[name='dataclearbtn']").removeAttr("disabled")
}
}
$.fn.search = function () {
var op = this.find("input[name='selecttext']") .attr("txtobj");
ToSearch(this, jQuery.parseJSON(decodeURI(op)));
}
$.fn.clear = function () {
var op = this.find("input[name='selecttext']").attr("txtobj");
ToClear(this, jQuery.parseJSON(decodeURI(op)));
}
$.fn.iTexbox = function (options) {
var beautifier = new createzeone(this, options);
}
})(jQuery, window, document);