当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery仿Excel表格编辑功能的实现代码

jQuery仿Excel表格编辑功能的实现代码

2018年11月07日  | 移动技术网IT编程  | 我要评论
在 excel 中可进行的操作,你几乎都可以在网页中做到,如拖动复制、ctrl+c 、ctrl+v 等等。 另外在支持方面,它支持以下的浏览器 ie7+, ff, chrome, safari, o

在 excel 中可进行的操作,你几乎都可以在网页中做到,如拖动复制、ctrl+c 、ctrl+v 等等。

另外在支持方面,它支持以下的浏览器 ie7+, ff, chrome, safari, opera。

如何使用:
首先在页面中引入 jquery 框架和 handsontable 插件的相关 js 和 css 文件,以下列出的两个是必要的,还有其它的是可选的,如果需要某个功能时就(参看demo)加上。

代码 代码如下:


<script src="jquery.min.js"></script>
<script src="jquery.handsontable.full.js"></script>
<link rel="stylesheet" href="jquery.handsontable.full.css">


  然后添加一个用于呈现 excel 编辑表格的 div 层

代码 代码如下:


<p id="example1" ></p>


  最后就可以对其进行初始化了

 

 

代码 代码如下:


//数据
var data = [
{id: 1, name: "ted", isactive: true, color: "orange"},
{id: 2, name: "john", isactive: false, color: "black"},
{id: 3, name: "al", isactive: true, color: "red"},
{id: 4, name: "ben", isactive: false, color: "blue"}
];
//黄色渲染方法
var yellowrenderer = function (instance, td, row, col, prop, value, cellproperties) {
handsontable.textcell.renderer.apply(this, arguments);
$(td).css({
background: 'yellow'
});
};
//绿色渲染方法
var greenrenderer = function (instance, td, row, col, prop, value, cellproperties) {
handsontable.textcell.renderer.apply(this, arguments);
$(td).css({
background: 'green'
});
};
//初始化
var $container = $("#example1");
$container.handsontable({
data: data,
startrows: 5,
colheaders: true,
minsparerows: 1,
columns: [
{data: "id"},
{data: "name", type: {renderer: yellowrenderer}}, //黄色渲染
{data: "isactive", type: handsontable.checkboxcell}, //多选框
{data: "color",
type: handsontable.autocompletecell, //自动完成
source: ["yellow", "red", "orange", "green", "blue", "gray", "black", "white"] //数据源
}
],
cells: function (row, col, prop) {
if (row === 0 && col === 0) {
return {type: {renderer: greenrenderer}};
}
}
});


注意是p容器加载完了之后进行初始化:

 

demo代码:

 

代码 代码如下:


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>basic demo</title>
<script src="jquery.min.js"></script>
<script src="jquery.handsontable.full.js"></script>
<link rel="stylesheet" href="jquery.handsontable.full.css">
<script>
$(function(){
//数据
var data = [
{id: 1, name: "ted", isactive: true, color: "orange"},
{id: 2, name: "john", isactive: false, color: "black"},
{id: 3, name: "al", isactive: true, color: "red"},
{id: 4, name: "ben", isactive: false, color: "blue"}
];
//黄色渲染方法
var yellowrenderer = function (instance, td, row, col, prop, value, cellproperties) {
handsontable.textcell.renderer.apply(this, arguments);
$(td).css({
background: 'yellow'
});
};
//绿色渲染方法
var greenrenderer = function (instance, td, row, col, prop, value, cellproperties) {
handsontable.textcell.renderer.apply(this, arguments);
$(td).css({
background: 'green'
});
};
//初始化
var $container = $("#example1");
$container.handsontable({
data: data,
startrows: 5,
colheaders: true,
minsparerows: 1,
columns: [
{data: "id"},
{data: "name", type: {renderer: yellowrenderer}}, //黄色渲染
{data: "isactive", type: handsontable.checkboxcell}, //多选框
{data: "color",
type: handsontable.autocompletecell, //自动完成
source: ["yellow", "red", "orange", "green", "blue", "gray", "black", "white"] //数据源
}
],
cells: function (row, col, prop) {
if (row === 0 && col === 0) {
return {type: {renderer: greenrenderer}};
}
}
});
});
</script>
</head>
<body>
<p id="example1" ></p>
</body>
</html>

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网