6

「ユーザー」を「もの」に関連付ける「新しいものを追加」フォームがあります。

Twitter Bootstrap Typeahead を使用して、入力時にユーザーのリストを表示しています。
ユーザーがリストにある場合は、それを選択できます。

しかし、ユーザーが存在しない場合は、「新しいユーザーの作成」リンクを表示したいと思います。

したがって、基本的に、..のどこかに「一致しない」関数の形式が必要.typeahead()です。私はそれを解決することはできません。

4

2 に答える 2

4

typehead クラスを拡張します。

var newusertext = 'New user';
var newuserlink = '/add/user/0';

$.fn.typeahead.Constructor.prototype.process = function (items) {
      var that = this

      items = $.grep(items, function (item) {
        return that.matcher(item)
      })

      items = this.sorter(items)

      if (!items.length) {
         return this.rendernonmatch().show()
      }

      return this.render(items.slice(0, this.options.items)).show()
    } 


$.fn.typeahead.Constructor.prototype.rendernonmatch = function () {
      var that = this
      var items = new Array();
      items.push(newusertext)
      items = $(items).map(function (i, item) {
        i = $(that.options.item).attr('data-value', item)
        i.find('a').html(that.highlighter(item))
        return i[0]
      })
      items.first().addClass('active')
      this.$menu.html(items)
      return this
    }

/* redirect to the registration page to add a new user */    
$.fn.typeahead.Constructor.prototype.updater = function (item) {
      if(item === newusertext){ window.location =  newuserlink; return}
      return item
    }      

参照: http://bootply.com/66630

于 2013-07-07T13:06:34.363 に答える