【问题标题】:How to show icon next to value in cloumn in aurelia slickgrid/slickgrid?如何在 aurelia slickgrid/slickgrid 的列中的值旁边显示图标?
【发布时间】:2021-10-11 11:03:57
【问题描述】:

我想在金额列中的值旁边显示编辑图标。这是因为 Amount 列实际上是可编辑的。但是为了给用户一个提示,我想在它旁边显示一些编辑图标。如何在 aurelia slickgrid 中做到这一点?

或者也许有一种方法可以在悬停时突出显示字段? 我正在使用 aurelia slickgrid 并查看 aurelia slickgrid 本身是否有一些选项。

【问题讨论】:

  • 你需要一个格式化程序,看看这个 SO answer(答案的第二部分)

标签: aurelia slickgrid angular-slickgrid


【解决方案1】:

我在金额旁边添加了一个编辑图标,

{
                id: "Edit",
                field: "edit",
                excludeFromColumnPicker: true,
                excludeFromExport: true,
                excludeFromQuery: true,
                excludeFromGridMenu: true,
                excludeFromHeaderMenu: true,
                minWidth: 30,
                maxWidth: 30,
                formatter: Formatters.editIcon,
            },

并使用来自 ghiscoding 注释的自定义格式:

const customEditableInputFormatter: Formatter = (_row, _cell, value, columnDef, dataContext, grid) => {
const isEditable = !!columnDef.editor;
value = (value === null || value === undefined) ? '' : value;
return isEditable ? `<div style="background-color: aliceblue">${value}</div>` : value;

};

结果如图所示。

【讨论】:

  • 太好了,如果您希望蓝色背景颜色填充整个单元格容器而没有填充,请注意,您可以更改格式化程序以返回对象而不是字符串及其addClass 当你这样做时,它是整个单元容器,而不是单元容器内的 div。看到这个FormatterResultObject - Wiki 类似return { addClass: 'custom-editor', text: value }
【解决方案2】:

转到 aurelia slickgrid 示例link 并点击示例源代码链接

打开的时候有个方法叫defineGrids

/* Define grid Options and Columns */
  defineGrids() {
    this.columnDefinitions1 = [
      ..., 
      ...,
      ...,
      ...,
      ...,
      { id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven', formatter: myCustomCheckmarkFormatter, type: FieldType.number, sortable: true, minWidth: 100 }
    ];
    ... rest of the code
  }

id 为effort-driven 的行是放置图标的位置。换句话说,当您将数据集合(通常是 json 对象数组)推送到表时,键名为 effort-driven 的数据对象的值将被赋予 ID 为 effort-driven 的列。此外,对于每个传递给列的值,myCustomCheckmarkFormatter 方法会重新格式化它(例如 0 -> false 或 null -> 未填充)并将其放置到相应表的单元格中。看看下面的方法:

// create my custom Formatter with the Formatter type
const myCustomCheckmarkFormatter: Formatter<DataItem> = (_row, _cell, value) => {
  // you can return a string of a object (of type FormatterResultObject), the 2 types are shown below
  return value ? `<i class="fa fa-fire red" aria-hidden="true"></i>` : { text: '<i class="fa fa-snowflake-o" aria-hidden="true"></i>', addClasses: 'lightblue', toolTip: 'Freezing' };
};

如您所见,当调用该方法时,它会返回一个图标,例如&lt;i class="fa fa-fire red" aria-hidden="true"&gt;&lt;/i&gt;,该图标放置在表格的单元格中。

【讨论】:

  • 谢谢,我从上面的 ghiscoding 评论中得到了这个想法,并且突出显示对我有用。我也尝试了上述解决方案,但由于某种原因,我的列中没有图标......也许我错过了一些图标包?无论如何,最后,我放置了一个带有突出显示的编辑图标列,现在这似乎很好。
  • 啊,我想我理解了你的答案,所以我需要一个带有关键的列,因为努力驱动和休息会映射。在我目前的情况下,我从后端(使用 odata)得到一个 api json 响应,因此数据已经定义。也许这适用于本地生成的数据或者我们可以更改后端响应的地方。你有你的解决方案的截图吗?然后我可以选择你的作为提供的答案。
  • @sb32134 您只需要列名 id 和 json 值键相同。不需要努力驱动的 id,您可以将 id 设置为 myId 在这种情况下,当您重视的关键是 myId
猜你喜欢
  • 2021-06-16
  • 2015-10-21
  • 2022-12-06
  • 1970-01-01
  • 2021-04-17
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多