2015-04-21 604 views
0

我有一個顏色變化的例子已經發生。問題是改變新單元格,行或列的顏色會改變前一個單元格/行/列的顏色。之前的單元格應該保持原來的顏色,並且我需要用一個函數(而不是多個渲染器)動態地發生這種情況。小提琴中有3種顏色選項,但我實際上使用了一種具有數百個選項的顏色選擇器。我該如何處理這種顏色變化(點擊右鍵)?Handsontable使用顏色選擇器更改單元格/行/列的顏色?

http://jsfiddle.net/anschwem/hkhk5zbo/17/

var data = [ 
     ['', 'Maserati', 'Mazda', 'Mercedes', 'Mini', 'Mitsubishi'], 
     ['2009', 0, 2941, 4303, 354, 5814], 
     ['2010', 3, 2905, 2867, 412, 5284], 
     ['2011', 4, 2517, 4822, 552, 6127], 
     ['2012', 2, 2422, 5399, 776, 4151] 
    ], 
    celldata = [], 
    container = document.getElementById('example'), 
    hot, 
    sentrow, 
    sentcol; 

var colorRenderer = function (instance, td, row, col, prop, value, cellProperties) { 
    Handsontable.renderers.TextRenderer.apply(this, arguments); 
    td.style.backgroundColor = $('#color_field').val(); 

}; 


var settings = { 
    data: data, 
    minSpareRows: 1, 
    rowHeaders: true, 
    colHeaders: true, 
    contextMenu: true, 
    startRows: 5, 
    startCols: 5, 

    //columns: coldata, 
    cell: celldata, 
    cells: function (row, col, prop) { 
     if (row === sentrow) { 
      this.renderer = colorRenderer; 
     } 
     if (col === sentcol) { 
      this.renderer = colorRenderer; 
     } 
    }, 
}; 

hot = new Handsontable(example, settings); 

hot.updateSettings({ 
    contextMenu: { 
     callback: function (key, options) { 
      if (key === 'cellcolor') { 
       setTimeout(function() { 
        //timeout is used to make sure the menu collapsed before alert is shown 
        var row = hot.getSelected()[0]; 
        var col = hot.getSelected()[1]; 

        var item = {}; 
        item.row = row; 
        item.col = col; 
        item.renderer = colorRenderer 
        celldata.push(item) 

        hot.updateSettings({cell: celldata}); 
        hot.render(); 

       }, 100); 
      } 
      if (key === 'rowcolor') { 
       setTimeout(function() { 
        //timeout is used to make sure the menu collapsed before alert is shown 
        var row = hot.getSelected()[0]; 
        sentrow = row; 
        hot.render(); 

       }, 100); 
      } 
      if (key === 'colcolor') { 
       setTimeout(function() { 
        //timeout is used to make sure the menu collapsed before alert is shown 
        var col = hot.getSelected()[1]; 
        sentcol = col; 
        hot.render(); 

       }, 100); 
      } 
     }, 
     items: { 
       "cellcolor": { 
       name: 'Cell color' 
      }, 
       "rowcolor": { 
       name: 'Row color' 
      }, 
       "colcolor": { 
       name: 'Column color' 
      }, 
     } 
    } 
}) 

回答

1

編輯:重構爲了清楚的代碼。

隨後的調用正在改變顏色,因爲您的colorRenderer回調在每次渲染時詢問下拉列表,而不是在創建單元格樣式時捕獲值。

$(document).ready(function() { 
    var data = [ 
       ['', 'Maserati', 'Mazda', 'Mercedes', 'Mini', 'Mitsubishi'], 
       ['2009', 0, 2941, 4303, 354, 5814], 
       ['2010', 3, 2905, 2867, 412, 5284], 
       ['2011', 4, 2517, 4822, 552, 6127], 
       ['2012', 2, 2422, 5399, 776, 4151] 
       ]; 

    var container = document.getElementById('example'); 

    // Put your color picker function here 
    function getSelectedColor() { 
     return $('#color_field').val(); 
    } 

    var TableStyles = function(hot) { 
     var self = this; 

     var _cellStyles = []; 

     var _createStyle = function(row, col, color) { 
      var _color = color; 

      var style = { 
       row: row, 
       col: col, 
       renderer: function (instance, td, row, col, prop, value, cellProperties) { 
           Handsontable.renderers.TextRenderer.apply(this, arguments); 
           td.style.backgroundColor = _color; 
          }, 
       color: function(c) { _color = c; }     
      };  

      return style; 
     }; 

     self.getStyles = function() { 
      return _cellStyles; 
     }; 

     self.setCellStyle = function(row, col, color, updateTable) { 
      var _color = color; 

      if (_cellStyles.length == 0) { 
       _cellStyles.push(_createStyle(row, col, color)); 
      } else { 
       var found = _cellStyles.some(function(cell) { 
        if (cell.row == row && cell.col == col) {       
         cell.color(color); 
         return true; 
        } 
       }); 

       if (!found) { 
        _cellStyles.push(_createStyle(row, col, color)); 
       } 
      }     

      if (updateTable!=false) { 
       hot.updateSettings({cell: self.getStyles()}); 
       hot.render();       
      };     
     }; 

     self.setRowStyle = function(row, color) { 
      for (var col=0; col<hot.countCols(); col++) 
       self.setCellStyle(row, col, color, false); 

      hot.updateSettings({cell: self.getStyles()}); 
      hot.render();       
     }; 

     self.setColStyle = function(col, color) { 
      for (var row=0; row<hot.countCols(); row++) 
       self.setCellStyle(row, col, color, false); 

      hot.updateSettings({cell: self.getStyles()}); 
      hot.render();       
     }; 
    }; 

    var settings = { 
     data: data, 
     minSpareRows: 1, 
     rowHeaders: true, 
     colHeaders: true, 
     contextMenu: true, 
     startRows: 5, 
     startCols: 5, 
     cell: [] 
    }; 

    hot = new Handsontable(container, settings); 

    var styles = new TableStyles(hot);   

    hot.updateSettings({ 
     contextMenu: { 
      callback: function (key, options) {     
       if (key === 'cellcolor') { 
        setTimeout(function() {       
         var sel = hot.getSelected();       

         styles.setCellStyle(sel[0], sel[1], getSelectedColor()); 
        }, 100); 
       } 
       if (key === 'rowcolor') { 
        setTimeout(function() { 
         //timeout is used to make sure the menu collapsed before alert is shown 
         var sel = hot.getSelected();       

         styles.setRowStyle(sel[0], getSelectedColor()); 
        }, 100); 
       } 
       if (key === 'colcolor') { 
        setTimeout(function() { 
         //timeout is used to make sure the menu collapsed before alert is shown 
         var sel = hot.getSelected();       

         styles.setColStyle(sel[1], getSelectedColor()); 
        }, 100); 
       } 
      }, 
      items: { 
        "cellcolor": { 
        name: 'Cell color' 
       }, 
        "rowcolor": { 
        name: 'Row color' 
       }, 
        "colcolor": { 
        name: 'Column color' 
       }, 
      } 
     } 
    }) 
}); 

的TableStyles對象提供的原始細胞風格陣列Handsontable期待各地的包裝,讓您的CAL只需要調用styles.setCellStyle(行,列,顏色),這將需要創建或更新的護理爲你的單元陣列。

JSFiddle

+0

這工作很漂亮,但因爲我使用的顏色選擇器我存儲功能之外的顏色。 http://jsfiddle.net/anschwem/0h6yqzw7/23/ – triplethreat77

+0

更新了JSFiddle以反映這一點。您仍然可以通過將storedColor的值分配給colorValue來捕獲封閉中所選顏色的值。 – McCroskey

+0

我一直在努力,無論以前的顏色會不會改變(儘管它完美地適用於你的例子) - 超級沮喪。這很古怪,我不確定。這是我正在作爲備用(http://jsfiddle.net/anschwem/9odsv02x/1/)的工作,並且單元格工作。行(在我的結尾)將取代以前的行顏色,我不知道爲什麼我必須長 - 1,否則它會添加一個額外的列。也許這是我<= settings.startCols? – triplethreat77