使用Twitter-Bootstrap-Typeahead和Laravel时遇到麻烦

时间:2013-07-15 21:44:05

标签: ajax twitter-bootstrap laravel laravel-4 bootstrap-typeahead

我正在尝试在我的应用程序中使用typeahead实现全文自动完成搜索。但是我弄清楚它是如何工作的。

如果我手动输入数据源,我可以得到建议。但是如果我尝试使用AJAX调用,我就无法得到结果。我的另一个问题是,我是否会从控制器中的typeahead查询中分离搜索查询?

感谢任何帮助。

我的论坛观点:

{{ Form::open(array('url' => 'searchblogs','method' => 'post','id' => "search", 'class' => "input-append")) }}

<input class="typeahead"  size="16" type="text" value="Arama..."
       onfocus="if(this.value=='Search...') this.value=''" onblur="if(this.value=='') this.value='Arama...'"
       data-provide="typeahead" data-items="4"  data-page="url('typequery')" />

<input class="btn search-bt" type="submit" name="submit" value="" />
{{ Form::close() }}

我的Jquery:

<script>

        $('.typeahead').typeahead({
            source : function(typeahead, query){

                    $.ajax({
                        url      : 'typequery',
                        type     : 'POST',
                        data     : { query : query, column : 'title' },
                        dataType : 'json',
                        async    : true,
                        success  : function(data) {
                            return process(data.titles);
                        }
                    });
                }

        });

</script>

我的控制器是:

 public function postTypeahead()
    {
        // Get the query strings.
        //
        $column = Input::get('column');
        $query  = Input::get('query');

        // Check if the column name is valid and make sure we have a query.
        //
        if ( in_array( $column, $this->valid_filters ) and strlen($query) >= 1 ):
            // Initiate an empty array.
            //
            $data = array();

            // Search the database.
            //
            $results = Post::select( $column )->where( $column, 'LIKE', '%' . $query . '%')->get();

            // Loop through the results.
            //
            foreach ( $results as $result ):
                $data[] = $result->$column;
            endforeach;
        endif;

        // Return a response.
        //
        return Response::json($data);
    }

0 个答案:

没有答案
相关问题