jProviderData
///
$j = jQuery.noConflect();
(함수 ($j) {
$j.providerData={
defaultSettings: {
select_Span_Items: false,
select_TD_Items: false,
select_LI_Items: false
},
init:function(options){
opts = $j.extend({}, $j.providerData.defaultSettings, options)
singleItemJsonFormat='"{0}":"{1}" ';
divItemFormat='{{0}}';
tableItemFormat='{0}';
},
getDataName:function(item){
var dataName = item.attr("dataname");
if(typeof dataName === 'undefine'||dataName==""){
dataName = item.attr("Id")
}
if(typeof dataName === '정의되지 않음'||dataName==""){
dataName=item.text();
}
if(typeof dataName === '정의되지 않음'| |dataName==""){
dataName=item[0].innerText;
}
return dataName;
},
getData:function(selector){
// var selector=opts.selector;
var items;
if(typeof selector!=="object"){
items=$j(selector)
}
else >items=selector;
}
var retJsonValue="";
var subSelector=":input";
if (opts.select_Span_Items) {
subSelector = 하위선택기 ",span";
}
if (opts.select_LI_Items) {
subSelector = subSelector ",li";
}
if(opts.select_TD_Items){
subSelector = subSelector ",td";
}
$j.each(items,function(i,item){
var t=$j(this);
var dataName=$j.providerData.getDataName(t );
//return:'{"dataname":"value"}'
if($j.util.isInput(t)){
var value=t.val() ;
var itemStr= String.format(singleItemJsonFormat,dataName,value);
retJsonValue=retJsonValue itemStr ","
retJsonValue=retJsonValue.trimEnd(",")
//return:'{"dataname1":"value1","dataname2":"value2"}'
else if($j.util.isDiv(t)){
var subItems = t .find(subSelector);
var subStr = "";
$j.each(subItems, function (i, subItem) {
//递归调用 处理子元素
var tempSubItemStr = $j .providerData.getData("#" $j(this).attr("id"));
subStr = subStr "," tempSubItemStr;
})
subStr=subStr.trimStart("," );
retJsonValue=String.format(divItemFormat,subStr)
}
//return:'[{"dataname1":"value1","dataname2":"value2"}, {"dataname1":"value3","dataname2":"value4"}]'
else if($j.util.isTable(t)){
var trItems=t.find("TR:gt (0)");
//迭代处理Table中的每一行
$j.each(trItems,function(i,trItem){
//处理行,找流行中的要收集数据的元素
var subItems =$j(this).find(subSelector);
var subStr = ""; var tempSubItemStr = $j.providerData.getData("#" $j(this).attr("id"))
subStr = subStr "," tempSubItemStr
}
else{
//如果是TD元素,且他里面不包含INPUT元素
if( $j.util.isTD($j(this)) && ($j(this).find(":input ").length==0)){
var tempSubItemStr = $j.providerData.getData($j(this));
subStr = subStr "," tempSubItemStr;
}
}
});
subStr=subStr.trimStart(",");
retJsonValue= retJsonValue String.format(divItemFormat,subStr) ",";
});
retJsonValue=retJsonValue.trimEnd(",");
retJsonValue="[" retJsonValue "]";
}
//return:'{"dataname":"value"}'
else if($j.util.isLi(t)){
var value=t. 텍스트();
var itemStr= String.format(singleItemJsonFormat,dataName,value);
retJsonValue=retJsonValue itemStr ",";
retJsonValue=retJsonValue.trimEnd(",");
}
//return:'{"dataname":"value"}'
else if($j.util.isTD(t)){
var 값 =t.text();
// var rowIndex = t.parent().prevAll().length;
//获取当前列的列索引
var colIndex = t.prevAll().length;
//dataName이 유형에 맞는 유형의 dataName 유형을 결정합니다.
var thItem=t.parent().parent().parent().find(" th:eq("colIndex")");
if(thItem.length==1){
dataName=$j.providerData.getDataName(thItem);
}
var itemStr= String.format(singleItemJsonFormat,dataName,value);
retJsonValue=retJsonValue itemStr ",";
retJsonValue=retJsonValue.trimEnd(",");
}
});
retJsonValue를 반환합니다.
}
};
jProviderData=function(옵션){
$j.providerData.init(옵션);
$j.providerData.getData(options.selector)를 반환합니다.
};
})(jQuery);
复代码
代码如下:
String.prototype.trimStart = function(trimStr){
if(!trimStr){return this;}
var temp = this;
while(true){
if(temp.substr(0,trimStr.length)!=trimStr){
break;
}
temp = temp.substr(trimStr.length);
}
반품 온도;
};
String.prototype.trimEnd = function(trimStr){
if(!trimStr){return this;}
var temp = this;
while(true){
if(temp.substr(temp.length-trimStr.length,trimStr.length)!=trimStr){
break;
}
temp = temp.substr(0,temp.length-trimStr.length);
}
반품 온도;
};
String.prototype.trim = function(trimStr){
var temp = TrimStr;
if(!trimStr){temp=" ";}
return this.trimStart(temp).trimEnd(temp);
};
String.format = function () {
if (arguments.length == 0) {
return null;
}
var str = 인수[0];
for (var i = 1; i < 인수.길이; i ) {
var re = new RegExp('\{' (i - 1) '\}', 'gm');
str = str.replace(re, 인수[i]);
}
return str;
}
(function ($j) {
$j.util = {
_compareTagName : function (item, tagName) {
return item.attr("tagName").toUpperCase( ) == tagName.toUpperCase();
},
isTable: function (item) {
return $j.util._compareTagName(item, "TABLE")},
isDiv: 함수(항목) {
return $j.util._compareTagName(item, "DIV")
},
isInput: 함수(항목) {
return $j.util._compareTagName(item, "INPUT");
},
isSpan: function (item) {
return $j.util._compareTagName(item, "SPAN"); },
isLi: 함수(항목) {
return $j.util._compareTagName(item, "LI")
},
isTD: 함수(항목) {
return $ j.util._compareTagName(item, "TD");
}
})(jQuery);