4

在下面的演示中,插入“Alaska”值后,源会更新,以便自动完成不再显示 Alaska 值。

var newSource = this.source
                    .slice(0,pos)
                    .concat(this.source.slice(pos+1));
this.source = newSource;

无论如何,如果我从文本区域中删除阿拉斯加,该值Alaska应该再次显示在源中。
如果用户从 textarea 中删除数据,任何提示如何恢复源数据?

我的想法是从

$('.typeahead').on('change', function () { })

有什么提示吗?

PS:
我正在使用 jquery 和下划线

4

3 回答 3

2

您可能应该更改您的matcher功能以测试已选择的状态:

var tabPresentStates = this.query.split(','),
    nbPresentStates = tabPresentStates.length;
for(var iState = 0; iState < nbPresentStates; iState++) {
   if(item === tabPresentStates[iState].trim())
       return false;
}

看到这个小提琴

于 2012-11-26T15:55:25.767 回答
1

您可以使用排序器排除您已经选择的值,而不是更改源。

http://jsfiddle.net/BwDmM/71/

PS 我可能会将您的代码包含在 Jasny 的扩展 Bootstrap http://jasny.github.com/bootstrap的下一个版本中:)

!function(source) {
    function extractor(query) {
        var result = /([^,]+)$/.exec(query);
        if(result && result[1])
            return result[1].trim();
        return '';
    }

    $('.typeahead').typeahead({
        source: source,
        updater: function(item) {
            return this.$element.val().replace(/[^,]*$/,'')+item+',';
        },
        matcher: function (item) {
          var tquery = extractor(this.query);
          if(!tquery) return false;
          return ~item.toLowerCase().indexOf(tquery)
        },
        highlighter: function (item) {

          var query = extractor(this.query).replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
          return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
            return '<strong>' + match + '</strong>'
          })
        },
        sorter: function(items) {
          var beginswith = []
            , caseSensitive = []
            , caseInsensitive = []
            , existing = $.each(this.$element.val().split(','), function(i, val) { return val.trim() })
            , item

          while (item = items.shift()) {
            if ($.inArray(item, existing) >= 0) continue;

            if (!item.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item)
            else if (~item.indexOf(this.query)) caseSensitive.push(item)
            else caseInsensitive.push(item)
          }

          return beginswith.concat(caseSensitive, caseInsensitive)              
        }
    });

}(["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Dakota","North Carolina","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]);
于 2012-11-26T15:49:59.103 回答
0

同意@SamuelCaillerie 的使用方法matcher- 这只是使用您的提取器功能。所以:

    updater: function(item) {
        return this.$element.val().replace(/[^,]*$/,'')+item+',';
    },
    matcher: function (item) {
      var previouslySelected = (this.query.indexOf(item) != -1); 
      var tquery = (previouslySelected ? "" : extractor(this.query));
      if(!tquery) return false;
      return ~item.toLowerCase().indexOf(tquery)
    },
于 2012-11-26T16:09:45.207 回答