【问题标题】:Textext plugin autosuggest generates list should begin with the searched string instead of contains as firstTextext 插件自动建议生成列表应该以搜索的字符串开头,而不是首先包含
【发布时间】:2018-01-18 09:06:12
【问题描述】:

我正在使用textext 插件进行自动完成、自动建议等。

我有一个 Destination 字段,即每当用户搜索国家或城市时的文本框,它应该在下拉列表中填充或建议 匹配 城市或国家。 p>

例如,如果用户搜索为 'Ameri'

电流输出

来自 textext 插件正在检索所有字符串包含搜索到的文本'Americ'

  • 北美
  • 南美洲
  • 美利坚合众国
  • 美国峡谷
  • 美国河

预期输出 想要匹配的字符串列表,以搜索的文本“Americ”开头,结果应该在前,后面的搜索文本包含该列表。

字符串以

开头
  • 美国峡谷
  • 美国河流

包含列表

  • 北美
  • 南美洲
  • 美利坚合众国

$('#textarea').textext({
         plugins: 'tags autocomplete filter focus',
         minLength: 3,
     })
     .bind('getSuggestions', function(e, data) {
         if (data.query.length) {
             var list = [
                     'United states of America',
                     'South America',
                     'India',
                     'Delhi',
                     'Pune',
                     'North America',
                     'American River ',
                     'American canyon'
                 ],
                 textext = $(e.target).textext()[0],
                 query = (data ? data.query : '') || '';
             $(this).trigger(
                 'setSuggestions', {
                     result: textext.itemManager().filter(list, query)
                 }
             );
         }
     });
<textarea id="textarea" class="example" rows="1"></textarea>

【问题讨论】:

    标签: javascript jquery autosuggest jquery-textext


    【解决方案1】:

    我已经通过在 texttext 插件上添加部分代码自己解决了这个问题,如果我的方法不正确,请纠正我。 这是它的演示 - http://holidays.coxandkings.com/

    $('#textarea')
        .textext({
            plugins : 'autocomplete filter',
            useSuggestionsToFilter : true
        })
        .bind('getSuggestions', function(e, data)
        {
            var list = [
                    'United states of America',
                    'South America',
                    'India',
                    'Delhi',
                    'Pune',
                    'North America',
                    'American River ',
                    'American canyon'
                ],
                textext = $(e.target).textext()[0],
                query = (data ? data.query : '') || ''
                ;
    
            // Implementing search functionality from the begining of the string, later dispalying the contains list.
            var filterItems = textext.itemManager().filter(list, query);
            var filteredResultCount = filterItems.length;
            var beginingMatches = [];
            var containsMatches = [];
            for (var i=0; i < filteredResultCount; i++) {
                if(filterItems[i].toLowerCase().indexOf(query.toLowerCase()) == 0 ) {
                    beginingMatches.push(filterItems[i]);
                }else{
                    containsMatches.push(filterItems[i]);
                }
            }
            var finalResult = beginingMatches.concat(containsMatches);
            $(this).trigger(
                'setSuggestions', { result : finalResult}
            );
        });
    

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 2014-10-15
      • 2023-04-10
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2015-01-14
      • 1970-01-01
      相关资源
      最近更新 更多