简体   繁体   English

如何根据Handsontable中的表格单元格值设置表格单元格外观

[英]How to set table cell appearance depending on table cell value in Handsontable

I am using Handsontable for PHP with MySQL database. 我正在使用Handsontable for MySQL和MySQL数据库。

In my table a have 6 columns as follows: 在我的表中有6列,如下所示:

| | Product | 产品展示 Qty. 数量 | | Good | Damaged | 损坏 Cut & Torn | 切碎

Qty.= Good + Damaged + Cut & Torn 数量=好+损坏+割破

First I'll enter the "Product" and "Quantity" in the table and at that time the values of "Good", "Damaged", "Cut & Torn" will be set automatically to zero. 首先,我将在表中输入“产品”和“数量”,届时“好”,“损坏”,“剪切和撕裂”的值将自动设置为零。 But when I enter a value in any of the cell "Good", "Damaged" or "Cut & Torn" the cell's background will be red until the total values of those three cells ("Good", "Damaged" and "Cut & Torn") will equal to the value of the "Quantity" cell. 但是,当我在“ Good”,“ Damaged”或“ Cut&Torn”任一单元格中输入值时,该单元格的背景将变为红色,直到这三个单元格的总值(“ Good”,“ Damaged”和“ Cut&Torn”撕裂”)将等于“数量”单元格的值。 Now I need some guide to achieve that via JS ? 现在,我需要一些指南来通过JS实现该目标? Any help will be appreciated. 任何帮助将不胜感激。

You may use this solution: 您可以使用以下解决方案:

Handsontable.hooks.add('afterRender', function() {
var d = this;
var quantity = d.getDataAtCol(1);
var good = d.getDataAtCol(2);
var damaged = d.getDataAtCol(3);
var cut_torn = d.getDataAtCol(4);


$.each(quantity,function(i,v){
    if (+good[i] !== 0 || +damaged[i] !== 0 || +cut_torn[i] !== 0){
 if ((+good[i]] + +damaged[i] + +cut_torn[i]) != v ) {
  $(d.getCell(i,1)).css({'background':'yellow'});
  $(d.getCell(i,2)).css({'background':'red'});
  $(d.getCell(i,3)).css({'background':'red'});
  $(d.getCell(i,4)).css({'background':'red'});
 } 
 }
});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM