selelct2自动完成不显示数据库中的值

时间:2016-03-31 10:53:27

标签: javascript php laravel

这是我的代码。回调函数显示未定义的值。In image You can see I am getting undefined values which are initially fetched from database

这是HTML

<input type="text" name="city[]" class="itemSearch form-control" id="city_dropdown" value='{{$cities}}'> 

PHP代码:      $ cities = $ this-&gt; city-&gt; select('city_name','id') - &gt; whereIn('id',$ city) - &gt; get() - &gt; toArray();             $ cities = json_encode($ cities);

我在$ cities中得到[{“city_name”:“Badarpur Ra​​ilway Town”,“id”:321},{“city_name”:“Bahbari Gaon”,“id”:322}]。这是HTML文本框的值。

 $("#city_dropdown").select2({
      minimumInputLength: 2,
      tags: [],
      ajax: {
          url: adminPath+"pincode/getcitydropdown",
          dataType: 'json',
          type: "GET",
          quietMillis: 50,
          data: function (term) {
              return {
                  term: term
              };
          },
          results: function (data) {
              return {
                  results: $.map(data, function (item) {
                      return {
                          text: item.city_name,
                          id: item.id
                      }
                  })
              };
          }
      }, initSelection : function (element, callback) {
         var elementText = $(element).attr('value');
          callback(JSON.parse(elementText));
      }

  });

0 个答案:

没有答案