1. 程式人生 > >[轉]Handsontable對單元格的操作

[轉]Handsontable對單元格的操作

splice mbo center tor led 示例 bottom turn 函數

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

1.自動填充單元格數據

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


2.合並單元格

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

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

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

[javascript] view plain copy print?
  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.渲染比對結果

示例代碼如下:

[javascript] view plain copy print?
  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. hot = new Handsontable(example, {
  12. data: data,
  13. colHeaders: true,
  14. search: true
  15. });
  16. function onlyExactMatch(queryStr, value) {
  17. return queryStr.toString() === value.toString();
  18. }
  19. Handsontable.Dom.addEvent(searchFiled, ‘keyup‘, function (event) {
  20. var queryResult = hot.search.query(this.value);
  21. console.log(queryResult);
  22. hot.render();
  23. });

7.評論

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

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

cell:[

{

row:行數,

col:列數,

comment:評論內容

}

]

[轉]Handsontable對單元格的操作