(
function
($) {
$.fn.ld =
function
(options) {
var
opts;
var
DATA_NAME =
"ld"
;
if
(
typeof
options ==
'string'
) {
if
(options ==
'api'
) {
return
$(
this
).data(DATA_NAME);
}
}
else
{
var
options = options || {};
opts = $.extend(
true
, {}, $.fn.ld.defaults, options);
}
if
($(
this
).size() > 0) {
var
ld =
new
yijs.Ld(opts);
ld.$applyTo = $(
this
);
ld.render();
$(
this
).data(DATA_NAME, ld);
}
return
$(
this
);
}
var
yijs = yijs || {};
yijs.Ld =
function
(options) {
this
.options = options;
this
.$applyTo =
this
.options.applyTo && $(
this
.options.applyTo) ||
null
;
this
.cachePrefix =
"data_"
;
this
.OPTIONS_CLASS =
"ld-option"
;
this
.cache = {};
}
yijs.Ld.prototype = {
render:
function
() {
var
_that =
this
;
var
_opts =
this
.options;
if
(
this
.$applyTo !=
null
&&
this
.size() > 0) {
_opts.style !=
null
&&
this
.css(_opts.style);
this
.load(_opts.defaultLoadSelectIndex, _opts.defaultParentId);
_opts.texts.length > 0 &&
this
.selected(_opts.texts);
this
.$applyTo.each(
function
(i) {
i < _that.size() - 1 && $(
this
).bind(
"change.ld"
, { target: _that, index: i }, _that.onchange);
})
}
return
this
;
},
texts:
function
(ts) {
var
that =
this
;
var
$select =
this
.$applyTo;
var
_arr = [];
var
txt =
null
;
var
$options;
$select.each(
function
() {
txt = $(
this
).children(
'.'
+ that.OPTIONS_CLASS +
':selected'
).text();
_arr.push(txt);
})
return
_arr;
},
size:
function
() {
return
this
.$applyTo.size();
},
css:
function
(style) {
style &&
this
.$applyTo.css(style);
return
this
;
},
load:
function
(selectIndex, parent_id, callback) {
var
_that =
this
;
for
(
var
i = selectIndex; i < _that.size(); i++) {
_that.removeOptions(i);
}
if
(_that.cache[parent_id]) {
_that._create(_that.cache[parent_id], selectIndex);
_that.$applyTo.eq(selectIndex).trigger(
"afterLoad"
);
if
(callback) callback.call(
this
);
}
else
{
var
_ajaxOptions =
this
.options.ajaxOptions;
var
_d = _ajaxOptions.data;
var
_parentIdField =
this
.options.field[
'parent_id'
];
_d[_parentIdField] = parent_id;
_ajaxOptions.data = _d;
_ajaxOptions.success =
function
(data) {
if
(data.success) {
var
_h = _that._getOptionsHtml(data.data);
_that._create(_h, selectIndex);
_that.cache[parent_id] = _h;
_that.$applyTo.eq(selectIndex).trigger(
"afterLoad.ld"
);
if
(callback) callback.call(
this
);
}
}
$.ajax(_ajaxOptions);
}
},
removeOptions:
function
(index) {
this
.$applyTo.eq(index).children(
"."
+
this
.OPTIONS_CLASS).remove();
return
this
;
},
selected:
function
(t, completeCallBack) {
var
_that =
this
;
if
(t &&
typeof
t ==
"object"
&& t.length > 0) {
var
$select =
this
.$applyTo;
_load(_that.options.defaultLoadSelectIndex, _that.options.defaultParentId);
}
function
_load(selectIndex, parent_id) {
_that.load(selectIndex, parent_id,
function
() {
var
id = _selected(selectIndex, t[selectIndex]);
selectIndex++;
if
(selectIndex > _that.size() - 1) {
if
(completeCallBack) completeCallBack.call(
this
);
return
;
}
_load(selectIndex, id);
});
}
function
_selected(index, text) {
var
id = 0;
_that.$applyTo.eq(index).children().each(
function
() {
var
reg =
new
RegExp(text);
if
(reg.test($(
this
).text())) {
$(
this
).attr(
"selected"
,
true
);
id = $(
this
).val();
return
;
}
})
return
id;
}
return
this
;
},
onchange:
function
(e) {
var
_that = e.data.target;
var
index = e.data.index;
var
$target = $(e.target);
var
_parentId = $target.val();
var
_i = index + 1;
_that.load(_i, _parentId);
},
_getOptionsHtml:
function
(data) {
var
_that =
this
;
var
ajaxOptions =
this
.options.ajaxOptions;
var
dataType = ajaxOptions.dataType;
var
field =
this
.options.field;
var
_h =
""
;
_h = _getOptions(data, dataType, field).join(
""
); ;
function
_getOptions(data, dataType, field) {
var
optionClass = _that.OPTIONS_CLASS;
var
aStr = [];
var
id, name;
if
(dataType ==
"json"
) {
$.each(data,
function
(i) {
id = data[i][field.region_id];
name = data[i][field.region_name];
var
_option =
"<option value='"
+ id +
"' class='"
+ optionClass +
"'>"
+ name +
"</option>"
;
aStr.push(_option);
})
}
else
if
(dataType ==
"xml"
) {
$(data).children().children().each(
function
() {
id = $(
this
).find(field.region_id).text();
name = $(
this
).find(field.region_name).text();
var
_option =
"<option value='"
+ id +
"' class='"
+ optionClass +
"'>"
+ name +
"</option>"
;
aStr.push(_option);
})
}
return
aStr;
}
return
_h;
},
_create:
function
(_h, index) {
var
_that =
this
;
this
.removeOptions(index);
this
.$applyTo.eq(index).append(_h);
}
}
$.fn.ld.defaults = {
selects:
null
,
ajaxOptions: {
url:
null
,
type:
'get'
,
data: {},
dataType:
'json'
,
success:
function
() { },
beforeSend:
function
() { }
},
defaultParentId: 0,
defaultLoadSelectIndex: 0,
texts: [],
style:
null
,
change:
function
() { },
field: {
region_id:
"CategoryId"
,
region_name:
"CategoryName"
,
parent_id:
"CategoryParentID"
}
}
})(jQuery);