繁体   English   中英

angularjs ui-select-choices下拉aphabetical顺序取决于给定的输入

[英]angularjs ui-select-choices dropdown aphabetical order depending on the input given

我正在使用https://github.com/angular-ui/ui-select用于我正在处理的网站。 目前代码如下:

       <ui-select ng-model="model.states">
         <ui-select-match placeholder="State">
           {{$item.Name}}
         </ui-select-match>
         <ui-select-choices repeat="item.Code as item in statesArray | filter:$select.search">
            {{item.Name}}
         </ui-select-choices>
      </ui-select>

当我在输入字段中输入任何内容时,例如“a”,ui-select-choices显示包含“a”的所有字段(默认情况下)。 我想要做的是,我想显示所有以“a”开头的州。 如果输入框中未输入任何内容,则按字母顺序显示所有字段。 有人可以提出解决方案吗? 我看过其他一些帖子,但还没有人回答这个问题。 提前致谢。

这是一个例子,假设州阵列有加利福尼亚州,康涅狄格州,阿拉斯加州,亚利桑那州,纽约州等州。

如果输入字段中没有输入任何内容,则下拉列表应显示阿拉斯加州,亚利桑那州,加利福尼亚州,康涅狄格州,纽约州(可以使用orderBy过滤器实现)。

如果用户在输入字段中键入“a”,则下拉列表应显示亚利桑那州,亚利桑那州,并且没有其他字段,因为阿拉斯加州和亚利桑那州只有以“a”开头的字段。 如果用户键入“c”,则下拉列表应显示康涅狄格州加利福尼亚州。 如果用户键入“ca”,则下拉列表应仅显示加利福尼亚,因为它仅匹配给定的输入字符串。

如果我理解正确,我认为你需要做的就是添加

| 排序依据:“名称”

<ui-select-choices repeat="item.Code as item in statesArray | filter:$select.search | orderBy:'Name'">
{{item.Name}}
</ui-select-choices>

查看订单以获取更多信息

在挖掘了一点并且在Tomy和KreepN的帮助下,我找到了答案。 这里是,

   <ui-select ng-model="model.states">
     <ui-select-match placeholder="State">
       {{$item.Name}}
     </ui-select-match>
     <ui-select-choices repeat="item.Code as item in statesArray | startsWith:$select.search | orderBy:'Code'">
        {{item.Name}}
     </ui-select-choices>
   </ui-select>

startsWith Filter如下:

.filter('startsWith', function() {
    return function(array,search) {
        if(!_.isUndefined(array) && !_.isUndefined(search)) {
            var matches = [];
            for (var i = 0; i < array.length; i++) {
                if (array[i].Name.toUpperCase().indexOf(search.toUpperCase()) === 0 &&
                    search.length <= array[i].Name.length) {
                    matches.push(array[i]);
                }
            }
            return matches;
        }
    };
});

我尝试了上面的所有选项,但我只在这种情况下实现了结果。 这个对我有用:

<ui-select-choices repeat="group in groups | filter: $select.search | orderBy:['name']">
  {{ group.name }}
</ui-select-choices>

暂无
暂无

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

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