combobox 与后台数据不一致匹配问题
$('#projID').combobox({
url: '/json/project/getAll',
valueField:'PROJECT_ID',
textField:'PROJECT_NAME',
editable:false,
onLoadSuccess : onLoadSuccess
});
function onLoadSuccess(){
var target = $(this);
var data = target.combobox("getData");
var options = target.combobox("options");
if(data && data.length>0){
var fs = data[0];
target.combobox("setValue",fs[options.valueField]);
}
}
combotree 后台数据提前设置
/**
* json格式转树状结构
* @param {json} json数据
* @param {String} id的字符串
* @param {String} 父id的字符串
* @param {String} children的字符串
* @param {String} posterity(所有后代ID)的字符串名
* @return {Array} 数组
*/
exports.transArrayToTree=function(a, idStr, pidStr, chindrenStr,posterityStr,textStr){
var r = [], hash = {}, id = idStr,text=textStr, pid = pidStr, children = chindrenStr, i = 0, j = 0, len = a.length,posterity=(posterityStr||'posterity');
// 排序
a.sort(function(pre,nxt){
return nxt[id]-pre[id];
})
for(; i < len; i++){
hash[a[i][id]] = a[i];
hash[a[i][id]].id=a[i][id];
hash[a[i][id]].text=a[i][text];
}
for(; j < len; j++){
var aVal = a[j], hashVP = hash[aVal[pid]];
if(hashVP){
!hashVP[children] && (hashVP[children] = []);
hashVP[children].push(aVal);
hashVP.state='closed';
!hashVP[posterity] && (hashVP[posterity] = []);
hashVP[posterity].push(aVal[id]);
if (aVal[posterity]) {
for (var i = 0; i < aVal[posterity].length; i++) {
hashVP[posterity].push(aVal[posterity][i]);
};
};
}else{
r.push(aVal);
}
}
return r;
}