如何配置 dojo.FilteringSelect 来匹配通配符值?

发布于 2024-08-31 23:50:46 字数 1448 浏览 4 评论 0原文

下面是填充了用户数据的filteringSelect 的示例。我的目标是对显示的值执行通配符匹配。例如,如果用户输入“son”,则下拉列表匹配将为“homer simpSON”和“carl calSON”。 默认情况下,匹配仅位于标签的开头。

我尝试更改 dijit.byId('userselect').searchAttr,但将其设置为字符串以外的任何内容都会导致错误的行为。

<input id="userselect">

<script type="text/javascript">
    dojo.require("dijit.form.FilteringSelect");
    dojo.require("dojo.data.ItemFileReadStore");

    var user_data = {
        "itentifier":"user_id",
        "label":"label",
        "items":[
            {"first_name":"Waylon","last_name":"Smithers","label":"Waylon Smithers","user_id":7}
            ,{"first_name":"Carl","last_name":"Carlson","label":"Carl Carlson","user_id":6}
            ,{"first_name":"Homer","last_name":"Simpson","label":"Homer Simpson","user_id":4}
            ,{"first_name":"Lenny","last_name":"Leonard","label":"Lenny Leonard","user_id":5}
            ,{"first_name":"Montgomery","last_name":"Burns","label":"Montgomery Burns","user_id":8}
            ]
        };

    dojo.addOnLoad(function() {
        var userStore = new dojo.data.ItemFileReadStore({
            //url: "/user/lookup",
            data: user_data
        });
        var filteringSelect = new dijit.form.FilteringSelect({
            id: "userselect",
            name: "userselect",
            store: userStore,
            searchAttr: 'label' //["first_name", "last_name", "oasis"]
        },
        "userselect");
    });
</script>

Below is a sample of a filteringSelect populated with user data. My goal is to have perform wilcard match on the displayed values. for example, if the user types 'son', the dropdown matches will be "homer simpSON' and 'carl calSON'.
By default, the match will only be on the beginning of the label.

I tried changing dijit.byId('userselect').searchAttr, but setting it to anything but a string causes erronious behaviour.

<input id="userselect">

<script type="text/javascript">
    dojo.require("dijit.form.FilteringSelect");
    dojo.require("dojo.data.ItemFileReadStore");

    var user_data = {
        "itentifier":"user_id",
        "label":"label",
        "items":[
            {"first_name":"Waylon","last_name":"Smithers","label":"Waylon Smithers","user_id":7}
            ,{"first_name":"Carl","last_name":"Carlson","label":"Carl Carlson","user_id":6}
            ,{"first_name":"Homer","last_name":"Simpson","label":"Homer Simpson","user_id":4}
            ,{"first_name":"Lenny","last_name":"Leonard","label":"Lenny Leonard","user_id":5}
            ,{"first_name":"Montgomery","last_name":"Burns","label":"Montgomery Burns","user_id":8}
            ]
        };

    dojo.addOnLoad(function() {
        var userStore = new dojo.data.ItemFileReadStore({
            //url: "/user/lookup",
            data: user_data
        });
        var filteringSelect = new dijit.form.FilteringSelect({
            id: "userselect",
            name: "userselect",
            store: userStore,
            searchAttr: 'label' //["first_name", "last_name", "oasis"]
        },
        "userselect");
    });
</script>

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

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

发布评论

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

评论(2

初心 2024-09-07 23:50:46

您需要设置 queryExpr 并将 autoComplete 设置为 false

var filteringSelect = new dijit.form.FilteringSelect({
  id: "userselect",
  name: "userselect",
  store: userStore,
  searchAttr: 'label',
  queryExpr: '*${0}*',
  autoComplete: false
},"userselect");

queryExpr 的 Dojo 文档:

这指定了什么查询发送到数据存储,基于什么
用户已输入。更改此表达式将修改是否
结果只是完全匹配、“开头为”匹配等。
dojo.data 查询表达式模式。 ${0} 将替换为
用户文本。 * 用于通配符。

${0}* 表示“开头为”
*${0}* 表示“包含”
${0} 表示“是”

You'll need to set queryExpr and set autoComplete to false

var filteringSelect = new dijit.form.FilteringSelect({
  id: "userselect",
  name: "userselect",
  store: userStore,
  searchAttr: 'label',
  queryExpr: '*${0}*',
  autoComplete: false
},"userselect");

Dojo documentation for queryExpr:

This specifies what query is sent to the data store, based on what the
user has typed. Changing this expression will modify whether the
results are only exact matches, a "starting with" match, etc.
dojo.data query expression pattern. ${0} will be substituted for the
user text. * is used for wildcards.

${0}* means "starts with"
*${0}* means "contains"
${0} means "is"

望喜 2024-09-07 23:50:46

看看 queryExpr

Have a look at queryExpr

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