最近自己封装了个JS脚本,用来创建和操作Table

基于JQuery封装的Table操作脚本

  1 /**
  2 依赖JQuery
  3
  4
  5 **/
  6
  7 (function () {
  8     var Table = window.Table = function (rowCount, columnCount, width, height, rowHeight) {
  9         this.rowCount = rowCount;
 10         this.columnCount = columnCount;
 11         this.width = width;
 12         this.height = height;
 13         this.rowHeight = rowHeight;
 14         this.context;
 15         this.tableCss;
 16         this.rowCss;
 17         this.columnCss;
 18         this.onTdCreated = function () { };
 19         this.onTdMouseDown = function (e) { };
 20         this.onTrCreated = function () { };
 21         this.onTrMouseDown = function (e) { };
 22     };
 23     Table.prototype.CreateTable = function (target) {
 24         this.context = $("<table style=‘width:auto‘></table>");
 25
 26         if (this.tableCss)
 27             this.context.addClass(this.tableCss);
 28         if (this.width)
 29             this.context.css("width", this.width);
 30         if (this.height)
 31             this.context.css("height", this.height);
 32
 33         if (target) {
 34             this.context.appendTo(target);
 35         }
 36
 37         for (var index = 0; index < this.rowCount; index++) {
 38             CreatedRow.call(this, this.context);
 39         }
 40
 41         return this.context;
 42     };
 43     Table.prototype.GetRowCount = function () {
 44         return this.rowCount;
 45     };
 46     Table.prototype.GetColumnCount = function () {
 47         return this.columnCount;
 48     };
 49     Table.prototype.AddRow = function () {
 50         var tr = CreatedRow.call(this, this.context);
 51         if (this.rowHeight)
 52             tr.css("height", this.rowHeight);
 53
 54         this.rowCount++;
 55         return tr;
 56     };
 57     Table.prototype.AddColumn = function () {
 58         var rows = $(this.context).children().children();
 59         for (var index = 0; index < rows.length; index++) {
 60             CreateTd.call(this, $(rows[index]));
 61         }
 62         this.columnCount++;
 63     };
 64     Table.prototype.RemoveRow = function (row) {
 65         $(row).remove();
 66         this.rowCount--;
 67         return $(row);
 68     }
 69     Table.prototype.RemoveColumn = function (column) {
 70         var tds = $(column).parent().children();
 71         var columnIndex = GetColumnIndex(tds, column);
 72         var rows = this.context.children().children()
 73         for (var index = 0; index < rows.length; index++) {
 74             var tr = rows[index];
 75             var tdIndex = GetTdIndex(tr, columnIndex);
 76             var td = $(tr).children()[tdIndex];
 77             if (parseInt($(td).attr("colspan") || 1) > 1) {
 78                 $(td).attr("colspan", parseInt($(td).attr("colspan") - 1));
 79             } else {
 80                 $(td).remove();
 81             }
 82         }
 83         this.columnCount--;
 84     }
 85     Table.prototype.MergeColumns = function (column, value) {
 86         value = parseInt(value);
 87         if (value <= 0) {
 88             throw "列数必须大于0!";
 89             return;
 90         }
 91
 92         var tds = $(column).nextAll(‘td‘);
 93         if (value > tds.length) value = tds.length;
 94         var colspan = parseInt($(column).attr("colspan") || 1);
 95
 96         for (var index = 0; index < value; index++) {
 97             colspan += parseInt($(tds[index]).attr("colspan") || 1);
 98             $(tds[index]).remove();
 99         }
100
101         $(column).attr("colspan", colspan);
102     }
103     Table.prototype.SplitColumn = function (column, value) {
104         value = parseInt(value);
105         if (value <= 0) {
106             throw "列数必须大于0!";
107             return;
108         }
109         var colspan = parseInt($(column).attr("colspan") || 1);
110
111         if (colspan <= 1) {
112             throw "不能拆分单元格!";
113             return;
114         }
115
116         if (value > colspan) {
117             throw "输入数字无效!";
118             return;
119         }
120
121         $(column).attr("colspan", colspan - parseInt(value) + 1);
122         var tr = $(column).parent();
123         for (var index = 0; index < value - 1; index++) {
124             CreateTd.call(this,tr);
125         }
126     }
127     Table.prototype.AddRowFrom = function (row, position) {
128         var tr = CreatedRow();
129         for (var index = 0; index < this.columnCount; index++) {
130             CreateTd(tr);
131         }
132         switch (position) {
133             case "north":
134                 $(row).before(tr);
135                 break;
136             case "south":
137                 $(row).after(tr);
138                 break;
139             default:
140                 $(row).after(tr);
141                 break;
142         }
143         this.rowCount++;
144         return tr;
145     }
146     Table.prototype.AddColumnFrom = function (column, position) {
147         var columnIndex = GetColumnIndex($(column).parent().children(), column);
148         if (columnIndex < 0) {
149             throw "获取当前列失败!";
150             return;
151         }
152         var rows = this.context.children().children();
153         for (var index = 0; index < rows.length; index++) {
154             row = rows[index];
155             var td = CreateTd();
156             var tdIndex = GetTdIndex(row, columnIndex);
157             switch (position) {
158                 case "west":
159                     $($(row).children()[tdIndex]).before(td);
160                     break;
161                 case "east":
162                     $($(row).children()[tdIndex]).after(td);
163                     break;
164                 default:
165                     $($(row).children()[tdIndex]).after(td);
166                     break;
167             }
168         }
169         this.columnCount++;
170     }
171
172     function CreatedRow(target) {
173         var tr;
174         if (this.rowCss)
175             tr = $("<tr class=‘" + this.rowCss + "‘></tr>");
176         else
177             tr = $("<tr></tr>");
178
179         if (target) {
180             tr.appendTo(target);
181         }
182
183         for (var index = 0; index < this.columnCount; index++) {
184             CreateTd.call(this, tr);
185         }
186
187         if ($.isFunction(this.onTrMouseDown))
188             tr.bind("mousedown ", this.onTrMouseDown);
189
190         if ($.isFunction(this.onTrCreated))
191             this.onTrCreated.call(tr);
192
193         return tr;
194     };
195
196     function CreateTd(target) {
197         var td;
198         if (this.columnCss)
199             td = $("<td class=‘" + this.columnCss + "‘></td>");
200         else
201             td = $("<td></td>");
202
203         if (target) {
204             td.appendTo(target);
205         }
206
207         if ($.isFunction(this.onTdMouseDown))
208             td.bind("mousedown ", this.onTdMouseDown);
209
210         if ($.isFunction(this.onTdCreated))
211             this.onTdCreated.call(td);
212
213         return td;
214     }
215
216     function GetColumnIndex(tds, td) {
217         var tdIndex = $.inArray(td, tds);
218         var columnIndex = 0;
219         for (var index = 0; index <= tdIndex; index++) {
220             columnIndex += parseInt($(tds[index]).attr("colspan") || 1);
221         }
222         return columnIndex;
223     }
224
225     function GetTdIndex(tr, columnIndex) {
226         var tds = $(tr).children();
227         var ColumnIndex = 0;
228         for (var index = 0; index < tds.length; index++) {
229             ColumnIndex += parseInt($(tds[index]).attr("colspan") || 1);
230             if (ColumnIndex >= columnIndex)
231                 return index;
232         }
233     }
234 }());

时间: 2024-07-31 19:53:19

最近自己封装了个JS脚本,用来创建和操作Table的相关文章

前端性能之非阻塞加载js脚本

SCRIPT标签的阻塞行为会对页面的性能产生影响,这是因为浏览器在下载脚本.解析.执行的过程中不会同时做其他事情,比如渲染页面.响应用户事件等.之所以这样做是因为正在执行的JavaScript代码可能会改变页面元素.修改样式.添加或者删除事件等各种操作,以及最关键的脚本之间的依赖性,浏览器必须等待当前执行的脚本执行完成之后再进行后续操作. 脚本阻塞 两种加载方式 HTML页面中的JavaScript脚本有两种方式加入 - 使用script标签内联到HTML页面,页面按从上到下的顺序执行到scri

(转)优化js脚本设计,防止浏览器假死

在Web开发的时候经常会遇到浏览器不响应事件进入假死状态,甚至弹出“脚本运行时间过长“的提示框,如果出现这种情况说明你的脚本已经失控了,必须进行优化. 为什么会出现这种情况呢,我们先来看一下浏览器的内核处理方式: 浏览器的内核是多线程的,它们在内核制控下相互配合以保持同步,一个浏览器至少实现三个常驻线程:javascript引擎线程,GUI渲染线程,浏览器事件触发线程. JavaScript引擎是基于事件驱动单线程执行的,JS引擎一直等待着任务队列中任务的到来然后加以处理,浏览器无论再什么时候都

优化js脚本设计,防止浏览器假死

在Web开发的时候经常会遇到浏览器不响应事件进入假死状态,甚至弹出"脚本运行时间过长"的提示框,如果出现这种情况说明你的脚本已经失控了,必须进行优化. 为什么会出现这种情况呢,我们先来看一下浏览器的内核处理方式: 浏览器的内核是多线程的,它们在内核制控下相互配合以保持同步,一个浏览器至少实现三个常驻线程:javascript引擎线程,GUI渲染线程,浏览器事件触发线程. JavaScript引擎是基于事件驱动单线程执行的,JS引擎一直等待着任务队列中任务的到来然后加以处理,浏览器无论再

如何封装自己的js类库

js中的很多变量(除了基本数据类型数字和字符串之外)可以看成类似java中的class是经过实例化的对象,有自己的成员方法,和成员变量.比如:window对象表示一个浏览器窗口或一个框架,在js中window对象是全局对象,如图下图所示.  接下来谈谈,如何实现自己的类库,先展示一个最简单的例子: 1 (function(a) { 2 function Person(name, age) { 3 this.name = name; 4 this.age = age; 5 } 6 Person.p

关于JS脚本语言的基础语法

JS脚本语言的基础语法:输出语法  alert("警告!");  confirm("确定吗?");   prompt("请输入密码");为弱类型语言: 开始时要嵌入JS代码:<script type="text/javascript"></script>: 关于写程序是需注意的基本语法:1.所有的字符全都是英文半角的:2.大部分情况下每条语句结束后要加分号:3.每一块代码结束后加换行:4.程序前呼后应:

Js脚本之jQuery学习笔记(1)

Js脚本之jQuery学习笔记(1) 一.javascript基础 单行注释 多行注释 /* */ 数据类型 数值型 字符串型 布尔型 空值 未定义值 转义字符 函数定义:1234567891011121314<head><script language="javascript"function test(m){var xixi="嘻嘻"alert("这是javascript")document.write(xixi + m)}

异步执行js脚本——防止阻塞

JS允许我们修改页面中的所有方面:内容,样式和用户进行交互时的行为. 但是js同样可以阻塞DOM树的形成并且延迟页面的渲染. 让你的js变成异步执行,并且减少不必要的js文件从而提高性能. JavaScript可以查询和修改DOM和CSSOM JavaScript的执行阻塞了CSSOM的执行 JavaScript 阻塞了DOM的形成,除非特殊声明js异步执行 js是一个同步语言可以修改网页的任何方面: <html> <head> <meta name="viewpo

2款JS脚本判断手机浏览器跳转WAP手机网站

随着移动设备的普及,企业的网络宣传已经不能局限在PC端,而需要同时在移动端有所建树.对于公司网站来说,以前都是做的PC端的,当然手机等移动端也可以访问,但是用户体验肯定不如完全适合的手机端来的方便.我们在给自己的网站做了WAP手机网站之后,如果有用户通过手机访问我们的企业顶级域名网站,那就判断跳转到专为的WAP网站. 这里整理到目前自己在使用的2种JS脚本,因为之前一直有朋友跟我要,所以这里分享出来. 第一种:直接JS脚本 <script type="text/javascript&quo

js脚本语言

js脚本语言,全称javascript,网页里面使用的脚本语言,非常强大的语言,其中注释语法单行注释//,多行注释/*注释内容*/,输出语法alert(信息)起到弹出信息的作用,confirm弹出一个和用户交互的对话框,prompt弹出一个可以让用户输入的对话框,嵌入js代码的时候尽量靠下写,<script type="text/javascript"></script>,程序基本只是,所有的字符全部是英文半角的,大部分情况下每条语句结束之后要加分号.每块代码结