|
как организовать код
#38229408
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
|
вот есть такой код как его лучше организовать
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227.
jQuery().ready(function () {
var report= {
tableid:"#mytable",
col_linked_select_id:'#selscol',
row_linked_select_id:'#selsrow',
head:{
seq:-1
},
body:{
seq:-1,
rows:{}
},
addMetaCol : function(){
var index=++this.head.seq;
var coltext ='Column'+index;// jQuery('#columnName').val();
this.head[index]={
id:index,
text:coltext,
rs:1,
merge:[],
pcnt:0,
par:{}
};
return this.head[index];
},
addColumn:function(){
var col = this.addMetaCol();
jQuery(this.col_linked_select_id).append( $('<option value="'+col.id+'">'+col.id+' - '+col.text+'</option>'));
this.addHtmlHead();
},
deleteColumn:function(arg){
var colidx =arg||jQuery(this.col_linked_select_id+" option:selected").val();
for(var key in this.head[colidx].par){
var par = this.head[colidx].par[key];
this.head[par.s].merge[par.n].numCol--;
}
if (this.head[colidx].merge.length > 0){
for(var i=0;i<this.head[colidx].merge.length;i++){
var cii =this.head[colidx].merge[i];
if (cii!=undefined){
for(var key in this.head) {
if (key != 'seq'){
var tp = this.head[key].par[colidx+'_'+i];
if(tp != undefined ){
delete this.head[key].par[colidx+'_'+i];
this.head[key].pcnt--;
}
}
}
}
}
}
delete this.head[colidx];
jQuery(this.col_linked_select_id+" [value='"+colidx+"']"). remove();
this.addHtmlHead();
},
addHtmlHead : function(){
var th,headhtml=[],level=0;
var colIndex =jQuery(this.col_linked_select_id+" option:selected").val();
for (var key in this.head) {
if (key != "seq"){
th = this.head[key];
var mlen = th.merge.length;
level = th.pcnt;
for(var m=mlen;m>1;m--){
if (th.merge[m]!=undefined){
if (headhtml[level] == undefined){
headhtml[level]='<tr>';
}
headhtml[level]+='<th colspan="'+th.merge[m].numCol+'">'+th.merge[m].mTxt+'</th>';
level++;
}
}
if (headhtml[level] == undefined) {
headhtml[level]='<tr>';
}
var hover =colIndex == th.id ? 'class="myhover"':'';
headhtml[level]+='<th rowspan id="'+th.id+'" '+hover+'>'+th.text+'</th>';
}
}
jQuery(this.tableid+" > thead").empty();
var htr = "";
for (var i = 0; i < headhtml.length;i++) {
htr +=(i+1!=headhtml.length)? headhtml[i].replace(/rowspan/g,'rowspan="20"'):headhtml[i].replace(/rowspan/g,'')+'</tr>';
//jQuery("#mytable>thead").append(tr+"</tr>");
}
jQuery(this.tableid+" > thead").html(htr);
},
mergeColumns:function(start,number,text){
var sCol=parseInt(start,10),
nCol=parseInt(number,10),
mText=text;
if (this.head[sCol].merge[nCol] == undefined){
var nc = nCol-1;
for(var key in this.head){
var ikey = parseInt(key,10);
if ( (ikey > sCol) && ( nc > 0) )
{
this.head[ikey].pcnt++;
this.head[ikey].par[sCol+'_'+nCol]=({
s:sCol,
n:nCol
});
nc--;
}
}
}
this.head[sCol].merge[nCol]={
mTxt:mText,
numCol:nCol
};
this.addHtmlHead();
},
addMetaCell : function(arg,arg1){
var idx =arg;
if (idx == undefined || arg1){
idx=++this.body.seq;
this.body.rows[idx] = {
ridx:idx,
cells:[]
}
}
var cellidx =this.body.rows[idx].cells.length;
this.body.rows[idx].cells.push({
cidx:cellidx,
celltxt:'Cell'+cellidx
});
return idx+'_'+cellidx;
},
addCell:function(innewrow){
var ridx =jQuery(this.row_linked_select_id+" option:selected").attr('row'),row,btr ='',seloptions='',ct,td;
var cellkey = this.addMetaCell(ridx,innewrow);
for(var key in report.body.rows){
row = report.body.rows[key];
seloptions+='<optgroup label="'+key+'">';
td='';
for(var i = 0;i<row.cells.length;i++){
if (row.cells[i] != undefined){
ct =row.cells[i].celltxt;
var cellid = key+'_'+row.cells[i].cidx;
td+='<td id="'+cellid+'" order="'+row.cells[i].cidx+'">'+ct+'</td>';
seloptions+='<option value="'+cellid+'" row="'+key+'" order="'+row.cells[i].cidx+'">'+ct+'</option>';
}
}
btr+=td.length>0?"<tr>"+td+"</tr>":"";
seloptions+='</optgroup>';
}
jQuery(this.row_linked_select_id).empty().html(seloptions);
$(this.row_linked_select_id+" [value='"+cellkey+"']").attr("selected", "selected");
jQuery(this.tableid+">tbody").empty().html(btr);
jQuery("td#"+cellkey).addClass('myhover');
},
deleteCell:function(){
var selobj=jQuery(this.row_linked_select_id+" option:selected");
var ckey = selobj.val(),
rowidx = selobj.attr('row'),
order = selobj.attr('order');
delete this.body.rows[rowidx].cells[order];
jQuery(this.row_linked_select_id+" :selected").remove();
jQuery("td#"+ckey).remove();
}
};
$(report.tableid)
.click(function(e){
var eid = e.target.id;
if (e.target.tagName=='TD'){
jQuery(report.tableid+" > tbody > tr > td.myhover").removeClass('myhover');
$(report.row_linked_select_id+" [value='"+eid+"']").attr("selected", "selected");
jQuery("td#"+eid).addClass('myhover');
}
if (e.target.tagName=='TH'){
jQuery(report.tableid+" > thead > tr > th.myhover").removeClass('myhover');
$(report.col_linked_select_id+" [value='"+eid+"']").attr("selected", "selected");
jQuery("th#"+eid).addClass('myhover');
}
});
jQuery("#addColumn").click(function (){
report.addColumn();
});
jQuery("#addCell").click(function (){
report.addCell((jQuery("#check1").attr('checked') == 'checked'));
});
jQuery("#delCell").click(function (){
report.deleteCell();
});
jQuery("#delColumn").click(function (){
report.deleteColumn();
});
jQuery("#mergeCol").click(function (){
var sCol=parseInt(jQuery("#selscol :selected").val(),10),
nCol=parseInt(jQuery('#toCol').val(),10),
mText=jQuery('#mergeColText').val();
report.mergeColumns(sCol, nCol, mText);
});
jQuery("#json").click(function (){
var jsonstring = jQuery.stringify(report);
alert(jsonstring);
});
jQuery("#confirm").click(function (){
var colIndex =jQuery("#selscol option:selected").val();
report.head[colIndex].text=jQuery('#columnName').val();
jQuery("#selscol option:selected").text(report.head[colIndex].id+' - '+report.head[colIndex].text);
report.addHtmlHead();
});
jQuery("#selscol").change(function(){
jQuery("#mytable > thead > tr > th.myhover").removeClass('myhover');
var colIndex =jQuery("#selscol option:selected").val();
jQuery("th#"+colIndex).addClass('myhover');
});
jQuery("#selsrow").change(function(){
jQuery("#mytable > tbody > tr > td.myhover").removeClass('myhover');
var cellkey =jQuery("#selsrow option:selected").val();
jQuery("td#"+cellkey).addClass('myhover');
});
});
|
|
|