table.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <title>标题</title>
  6. <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  7. <style type="text/css">
  8. *{margin: 0;padding: 0;font-size: 12px;}
  9. .wrapper{margin-left: 2px;}
  10. .base{width: 472px;height: 70px; margin-left: 10px;margin-top:10px;border: 1px solid #ddd;}
  11. legend{margin-left: 5px; font-weight: bold;font-size: 12px;color: #0066cc;}
  12. fieldset table{margin-left: 5px;float: left;}
  13. select{width: 50px}
  14. .base table{margin-left: 10px;}
  15. .base table tr{height: 25px;}
  16. input{ width: 50px;border:1px solid #ccc;}
  17. .extend {width: 300px;height: 160px;margin:10px;;float: left;border: 1px solid #ddd;}
  18. .extend table{height: 135px;}
  19. #preview{width: 160px;height: 155px;float: left;border: 1px solid #ccc;margin: 16px 0;background-color: #eee}
  20. span.bold{font-weight: bold}
  21. #preview table {margin:3px 5px;}
  22. #preview table td{width: 30px;height: 20px;}
  23. #message{float: left;width: 110px;margin-left: 10px;font-size: 10px;color: red;line-height: 15px}
  24. #messageContent{color: green;margin-top: 5px;}
  25. .extend select{width: 90px}
  26. #bgColor{width: 85px;}
  27. </style>
  28. </head>
  29. <body>
  30. <div class="wrapper">
  31. <fieldset class="base">
  32. <legend>基本信息</legend>
  33. <table>
  34. <tr>
  35. <td width="120"><label for="numRows">行数: </label><input id="numRows" type="text" /> 行</td>
  36. <td>
  37. <label for="width">宽度: </label><input id="width" type="text" />
  38. <label for="widthUnit">度量单位: </label>
  39. <select id="widthUnit">
  40. <option value="%">%</option>
  41. <option value="px">px</option>
  42. </select>
  43. </td>
  44. </tr>
  45. <tr>
  46. <td><label for="numCols">列数: </label><input id="numCols" type="text" /> 列</td>
  47. <td>
  48. <label for="height">高度: </label><input id="height" type="text" />
  49. <label for="heightUnit">度量单位: </label>
  50. <select id="heightUnit">
  51. <option value="%">%</option>
  52. <option value="px">px</option>
  53. </select>
  54. </td>
  55. </tr>
  56. </table>
  57. <div id="message" style="display: none">
  58. <p>温馨提示:</p>
  59. <p id="messageContent">边距最大值不能超过13px!</p>
  60. </div>
  61. </fieldset>
  62. <div>
  63. <fieldset class="extend">
  64. <legend>扩展信息<span style="font-weight: normal;">(可预览)</span></legend>
  65. <table>
  66. <tr>
  67. <td width="60"><span class="bold">表格边框</span>:</td>
  68. <td><label for="border">大小: </label><input id="border" type="text" /> px&nbsp;</td>
  69. <td><label for="borderColor">颜色: </label><input id="borderColor" type="text" /></td>
  70. </tr>
  71. <tr style="border-bottom: 1px solid #ddd">
  72. <td><span class="bold">边距间距</span>:</td>
  73. <td><label for="cellPadding">边距: </label><input id="cellPadding" type="text" /> px&nbsp;</td>
  74. <td><label for="cellSpacing">间距: </label><input id="cellSpacing" type="text" /> px </td>
  75. </tr>
  76. <tr>
  77. <td colspan="3"><span class="bold">表格的背景颜色</span>:
  78. <input id="bgColor" type="text" />
  79. </td>
  80. </tr>
  81. <tr>
  82. <td colspan="3"><span class="bold">表格的对齐方式</span>:
  83. <select id="align">
  84. <option value="">默认</option>
  85. <option value="center">居中</option>
  86. <option value="left">居左</option>
  87. <option value="right">居右</option>
  88. </select>
  89. </td>
  90. </tr>
  91. <tr>
  92. <td colspan="3">
  93. <span class="bold">边框设置作用于</span>:
  94. <select id="borderType">
  95. <option value="0">仅表格</option>
  96. <option value="1">所有单元格</option>
  97. </select>
  98. </td>
  99. </tr>
  100. </table>
  101. </fieldset>
  102. <div id="preview">
  103. <table border="1" borderColor="#000" cellpadding="0" cellspacing="0" style="border-collapse: collapse;">
  104. <tr><td>这</td><td>是</td><td>用</td></tr>
  105. <tr><td>来</td><td>预</td><td>览</td></tr>
  106. <tr><td>的</td><td></td><td></td></tr>
  107. </table>
  108. </div>
  109. </div>
  110. </div>
  111. <script type="text/javascript" src="../internal.js"></script>
  112. <script type="text/javascript" src="table.js"></script>
  113. <script type="text/javascript">
  114. var inputs = document.getElementsByTagName('input'),
  115. selects=document.getElementsByTagName('select');
  116. //ie给出默认值
  117. for(var i=0,ci;ci=inputs[i++];){
  118. switch (ci.id){
  119. case 'numRows':
  120. case 'numCols':
  121. ci.value = 5;
  122. break;
  123. case 'bgColor':
  124. ci.value = '#FFFFFF';
  125. break;
  126. case 'borderColor':
  127. ci.value = '#000000';
  128. break;
  129. case 'border':
  130. ci.value = 1;
  131. break;
  132. case 'cellSpacing':
  133. case 'cellPadding':
  134. ci.value = '0';
  135. break;
  136. default:
  137. }
  138. }
  139. for(var i=0,ci;ci=selects[i++];){
  140. ci.options[0].selected = true;
  141. }
  142. var state = editor.queryCommandState("edittable");
  143. if(state == 0){
  144. var range = editor.selection.getRange(),
  145. table = domUtils.findParentByTagName(range.startContainer,'table',true);
  146. if(table){
  147. var numRows = table.rows.length,cellCount=0;
  148. //列取最大数
  149. for(var i=0,ri;ri=table.rows[i++];){
  150. var tmpCellCount = 0;
  151. for(var j=0,cj;cj=ri.cells[j++];){
  152. if(cj.style.display != 'none'){
  153. tmpCellCount++;
  154. }
  155. }
  156. cellCount = Math.max(tmpCellCount,cellCount)
  157. }
  158. domUtils.setAttributes($G('numRows'),{
  159. 'value':numRows,
  160. 'disabled':true
  161. });
  162. domUtils.setAttributes($G('numCols'),{
  163. 'value':cellCount,
  164. 'disabled':true
  165. });
  166. $G("cellPadding").value = table.getAttribute("cellPadding") || '0';
  167. $G("cellSpacing").value = table.getAttribute("cellSpacing") || '0';
  168. var value = table.getAttribute('width');
  169. value = !value ? ['',table.offsetWidth]:/%$/.test(value) ? value.match(/(\d+)(%)?/) : ['',value.replace(/px/i,'')];
  170. $G("width").value = value[1];
  171. $G('widthUnit').options[value[2] ? 0:1].selected = true;
  172. value = table.getAttribute('height');
  173. value = !value ? ['','']:/%$/.test(value) ? value.match(/(\d+)(%)?/) : ['',value.replace(/px/i,'')];
  174. $G("height").value = value[1];
  175. $G('heightUnit').options[value[2]?0:1].selected = true;
  176. $G('borderColor').value = (table.getAttribute('borderColor')||"#000000").toUpperCase();
  177. $G("border").value = table.getAttribute("border");
  178. for(var i=0,ip,opts= $G("align").options;ip=opts[i++];){
  179. if(ip.value == table.getAttribute('align')){
  180. ip.selected = true;
  181. break;
  182. }
  183. }
  184. $G("borderType").options[table.getAttribute('borderType') == '1' ? 1: 0].selected = true;
  185. $G("bgColor").value = (table.getAttribute("bgColor")||"#FFFFFF").toUpperCase();
  186. createTable();
  187. }
  188. }else{
  189. $focus($G("numRows"));
  190. }
  191. init();
  192. domUtils.on($G("width"),"keyup",function(){
  193. var value = this.value;
  194. if(value>100){
  195. $G("widthUnit").value="px";
  196. }
  197. });
  198. domUtils.on($G("height"),"keyup",function(){
  199. var value = this.value;
  200. if(value>100){
  201. $G("heightUnit").value="px";
  202. }
  203. });
  204. dialog.onok = function(){
  205. for(var i=0,opt={},ci;ci=inputs[i++];){
  206. switch (ci.id){
  207. case 'numRows':
  208. case 'numCols':
  209. case 'height':
  210. case 'width':
  211. if(ci.value && !/^[1-9]+[0-9]*$/.test(ci.value)){
  212. alert("请输入正确的数值!");
  213. $focus(ci);
  214. return false;
  215. }
  216. break;
  217. case 'cellspacing':
  218. case 'cellpadding':
  219. case 'border':
  220. if(ci.value && !/^[0-9]*$/.test(ci.value)){
  221. alert("请输入正确的数值!");
  222. $focus(ci);
  223. return false;
  224. }
  225. break;
  226. case 'bgColor':
  227. case 'borderColor':
  228. if(ci.value && !/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(ci.value)){
  229. alert("请输入正确的颜色值");
  230. $focus(ci);
  231. return false;
  232. }
  233. break;
  234. default:
  235. }
  236. opt[ci.id] = ci.value || (ci.id == 'border' ? 0 : '')
  237. }
  238. for(var i=0,ci;ci=selects[i++];){
  239. opt[ci.id] = ci.value.toUpperCase()
  240. }
  241. editor.execCommand(state == -1 ? 'inserttable' : 'edittable',opt );
  242. };
  243. </script>
  244. </body>
  245. </html>