057c8d84c97db07afc9709f3eca3344cd6a446b0.svn-base 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * 创建表格
  3. * @param contid
  4. * @param gridData
  5. * @param columsData
  6. * @param callback
  7. */
  8. function createIndexTable(contid,gridData,columsData,callback){
  9. var $dom = $("#"+contid);
  10. var $gridMainCont = $('<div class="gridmaincont"></div>');
  11. $dom.append($gridMainCont);
  12. var $gridCont = $('<div class="gridCont"></div>');
  13. $gridMainCont.append($gridCont);
  14. var tablehtml = '<div class="tablediv"><table class="tablecont" cellspacing="0" >';
  15. tablehtml += '<tr class="tablehead">';
  16. for(var i=0;i<columsData.length;i++){
  17. tablehtml += '<th width="'+columsData[i].width+'"><span>'+columsData[i].display+'</span></th>';
  18. }
  19. tablehtml += '</tr>';
  20. if(gridData.length!=0){
  21. for(var j=0;j<gridData.length;j++){
  22. var rowData = gridData[j];
  23. var idx = gridData.length-j;
  24. tablehtml += '<tr id="row_'+idx+'" class="tablerow">';
  25. for(var k=0;k<columsData.length;k++){
  26. var culum = columsData[k].name;
  27. var renderFun = columsData[k].render;
  28. var editordata = columsData[k].editor;
  29. var value = rowData[culum]==undefined ?"":rowData[culum];;
  30. if(renderFun&&renderFun!=""){
  31. var strhtml = renderFun(idx,rowData,culum,columsData);
  32. tablehtml += '<td id="td_'+culum+'_'+idx+'" >'+strhtml+'</td>';
  33. }else{
  34. if(editordata && editordata.type == "select"){
  35. var editData = editordata.data.list;
  36. value = getKeyFromValue(editData,value);
  37. }
  38. tablehtml += '<td id="td_'+culum+'_'+idx+'" ><span title="'+value+'">'+value+'</span></td>';
  39. }
  40. }
  41. tablehtml += '</tr>';
  42. }
  43. }
  44. tablehtml += '<tr id="row_end" ><td height="100px" colspan="'+columsData.length+'"></td></tr>';
  45. tablehtml += '</table></div>';
  46. $gridCont.append(tablehtml);
  47. if(gridData.length!=0){
  48. for(var j=0;j<gridData.length;j++){
  49. var rowData = gridData[j];
  50. var idx = gridData.length-j;
  51. $("#row_"+idx).data("idx",idx).data("rowData",rowData).data("columsData",columsData);
  52. }
  53. }
  54. $(".tablerow").bind("dblclick", function() { //双击事件
  55. if(!($(this).hasClass("dbclickflag"))){
  56. var idx = parseInt($(this).data("idx"));
  57. var rowData = $(this).data("rowData");
  58. var colData = $(this).data("columsData");
  59. callback(idx,rowData,colData);
  60. }
  61. });
  62. $gridMainCont = null;
  63. $gridCont = null;
  64. };
  65. /**
  66. * 将编辑表单行保存成普通表格行
  67. * @param idx
  68. * @param rowData
  69. * @param columsData
  70. */
  71. function rowInputToSpan(idx,rowData,columsData){
  72. var $trdom = $("#row_"+idx);
  73. $trdom.removeClass("dbclickflag");
  74. for(var k=0;k<columsData.length;k++){
  75. var culum = columsData[k].name;
  76. var renderFun = columsData[k].render;
  77. var editordata = columsData[k].editor;
  78. var value = rowData[culum]==undefined ?"":rowData[culum];
  79. var $tddom = $('#td_'+culum+'_'+idx);
  80. $tddom.html("");
  81. if(renderFun&&renderFun!=""){
  82. var strhtml = renderFun(idx,rowData,culum,columsData);
  83. $tddom.append(strhtml);
  84. }else{
  85. if(editordata && editordata.type == "select"){
  86. var editData = editordata.data.list;
  87. value = getKeyFromValue(editData,value);
  88. }
  89. var html = '<span title="'+value+'" >'+value+'</span>';
  90. $tddom.append(html);
  91. }
  92. }
  93. $trdom.data("rowData",rowData);
  94. }
  95. /***
  96. * 将普通表格行转变成编辑表单行
  97. * @param idx
  98. * @param rowData
  99. * @param columsData
  100. */
  101. function rowSpanToInput(idx,rowData,columsData){
  102. var $trdom = $("#row_"+idx);
  103. $trdom.addClass("dbclickflag");
  104. for(var k=0;k<columsData.length;k++){
  105. var culum = columsData[k].name;
  106. var renderFun = columsData[k].render;
  107. var editordata = columsData[k].editor;
  108. var value = rowData[culum]==undefined ?"":rowData[culum];
  109. var $tddom = $('#td_'+culum+'_'+idx);
  110. $tddom.html("");
  111. if(editordata&&editordata!=""){
  112. if(renderFun&&renderFun!=""){
  113. var strhtml = renderFun(idx,rowData,culum,columsData);
  114. var html = strhtml;
  115. $tddom.append(html);
  116. }else{
  117. var $input = null;
  118. if(editordata.type=="text"){
  119. $input = $('<input id="'+culum+idx+'" name="'+culum+'" style="width: 99%;height:44px;line-height:44px;text-align:center;" type="'+editordata.type+'" value="'+value+'" />');
  120. $tddom.append($input);
  121. }
  122. if(editordata.type=="textarea"){
  123. $input = $('<textarea id="'+culum+idx+'" name="'+culum+'" style="width:99%;height:44px;rows="2">'+value+'</textarea>');
  124. //$input = $('<input id="'+culum+idx+'" name="'+culum+'" style="width: 99%;height:32px;line-height:32px;" type="'+editordata.type+'" value="'+value+'" />');
  125. $tddom.append($input);
  126. }
  127. if(editordata.type=="select"){
  128. //$input = $('<input style="width: 99%;" type="'+editordata.type+'" data="'+editordata.data+'" />');
  129. $input = $('<select id="'+culum+idx+'" name="'+culum+'" editable=false prompt="请选择" selWidth="'+editordata.selWidth+'" ></select>');
  130. $tddom.append($input);
  131. $input.data("data",editordata.data);
  132. $input.render();
  133. $input.setValue(value);
  134. }
  135. $input.render();
  136. }
  137. }else{
  138. if(renderFun&&renderFun!=""){
  139. var strhtml = renderFun(idx,rowData,culum);
  140. var html = strhtml;
  141. $tddom.append(html);
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * 添加新可编辑表单行
  148. * @param idx
  149. * @param rowData
  150. * @param columsData
  151. * @param callback
  152. */
  153. function addNewRows (idx,rowData,columsData,callback){
  154. var $dom = $(".tablerow").eq(0);
  155. //console.log($dom);
  156. if($dom.length==0){
  157. $dom = $("#row_end");
  158. }
  159. idx = idx + 1;
  160. var tablehtml = '<tr id="row_'+idx+'" class="tablerow">';
  161. for(var k=0;k<columsData.length;k++){
  162. var culum = columsData[k].name;
  163. var value = rowData[culum]==undefined ?"":rowData[culum];
  164. tablehtml += '<td id="td_'+culum+'_'+idx+'" ><span title="'+value+'">'+value+'</span></td>';
  165. }
  166. tablehtml += '</tr>';
  167. $dom.before(tablehtml);
  168. $("#row_"+idx).data("idx",idx).data("rowData",rowData).data("columsData",columsData);
  169. rowSpanToInput(idx,rowData,columsData);
  170. $("#row_"+idx).bind("dblclick", function() { //双击事件
  171. if(!($(this).hasClass("dbclickflag"))){
  172. var idx = parseInt($(this).data("idx"));
  173. var rowData = $(this).data("rowData");
  174. var colData = $(this).data("columsData");
  175. callback(idx,rowData,colData);
  176. }
  177. });
  178. }
  179. /**
  180. * 下拉框数据,通过value取key值
  181. * @param editData
  182. * @param value
  183. * @returns
  184. */
  185. function getKeyFromValue(editData,value){
  186. for(var l=0;l<editData.length;l++){
  187. if(editData[l].value == value){
  188. value = editData[l].key;
  189. }
  190. }
  191. return value;
  192. }
  193. /***
  194. * 将json转变成Array数组,并去掉value为空的数据
  195. * @param json
  196. * @returns {Array}
  197. */
  198. function jsonToArray(json){
  199. var result = [];
  200. $.each(json, function(key, value) {
  201. if(value != ""){
  202. result.push(value);
  203. }
  204. });
  205. return result;
  206. }