最近自己封装了个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"工具栏 中的"选项"---" ...
随机推荐
- HBase Shell操作
Hbase 是一个分布式的.面向列的开源数据库,其实现是建立在google 的bigTable 理论之上,并基于hadoop HDFS文件系统. Hbase不同于一般的关系型数据库(RDBMS ...
- Git Pro - (1) 基础
近乎所有操作都可本地执行 在Git中的绝大多数操作都只需要访问本地文件和资源,不用连网. 三种状态 对于任何一个文件,在 Git 内都只有三 种状态:已提交(committed),已修改(modifi ...
- 自学python3随笔--连接数据库和写EXCEL文件实现
近日在自学python3,选择python,是由于它命令比较简单,语法也不算复杂,对我来说,要实现的功能用100行语句就能够实现.另外,一个原因,它是脚本形式的,调试比较方便,对我这些有很长时间没有写 ...
- SQLServer异步调用,批量复制
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- session的常用方法。
void setAttribute(String attribute, Object value) 设置Session属性.value参数可以为任何Java Object.通常为Java Bean.v ...
- phpwind ecshop 用户整合
phpwind ecshop 用户整合,其实很简单.但在网上搜到的尽是乱七八遭的方法,搞得很复杂. 原来公司做的phpwind 与 ecshop 结合的项目,别的同事已经把用户整合好了,当时我还不知道 ...
- jquery 高度的获取
alert($(window).height()); //浏览器当前窗口可视区域高度 alert($(document).height()); //浏览器当前窗口文档的高度 alert($(docum ...
- 用浏览器模拟各种User Agent
转至:http://www.cnblogs.com/top5/archive/2012/06/07/2540686.html 测试页面的时候经常需要不同的User Agent,Firefox.Chro ...
- android第二天(项目的组成结构)
1:src文件夹分析: helloWorld----src(源码文件夹) MainActivity:主界面类----gen(自动生成的源码文件夹) R.java:对应res文件夹 下面又包含三个内部类 ...
- mysql 忘记root 密码的解决方法
LINUX 1.切换root 用户 2.停止mysqld 服务 /etc/inid.d mysqld stop 3.跳过验证登录 mysqld_safe --skip-grant-tables &am ...