【问题标题】:Is there a way to add multiple classes using the renderers method?有没有办法使用 renderers 方法添加多个类?
【发布时间】:2018-04-21 19:13:33
【问题描述】:

我正在尝试构建一个看起来相当复杂的桌子,并且我一直在使用 handsontable 的各种功能。

我希望实现的一件事是为单元格分配不同的类以用于样式设置。所以我将渲染器用于各种场景。问题是,当我将新类分配给单元格时,就像是第一次渲染它。

例子:

const cellClasses = (row, col, prop) => {
const cellProperties = {};
if (col === 2 || col === 8 || col === 15) {
    cellProperties.renderer = borderTest; // uses function directly
}
if ((row === 6 && col > 1) || (row === 12 && col > 1) || (row === 18 && col > 1)) {
    cellProperties.renderer = bgTest; // uses function directly
}
return cellProperties;

};

function bgTest(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.renderers.TextRenderer.apply(this, arguments);
    td.className = 'testbg';
}

function borderTest(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.renderers.TextRenderer.apply(this, arguments);
    td.className += 'testborder';
}

请不要太在意逻辑。在这一点上,我担心的是,如果一个单元格恰好同时满足这两个条件,它就会将一个类添加到另一个类中。

一种老生常谈的方法是我用两种逻辑的组合来制作一个更大的 IF,但随着我的网格变得越来越复杂,维护起来会变得更加困难。

所以,我的问题是,有没有一种简单的方法可以将多个类分配给单元格,而不是同时分配。

【问题讨论】:

    标签: handsontable


    【解决方案1】:

    简而言之,不,您不能按照您描述的方式分配类,因为 Handsontable 故意在每次迭代时重新渲染整个单元格。这样做是有充分理由的,主要是为了减少状态错误。

    拥有更具体的逻辑来确定列所需的所有渲染器并不是一个坏主意,所以我想说,提出逻辑来声明性地定义列是合理的(而不是 hacky!)。您可能需要考虑使用 col 索引以外的其他东西来使代码更具可伸缩性。在我们的表中,我们倾向于将列定义存储在配置文件中或从我们的后端服务创建。希望对您有所帮助!

    【讨论】:

    猜你喜欢
    • 2012-09-23
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2016-10-17
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多