Tom-Select(或 Selectize.js)选项组的远程数据

发布于 2025-01-09 07:00:30 字数 493 浏览 3 评论 0原文

我正在尝试使用远程数据进行 tom-select (https://tom-select.js .org/examples/optgroups/)。我不知道如何使用远程数据配置选项组。我选择加载远程数据,如下所示:

"optgroup": "1 Materials | 1.2 Gravel",
"value": 65,
"label": "1.2.1 Tanks"

从文档中我得到的印象是您设置了 optgroupField: 'optgroup' 并且选项组将自动设置。我需要将 optgroups 数组添加到我的 JSON 数据中吗?我似乎在任何地方都找不到带有选项组的远程数据的任何示例。

tom-select 共享 Selectize.js 中的大部分代码,因此我也对其进行交叉标记。

I am trying to use remote data for tom-select (https://tom-select.js.org/examples/optgroups/). I am at a loss how to configure option groups with remote data. I have the select loading with remote data like this:

"optgroup": "1 Materials | 1.2 Gravel",
"value": 65,
"label": "1.2.1 Tanks"

From the docs I got the impression that you set optgroupField: 'optgroup' and the option groups would be set automatically. Do I need to add the optgroups array to my JSON data? I can't seem to find any examples of remote data with option groups anywhere.

tom-select shares much of it's code from Selectize.js so I am cross tagging this also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2025-01-16 07:00:30

我在 selectize 世界中找到了一个解决方案:

https://github.com /selectize/selectize.js/issues/151#issuecomment-111056161

我添加了一个组 ID:

"optgroup_id": 13,
"optgroup": "1 Materials | 1.1 Pipe, valves & fittings",
"value": 5,
"label": "1.1.1 Line Pipe"

重置组字段:optgroupField: 'optgroup_id' 然后在 load 中的 json 回调之后添加了这个:

json.items.forEach((item) => {
this.addOptionGroup(item['optgroup_id'], { label: item['optgroup'] } );
  });
this.refreshItems()

我还在尝试添加第二个 optgroups json 数组,其中仅包含组以避免循环通过所有选项。

我希望有一个更好的答案 - 对此将保持开放。希望这对其他人有帮助。

I found a solution in the selectize world:

https://github.com/selectize/selectize.js/issues/151#issuecomment-111056161

I added a group id:

"optgroup_id": 13,
"optgroup": "1 Materials | 1.1 Pipe, valves & fittings",
"value": 5,
"label": "1.1.1 Line Pipe"

Reset the group field: optgroupField: 'optgroup_id' then added this after the json callback in load:

json.items.forEach((item) => {
this.addOptionGroup(item['optgroup_id'], { label: item['optgroup'] } );
  });
this.refreshItems()

I am also playing around with adding a second optgroups json array with just the groups to avoid cycling through all the options.

I hope there is a better answer - will leave this open for that. Hoping this helps someone else.

﹉夏雨初晴づ 2025-01-16 07:00:30

使用 TomSelect,您可以将远程数据格式化为选项组,如下所示:

new TomSelect('#id', {
  // these are the defaults
  optgroupField: 'optgroup',
  optgroupValueField: 'value',
  optgroupLabelField: 'label',
  // specify your option groups
  optgroups: [
    {value: 'foo', label: 'Foo Group'},
    {value: 'bar', label: 'Bar Group'},
  ],
});

然后,您的数据应包含每个项目的 optgroup 属性。

[
  {
    id: 1,
    name: "Result",
    optgroup: "foo",
  }
]

With TomSelect you can format remote data into option groups like this:

new TomSelect('#id', {
  // these are the defaults
  optgroupField: 'optgroup',
  optgroupValueField: 'value',
  optgroupLabelField: 'label',
  // specify your option groups
  optgroups: [
    {value: 'foo', label: 'Foo Group'},
    {value: 'bar', label: 'Bar Group'},
  ],
});

Your data should then include an optgroup property for each item.

[
  {
    id: 1,
    name: "Result",
    optgroup: "foo",
  }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文