格式化Jquery UI自动完成的JSON响应

时间:2014-02-23 17:47:47

标签: php jquery ajax json laravel

我正在尝试对此形成适当的回应,但我遇到了麻烦。标准在这里:https://github.com/devbridge/jQuery-Autocomplete

    $query = Input::get('query');

    $query = $query . "%";
    $categories = Category::select('name', 'id')->where('name', 'like', $query)->get();

    //$suggestion = array();
    foreach($categories as $category){

        $suggestion['value'] = $item['value'] = $category->name;
        $suggestion['data'] = $item['data'] = $category->id;


    }
    $suggestions = array('suggestions' => $suggestion);





    return Response::json($suggestions);

1 个答案:

答案 0 :(得分:1)

您需要提供一系列建议,现在您只需覆盖整个建议并返回其中一条建议。像这样:

$suggestions = array();
foreach($categories as $category){

    $suggestion['value'] = $item['value'] = $category->name;
    $suggestion['data'] = $item['data'] = $category->id;

    $suggestions[] = $suggestion;
}
return array('suggestions' => $suggestions);