var
selectData={};
$.fn.selectDataFun=
function
(json) {
selectData[$(this).attr(
"id"
)]={thisDom:null,initData:null,jsonData:null,htmlText:null,
isShow:true,selectElements:true,overStat:true,checkedElementIds:''};
thisSelectFun($(this)).thisDom=$(this);
thisSelectFun($(this)).initData=json;
setFieldTypeFun($(this));
getSelectDataFun($(this));
thisSelectFun($(this)).htmlText = createSelectFun($(this));
callMethodFun($(this));
}
function
thisSelectFun(thisD) {
if
(thisD.attr(
"id"
)==''){
alert(
"id值为空"
);
return
null;
}
return
selectData[thisD.attr(
"id"
)]
}
function
setFieldTypeFun(thisD){
var
thisId=thisD.attr(
"id"
);
var
fields = selectData[thisId].initData;
var
title=fields.title;
var
inputId=fields.inputId;
var
selectHtml='<p
class
=
"layui-form-select"
>'+'<p
class
=
"layui-select-title"
>'+
'<input type=
"hidden"
id=
"'+inputId+'"
name=
"'+inputId+'"
/>'+
'<span>'+title+':</span>'+' '+'<input id=
"'+thisD.attr("
id
")+'Search"
value=
""
hiddenValue=
""
class
=
"form-control input-sm"
type=
"text"
>'+
'<i
class
=
"layui-edge"
></i>'+'</p>'+'<dl
class
=
"layui-anim layui-anim-upbit"
id=
"'+thisD.attr("
id
")+'dl"
style=
""
></dl>'+
'</p>';
thisD.append(selectHtml);
}
function
getSelectDataFun(thisD) {
var
fields = selectData[thisD.attr(
"id"
)].initData;
if
(fields.ajaxUrl.hasOwnProperty('url')){
thisSelectFun(thisD).jsonData=ajaxFunss(fields.ajaxUrl);
}
else
{
if
($.isEmptyObject(thisSelectFun(thisD).jsonData)){
thisSelectFun(thisD).jsonData={ code: 0, msg:
"获取成功"
,
count
: 0,data:
new
Array()};
}
}
}
function
ajaxFunss(json) {
json['async']=false;
json['dataType']='json';
json['type']='post';
var
layerLoadIndex = layer.load(1);
var
resultData=$.ajax(json);
layer.close(layerLoadIndex);
if
(resultData.status==200){
return
resultData.responseJSON;
}
else
{
return
null;
}
}
function
createSelectFun(thisD) {
var
json=thisSelectFun(thisD).jsonData;
var
html='';
for
(
var
n in json){
html+='<dd lay-value=
"'+json[n].id+'"
class
=
""
>'+json[n].name+'</dd>';
}
var
id=thisD.attr(
"id"
)+'dl';
$(
"#"
+id).append(html);
return
html;
}
function
callMethodFun(thisD) {
var
iDom=thisD.find(
".layui-edge"
).eq(0);
var
pDom = thisD.find(
".layui-form-select"
).eq(0);
iDom.click(
function
() {
if
(thisSelectFun(thisD).isShow){
thisSelectFun(thisD).isShow=false;
pDom.addClass(
"layui-form-selected"
);
}
else
{
thisSelectFun(thisD).isShow=true;
pDom.attr(
"class"
,
"layui-form-select"
);
}
});
var
dlDom=thisD.find(
"dl"
).eq(0);
var
searchId = thisD.attr(
"id"
)+'Search';
dlDom.on(
"click"
,'dd',
function
() {
if
(thisSelectFun(thisD).initData.selectType) {
if
(thisSelectFun(thisD).selectElements) {
thisSelectFun(thisD).selectElements = false;
$(this).addClass(
"layui-this"
);
var
text = $(
"#"
+ searchId).val();
var
selectText = $(this).text() +
","
;
var
checkedId = $(this).attr(
"lay-value"
) +
","
;
if
(text.indexOf(selectText) != -1) {
return
;
}
thisSelectFun(thisD).checkedElementIds += checkedId;
$(
"#"
+ searchId).val(text + selectText);
}
else
{
thisSelectFun(thisD).selectElements = true;
$(this).attr(
"class"
,
""
);
var
val = $(this).text() +
","
;
var
checkedId = $(this).attr(
"lay-value"
) + ',';
var
text = $(
"#"
+ searchId).val().replace(val,
""
);
$(
"#"
+ searchId).val(text);
thisSelectFun(thisD).checkedElementIds = thisSelectFun(thisD).checkedElementIds.replace(checkedId,
""
);
}
}
else
{
var
ddDom=thisD.find(
".layui-this"
).eq(0);
ddDom.attr(
"class"
,
""
);
$(this).addClass(
"layui-this"
);
var
selectText = $(this).text();
var
checkedId = $(this).attr(
"lay-value"
);
thisSelectFun(thisD).checkedElementIds = checkedId;
$(
"#"
+ searchId).val(selectText);
pDom.attr(
"class"
,
"layui-form-select"
);
}
var
hiddenId=selectData[thisD.attr(
"id"
)].initData.inputId;
$(
"#"
+hiddenId).val(thisSelectFun(thisD).checkedElementIds.substring(0,thisSelectFun(thisD).checkedElementIds.length-1));
})
dlDom.mouseover(
function
() {
thisSelectFun(thisD).overStat=false;
});
dlDom.mouseout(
function
() {
thisSelectFun(thisD).overStat=true;
});
$(
"body"
).mouseup(
function
() {
if
(thisSelectFun(thisD).overStat==true){
pDom.attr(
"class"
,
"layui-form-select"
);
}
});
var
searchDom = thisD.find(
"#"
+searchId).eq(0);
searchDom.keyup(
function
() {
searchFunssss(thisD,searchId,pDom,dlDom);
});
}
function
searchFunssss(thisD,searchId,pDom,dlDom) {
var
val=$(
"#"
+searchId).val();
if
(val.length>0){
var
conText='';
var
searchStats=false;
var
htmlText=thisSelectFun(thisD).htmlText;
dlDom.children().each(
function
() {
var
thisText=$(this).text();
var
thisDom='<dd lay-value=
"'+$(this).attr("
lay-value
")+'"
class
=
""
>'+thisText+'</dd>';
if
(val==thisText){
conText+=thisDom;
htmlText=htmlText.replace(thisDom,
""
);
searchStats=true;
var
searchId = thisD.attr(
"id"
)+'Search';
$(
"#"
+searchId).val(
""
);
}
});
htmlText=conText+htmlText;
dlDom.children().remove();
dlDom.append(htmlText);
if
(searchStats){
pDom.addClass(
"layui-form-selected"
);
}
}
}