Handsontable對單元格的操作


原文地址:http://blog.csdn.net/mafan121/article/details/46119905

1.自動填充單元格數據

fillHandle:true/false    //當值為true時,允許拖動單元格右下角,將其值自動填充到選中的單元格

 

2.合並單元格

初始化配置:mergeCells:[{row:起始行數,cols:起始列數,rowspan:合並的行數,colspan:合並的列數},...]

或者初始化聲明:mergeCells:true   //表示允許單元格合並

但單元格合並的操作,需如下操作:

 1 if(key == "merge") {    
 2     if(hot.mergeCells.mergedCellInfoCollection.length > 0) {    
 3         for(var k=0; k<hot.mergeCells.mergedCellInfoCollection.length; k++) {    
 4             if(hot.mergeCells.mergedCellInfoCollection[k].row == row &&    
 5                  hot.mergeCells.mergedCellInfoCollection[k].col == col) {    
 6                     hot.mergeCells.mergedCellInfoCollection.splice(k,1);    
 7                     return;    
 8             }else{    
 9                 hot.mergeCells.mergedCellInfoCollection.push({"row": row, "col": col,     
10                     "rowspan": rowspan, "colspan": colspan});    
11                     break;    
12             }    
13         }    
14     } else {    
15         hot.mergeCells.mergedCellInfoCollection.push({"row": row, "col": col, "rowspan": rowspan, "colspan": colspan});    
16     } 

3.初始化單元格或列的對齊方式

水平樣式:htLeft,htCenter,htRight,htJustify

垂直樣式:htTop,htMiddle,htBottom

 

4.只讀模式

列只讀,設置列屬性:readOnly:true

單元格只讀:cellProperties.readOnly = true

 

5.設置單元格的編輯類型

columns:[{type:類型}]

主要的類型有:

text:字符串

numeric:數字類型

date:日期框

checkbox:單選框

password:密碼框

 

下面幾種類型的使用比較特殊

select:下拉編輯器

columns:[

      {editor:'select',

      selectOption:[選項1,選項2,...]},

      .......

]

 

dropdown:下拉選擇

columns:[

     {type:'dropdown',

     source:[選項1,選項2,...]},

     ......

]

 

autocomplete:自動匹配模式

columns:[

     {type:'autocomplete',

     source:[選項1,選項2,...],

     strict:true/false,                        //值為true,嚴格匹配

     allowInvalid:true/false              //值為true時,允許輸入值作為匹配內容,只能在strict為true時使用

     },

     ......

]

 

handsontable:表格模式

columns:[

     {type:'handsontable',

     handsontable:{...},

     ......

]

 

自定義模式

data=[{

           title:html標簽,

           description:描述,

           comments:評論,

           cover:封面

       },

       ......

]

 

自定義邊界

customBorders:[

      range:{

               form:{row:行數,col:列數},          //起始行列

               to:{row:行數,col:列數},              //終止行列

               top/bottom/right/left:{                     //上下右左邊線

                   width:像數,

                   color:顏色

               }

      }

]

 

6.查詢單元格的值

查詢單元格的值需要3個步驟:

a.設置hot的屬性search為true

b.創建比對函數

c.渲染比對結果

示例代碼如下:

 1 var  
 2    data = [  
 3      ['Nissan', 2012, 'black', 'black'],  
 4      ['Nissan', 2013, 'blue', 'blue'],  
 5      ['Chrysler', 2014, 'yellow', 'black'],  
 6      ['Volvo', 2015, 'yellow', 'gray']  
 7    ],  
 8    example = document.getElementById('example1'),  
 9    searchFiled = document.getElementById('search_field'),  
10    hot;  
11   
12  hot = new Handsontable(example, {  
13    data: data,  
14    colHeaders: true,  
15    search: true  
16  });  
17   
18  function onlyExactMatch(queryStr, value) {  
19    return queryStr.toString() === value.toString();  
20  }  
21   
22  Handsontable.Dom.addEvent(searchFiled, 'keyup', function (event) {  
23    var queryResult = hot.search.query(this.value);  
24   
25    console.log(queryResult);  
26    hot.render();  
27  });  

7.評論

comments:true/false    //當值為true時可以顯示評論,右鍵菜單可添加刪除評論。

當值為true時,可設置單元格的評論內容,格式為:

cell:[

         {

             row:行數,

             col:列數,

             comment:評論內容

         }

]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM