Ввод тегов Bootstrap с объектами в виде тегов typehead Бесплатный ввод не работает

Я использую ввод bootstrap-tags (объекты как теги — https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/) для ввода моих тегов. Он отлично работает с объектами в качестве тегов для автозаполнения предложений.

Но я тоже пытаюсь активировать свободный ввод. Таким образом, если тегов нет в списке автозаполнения, можно добавить новые теги.

Вот мой код:


var tags = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
   prefetch:  {
        ttl: 1,
        url:'[Json File Path]'
        }
});
tags.initialize();

var elt = $('input#tags');

elt.tagsinput({
  tagClass: 'badge badge-primary',
   itemValue: function(item) {
    return item.id;
  },
  itemText: 'text',
  freeInput: true,
   typeaheadjs: {
    name: 'tags',
    displayKey: 'text',
    source: tags.ttAdapter()
  } 
});

Пример Json:

[{"id":15,"text":"money"},{"id":14,"text":"startup"},{"id":13,"text":"inspiration"},{"id":12,"text":"success"},{"id":11,"text":"challenge"}]


person Shabnam Khan    schedule 03.04.2019    source источник
comment
попробуйте поставить freeInput после typeAhead   -  person Demonyowh    schedule 03.04.2019
comment
@Demonyowh, пытался, но у меня не сработало.   -  person Shabnam Khan    schedule 03.04.2019


Ответы (1)


jS

var citynames = new Bloodhound({
datumTokenizer: 

Bloodhound.tokenizers.obj.whitespace('name'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: {
    url: 'https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/assets/citynames.json',
    filter: function(list) {
      return $.map(list, function(cityname) {
        return { name: cityname }; });
    }
  }
});
citynames.initialize();

$('input').tagsinput({
  typeaheadjs: {
    name: 'citynames',
    displayKey: 'name',
    valueKey: 'name',
    source: citynames.ttAdapter()
  }
});

HTML

    <div class="container">
  <div class="col-12-xs">
    <div class="">
      <input type="text" value="Amsterdam,Washington" />
    </div>
  </div>
</div>

CSS

    .icon-github {
    background: no-repeat url('../img/github-16px.png');
    width: 16px;
    height: 16px;
}

.bootstrap-tagsinput {
    width: 100%;
}

.accordion {
    margin-bottom:-3px;
}

.accordion-group {
    border: none;
}

.twitter-typeahead .tt-query,
.twitter-typeahead .tt-hint {
    margin-bottom: 0;
}

.twitter-typeahead .tt-hint
{
    display: none;
}

.tt-menu {
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 1000;
    display: none;
    float: left;
    min-width: 160px;
    padding: 5px 0;
    margin: 2px 0 0;
    list-style: none;
    font-size: 14px;
    background-color: #ffffff;
    border: 1px solid #cccccc;
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 4px;
    -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
    background-clip: padding-box;
    cursor: pointer;
}

.tt-suggestion {
    display: block;
    padding: 3px 20px;
    clear: both;
    font-weight: normal;
    line-height: 1.428571429;
    color: #333333;
    white-space: nowrap;
}

.tt-suggestion:hover,
.tt-suggestion:focus {
  color: #ffffff;
  text-decoration: none;
  outline: 0;
  background-color: #428bca;
}

Codepen Codepen

person BlackFox    schedule 14.08.2019
comment
Спасибо за помощь. Свободный ввод работает только при использовании строки в качестве тега. Я изменил структуру кода, чтобы использовать строки в качестве параметров вместо массива. - person Shabnam Khan; 15.08.2019
comment
@ShabnamKhan, можешь отметить мой ответ, чтобы я мог получить баллы? - person BlackFox; 14.08.2020