最近自己封装了个JS脚本,用来创建和操作Table
/**
依赖JQuery **/ (function () {
var Table = window.Table = function (rowCount, columnCount, width, height, rowHeight) {
this.rowCount = rowCount;
this.columnCount = columnCount;
this.width = width;
this.height = height;
this.rowHeight = rowHeight;
this.context;
this.tableCss;
this.rowCss;
this.columnCss;
this.onTdCreated = function () { };
this.onTdMouseDown = function (e) { };
this.onTrCreated = function () { };
this.onTrMouseDown = function (e) { };
};
Table.prototype.CreateTable = function (target) {
this.context = $("<table style='width:auto'></table>"); if (this.tableCss)
this.context.addClass(this.tableCss);
if (this.width)
this.context.css("width", this.width);
if (this.height)
this.context.css("height", this.height); if (target) {
this.context.appendTo(target);
} for (var index = 0; index < this.rowCount; index++) {
CreatedRow.call(this, this.context);
} return this.context;
};
Table.prototype.GetRowCount = function () {
return this.rowCount;
};
Table.prototype.GetColumnCount = function () {
return this.columnCount;
};
Table.prototype.AddRow = function () {
var tr = CreatedRow.call(this, this.context);
if (this.rowHeight)
tr.css("height", this.rowHeight); this.rowCount++;
return tr;
};
Table.prototype.AddColumn = function () {
var rows = $(this.context).children().children();
for (var index = 0; index < rows.length; index++) {
CreateTd.call(this, $(rows[index]));
}
this.columnCount++;
};
Table.prototype.RemoveRow = function (row) {
$(row).remove();
this.rowCount--;
return $(row);
}
Table.prototype.RemoveColumn = function (column) {
var tds = $(column).parent().children();
var columnIndex = GetColumnIndex(tds, column);
var rows = this.context.children().children()
for (var index = 0; index < rows.length; index++) {
var tr = rows[index];
var tdIndex = GetTdIndex(tr, columnIndex);
var td = $(tr).children()[tdIndex];
if (parseInt($(td).attr("colspan") || 1) > 1) {
$(td).attr("colspan", parseInt($(td).attr("colspan") - 1));
} else {
$(td).remove();
}
}
this.columnCount--;
}
Table.prototype.MergeColumns = function (column, value) {
value = parseInt(value);
if (value <= 0) {
throw "列数必须大于0!";
return;
} var tds = $(column).nextAll('td');
if (value > tds.length) value = tds.length;
var colspan = parseInt($(column).attr("colspan") || 1); for (var index = 0; index < value; index++) {
colspan += parseInt($(tds[index]).attr("colspan") || 1);
$(tds[index]).remove();
} $(column).attr("colspan", colspan);
}
Table.prototype.SplitColumn = function (column, value) {
value = parseInt(value);
if (value <= 0) {
throw "列数必须大于0!";
return;
}
var colspan = parseInt($(column).attr("colspan") || 1); if (colspan <= 1) {
throw "不能拆分单元格!";
return;
} if (value > colspan) {
throw "输入数字无效!";
return;
} $(column).attr("colspan", colspan - parseInt(value) + 1);
var tr = $(column).parent();
for (var index = 0; index < value - 1; index++) {
CreateTd.call(this,tr);
}
}
Table.prototype.AddRowFrom = function (row, position) {
var tr = CreatedRow();
for (var index = 0; index < this.columnCount; index++) {
CreateTd(tr);
}
switch (position) {
case "north":
$(row).before(tr);
break;
case "south":
$(row).after(tr);
break;
default:
$(row).after(tr);
break;
}
this.rowCount++;
return tr;
}
Table.prototype.AddColumnFrom = function (column, position) {
var columnIndex = GetColumnIndex($(column).parent().children(), column);
if (columnIndex < 0) {
throw "获取当前列失败!";
return;
}
var rows = this.context.children().children();
for (var index = 0; index < rows.length; index++) {
row = rows[index];
var td = CreateTd();
var tdIndex = GetTdIndex(row, columnIndex);
switch (position) {
case "west":
$($(row).children()[tdIndex]).before(td);
break;
case "east":
$($(row).children()[tdIndex]).after(td);
break;
default:
$($(row).children()[tdIndex]).after(td);
break;
}
}
this.columnCount++;
} function CreatedRow(target) {
var tr;
if (this.rowCss)
tr = $("<tr class='" + this.rowCss + "'></tr>");
else
tr = $("<tr></tr>"); if (target) {
tr.appendTo(target);
} for (var index = 0; index < this.columnCount; index++) {
CreateTd.call(this, tr);
} if ($.isFunction(this.onTrMouseDown))
tr.bind("mousedown ", this.onTrMouseDown); if ($.isFunction(this.onTrCreated))
this.onTrCreated.call(tr); return tr;
}; function CreateTd(target) {
var td;
if (this.columnCss)
td = $("<td class='" + this.columnCss + "'></td>");
else
td = $("<td></td>"); if (target) {
td.appendTo(target);
} if ($.isFunction(this.onTdMouseDown))
td.bind("mousedown ", this.onTdMouseDown); if ($.isFunction(this.onTdCreated))
this.onTdCreated.call(td); return td;
} function GetColumnIndex(tds, td) {
var tdIndex = $.inArray(td, tds);
var columnIndex = 0;
for (var index = 0; index <= tdIndex; index++) {
columnIndex += parseInt($(tds[index]).attr("colspan") || 1);
}
return columnIndex;
} function GetTdIndex(tr, columnIndex) {
var tds = $(tr).children();
var ColumnIndex = 0;
for (var index = 0; index < tds.length; index++) {
ColumnIndex += parseInt($(tds[index]).attr("colspan") || 1);
if (ColumnIndex >= columnIndex)
return index;
}
}
}());
最近自己封装了个JS脚本,用来创建和操作Table的更多相关文章
- (转)优化js脚本设计,防止浏览器假死
在Web开发的时候经常会遇到浏览器不响应事件进入假死状态,甚至弹出“脚本运行时间过长“的提示框,如果出现这种情况说明你的脚本已经失控了,必须进行优化. 为什么会出现这种情况呢,我们先来看一下浏览器的内 ...
- 优化js脚本设计,防止浏览器假死
在Web开发的时候经常会遇到浏览器不响应事件进入假死状态,甚至弹出"脚本运行时间过长"的提示框,如果出现这种情况说明你的脚本已经失控了,必须进行优化. 为什么会出现这种情况呢,我们 ...
- 【转】第6篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:自动注册JS脚本+自动反射方法分析
作者: 牛A与牛C之间 时间: 2013-11-21 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第6篇:Xilium CefGlue 关于 CLR Object 与 JS ...
- 【转】第5篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:自动注册JS脚本+委托回调方法分析
作者: 牛A与牛C之间 时间: 2013-11-19 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第5篇:Xilium CefGlue 关于 CLR Object 与 JS ...
- 自动化测试中执行JS脚本方法封装
执行JS脚本方法封装: class JavaScript(Base): def execute_javascript(self, js): """执行 JavaScrip ...
- ASP.Net MVC4中封装CSS和js冗余代码(不让其大篇的显示在前台上)
(1)封装CSS和JS代码,使用调用的方式在前台进行调用.是开发看起来简洁和易于管理,可达到重用. 由于asp.netMVC4 框架 ,在封装js和CSS的时候,有如下规范: using Syst ...
- Octopus系列之如何让前台的js脚本变得灵活重用
Octopus系列如何让前台的js脚本变得灵活,重用 方式1:ajax方式 方式2:form表单方式 面向对象的脚本封装 jQuery的封装 做Web开发的少不了前台Ajax的使用, 返回true:f ...
- JS脚本动态给元素/控件添加事件
最近突然要用到JS脚本动态给元素添加事件.如TextBox的onclick事件.但有的onclick事件原先已经定义了相应代码!这里又不能替代原有方法,而JS脚本里面有个方法可以给控件在原有事件的基础 ...
- Firebug中调试中的js脚本中中文内容显示为乱码
Firebug中调试中的js脚本中中文内容显示为乱码 设置 页面 UFT-8 编码没用, 解决方法:点击 "Firebug"工具栏 中的"选项"---" ...
随机推荐
- Ibatis.net总是报:【ExecuteStoreCommand SqlParameterCollection 中已包含 SqlParameter】(转)
今天很奇怪调用EF的ExecuteStoreCommand 出现了个这样的错误,怎么也调试不过,痛定思痛 原来 command被连着调用了而没有销毁掉 public static DataTabl ...
- Struts2:国际化
链接:[Java:国际化] src下有国际化资源文件:lan.propertieslan_zh_CN.properties 中文系统系统默认使用zh_CN文件,没有的话使用基本文件lan.proper ...
- Struts2:效验器——声明式
就是用xml配置的方式,而不是Java代码的形式,那个是“编程式” Action: package org.ah.s2; import com.opensymphony.xwork2.ActionSu ...
- dialogfield
before ax2012: typeof() or extendedtype ax2012: extendedtypestr()
- AOP是什么?
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向方面编程.AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可 ...
- Build subversion 1.8 with SSL on OS X Yosemite
mkdir -p /tmp/buildroot && cd /tmp/buildroot # Need to build the latest libtool and automake ...
- JQUERY DIALOG窗格内不能使用FORM
今天在做AJAX DIALOG数据提交实验,实验目的是,在DIALOG内显示另一页面,并将FORM组件信息提交保存至数据库.代码如下: DIALOG实现: function OpenDialog(ur ...
- cs11_c++_lab3
Matrix.hh class Matrix { int row; int col; int *p; void copy(const Matrix &m); void clearup(); p ...
- 用JS实现九九乘法表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [python]获取文件夹下所有文件名
#---picknames.py---import os filenames = os.listdir(os.getcwd()) for name in filenames: print(name)