weixin_33720452 2015-02-04 08:31 采纳率: 0%
浏览 16

Laravel Ajax下拉过滤器

I´m trying to make a filter using Ajax, I want to display images based on the chosen category in the dropdown, whenever it changes. Right now I´m displaying all images in my view and I want to make an Ajax call that returns the filtered $images array to display in the view. But I´m stuck. I can't figure out how to return the data from the controller correct, so I can access it in the view.

Form with dropdown in the index.blade.php

{{ Form::open( array(
  'route' => 'index.filter',
  'method' => 'post',
  'id' => 'form-filter'
) ) }}

{{ Form::select('category', $categories, 'default', array('id' => 'categories')) }}

{{ Form::close() }}

routes

Route::post( 'filter', array(
  'as' => 'index.filter',
  'uses' => 'MyController@filter'
) );

.js

$( '#form-filter' ).change( function() {

    $.post(
        $( this ).prop( 'action' ),
        {
            "id": $( '#categories' ).val()
        },
        function( data ) {
            alert(data);
        },
        'json'
    );
    return false;
} );

controllers

public function filter()
{
    Log::info(Input::get('id'));

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

    if($id)
      $images = Image::where('category_id', '=', $id);

    return Response::json($images);
}

public function index()
{
   $images = Image::all();

   $categories = ..\Models\Category::lists('name', 'id');

   return View::make('index', array('images' => $images, 'categories'=>$categories));
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改