2013-07-18 22 views
1

我有一個函數可以處理一些onChange事件並且運行良好。該函數調用另一個函數來檢查單元格的內容,如果出現錯誤,它應該改變單元格的顏色。在handontable插件中不能使用的自定義渲染器函數

function Check(x, y) 
{ 
    var content = $editorTableContainer.handsontable('getDataAtCell', y, x); 
    var split = content.split(' ');  

    $.each(split, function (key, value) { 
     $.get('check.php?word=' + value, function (data) { 
      //blank if no error otherwise it returns an array of suggestions (only need to check if there is an error) 
      if (data) { 
       alert("test"); 
       var meta = $editorTableContainer.handsontable('getCellMeta', y, x); 
       meta.renderer = ErrorRenderer; 
      } 
     }); 
    }); 

    return; 
} 

這裏是我的簡單ErrorRenderer:

function ErrorRenderer(instance, td, row, col, prop, value, cellProperties) 
{ 
    Handsontable.TextCell.renderer.apply(this, arguments); 
    console.log(row); 
    td.style.fontWeight = 'bold'; 
    td.style.color = 'green'; 
    td.style.background = '#CEC'; 
} 

的ErrorRenderer不會被調用沉綿警報被觸發,任何想法,爲什麼?

謝謝

+1

題外話:FWIW,有一個有用的JS代碼約定,以儲備只有構造函數的大寫函數名稱。 http://javascript.crockford.com/code.html#names – alxndr

+0

謝謝。我不知道這 – Cornwell

+0

看到我的評論關於在下面的答案渲染 – PostureOfLearning

回答

1

如果使用handsontable,你爲什麼不使用其內置的功能?

看一看高溫超導conditional formatting

此外,在0.9.5版本中列選項加入validator。詳情here

validator (value: Mixed, callback: Function) 

validator : RegExp Object 

然後使用事件(詳見here):

afterValidate (isValid: Boolean, value: Mixed, row: Number, prop: String, source: String) 

做單元格的格式

而且,在你的榜樣,您正在設置渲染器,但實際上正在渲染單元格?你需要重新渲染嗎?

0

我可以看到你的渲染是TextCell ..那就只有文本單元工作,檢查錯誤渲染應該TextCell工作或NumericCell