4

I just migrated my backoffice from Boostrap 2 to Boostrap 3.

My typeahead instruction give me some problems.

On bootstrap v2 I had this :

var typeaheadSettings = {

    source: function (query, process) {

         list = [];

         return $.ajax({

            minLength: 3,
            item: 10,
            url: "/ajax/articles/",
            type: 'POST',
            data : { query: query },
            dataType: 'json',
            success: function (result) {

            var resultList = result.aaData.map(function (item) {

               list[item.name + ' - ' + item.code + ' (' + item.category + ')'] = item.id;
               return item.name + ' - ' + item.code + ' (' + item.category + ')';

            });

            return process(resultList);

         }

         }); 
                },
                updater: function (item) {
                    $("#parent").val(list[item]);
                    $(this).attr("placeholder",item);
                }

        };

for now, with Bootstrap 3 and typeahead (v. 0.9.3) included explicitly, I am on this part :

     $(".typeahead").typeahead({

         name : 'resultArticle',
         remote : {

          url: '/ajax/articles?query=%QUERY',
          filter: function(data) {

              var resultList = data.aaData.map(function (item) {

              return item.name;

          });

          return process(resultList);

        }
     }

});

The call to the json is ok, but there is no return, I have no idea what I can do to debug/find solution.

Thanks!

4

1 回答 1

6

首先,您可以考虑使用https://github.com/bassjobsen/Bootstrap-3-Typeahead

您应该检查您的resultList或结果是否process(resultList)具有以下格式:

组成数据集的各个单元称为基准。数据的规范形式是具有值属性和标记属性的对象。value 是表示数据基础值的字符串,tokens 是单字字符串的集合,可帮助 typeahead.js 将数据与给定查询匹配。

为了模仿你的/ajax/articles?query我使用:

<?php
class names
{
    var $name;

    function __construct($name)
    {
        $this->name = $name;
    }   
}   

$data=array();
$data['aaData'] = array();
foreach (array('kiki','dries','wolf') as $name)
{
    $data['aaData'][] = new names($name); 
}


echo json_encode($data);
exit;

此端点始终返回独立于查询的三个名称的列表。此列表应显示在下拉列表中。

您的(采用)js 代码:

     $(".typeahead").typeahead({

         name : 'resultArticle',
         remote : {

          url: 'search.php?query=%QUERY',
          filter: function(data) {

              var resultList = data.aaData.map(function (item) {
              return item.name;

          });
          console.log(resultList);
          return resultList;

        },

     }

});

当我运行这console.log(resultList);给出了["kiki", "dries", "wolf"]。适合数据格式的字符串数组。typeahead 下拉菜单也显示这些名称。(不要忘记包含来自:https ://github.com/jharding/typeahead.js-bootstrap.css https://github.com/bassjobsen/typeahead.js-bootstrap-css的 CSS )

请注意,您不需要您的return process(resultList);

于 2013-09-12T22:47:51.333 回答