DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jQuery仿Excel表格編輯功能的實現代碼
jQuery仿Excel表格編輯功能的實現代碼
編輯:JQuery特效代碼     

  在 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 層

代碼 代碼如下:
<div id="example1" ></div>

  最後就可以對其進行初始化了

代碼 代碼如下:
//數據
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}};
}
}
});

注意是div容器加載完了之後進行初始化:

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>
<div id="example1" ></div>
</body>
</html>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved