简体   繁体   中英

Formating JSON response for JqueryUI Autocomplete

I am trying to form that proper response to this but am having trouble. Standard is here: 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);

You need to supply an array of suggestions, now you just overwrite the suggestion the whole time and returning one of them. Something like this:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM