Rumah >hujung hadapan web >tutorial js >jQuery仿Excel表格编辑功能的实现代码_jquery

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

WBOY
WBOYasal
2016-05-16 17:34:511344semak imbas

  在 Excel 中可进行的操作,你几乎都可以在网页中做到,如拖动复制、Ctrl+C 、Ctrl+V 等等。

  另外在浏览器支持方面,它支持以下的浏览器 IE7+, FF, Chrome, Safari, Opera。

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

复制代码 代码如下:

   
   
   

  然后添加一个用于呈现 Excel 编辑表格的 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代码:

复制代码 代码如下:




   
    Basic Demo
   
   
   
    <script><BR> $(function(){<BR> //数据<BR> var data = [<BR> {id: 1, name: "Ted", isActive: true, color: "orange"},<BR> {id: 2, name: "John", isActive: false, color: "black"},<BR> {id: 3, name: "Al", isActive: true, color: "red"},<BR> {id: 4, name: "Ben", isActive: false, color: "blue"}<BR> ];<BR> //黄色渲染方法<BR> var yellowRenderer = function (instance, td, row, col, prop, value, cellProperties) {<BR> Handsontable.TextCell.renderer.apply(this, arguments);<BR> $(td).css({<BR> background: 'yellow'<BR> });<BR> };<BR> //绿色渲染方法<BR> var greenRenderer = function (instance, td, row, col, prop, value, cellProperties) {<BR> Handsontable.TextCell.renderer.apply(this, arguments);<BR> $(td).css({<BR> background: 'green'<BR> });<BR> };<BR> //初始化<BR> var $container = $("#example1");<BR> $container.handsontable({<BR> data: data,<BR> startRows: 5,<BR> colHeaders: true,<BR> minSpareRows: 1,<BR> columns: [<BR> {data: "id"},<BR> {data: "name", type: {renderer: yellowRenderer}}, //黄色渲染<BR> {data: "isActive", type: Handsontable.CheckboxCell}, //多选框<BR> {data: "color",<BR> type: Handsontable.AutocompleteCell, //自动完成<BR> source: ["yellow", "red", "orange", "green", "blue", "gray", "black", "white"] //数据源<BR> }<BR> ],<BR> cells: function (row, col, prop) {<BR> if (row === 0 && col === 0) {<BR> return {type: {renderer: greenRenderer}};<BR> }<BR> }<BR> });<BR> });<BR> </script>


   



Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn