用ajax轻松展示?

时间:2017-08-01 13:38:34

标签: javascript jquery sql ajax laravel

我在控制器中有6个功能,可以通过ASC和DESC 3字段进行排序。

功能:

 public function orderByIdAsc(){
        $projects = DB::table('projects')->orderBy('id','asc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 

    }
    public function orderByIdDesc(){
        $projects = DB::table('projects')->orderBy('id','desc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 
    }
    public function orderByOrderAsc(){
        $projects = DB::table('projects')->orderBy('order','asc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 
    }
    public function orderByOrderDesc(){
        $projects = DB::table('projects')->orderBy('order','desc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 
    }
    public function orderByPublicAsc(){
        $projects = DB::table('projects')->orderBy('public','asc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 
    }
    public function orderByPublicDesc(){
        $projects = DB::table('projects')->orderBy('public','desc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 
    } 

她也有6条路线:

Route::prefix('admin')->group(function () {

    Route::get('project/orderByIdAsc', ['uses' => 'AdminController@orderByIdAsc', 'as' => 'admin.projects.orderByIdAsc']);
        Route::get('project/orderByIdDesc', ['uses' => 'AdminController@orderByIdDesc', 'as' => 'admin.projects.orderByIdDesc']);
        Route::get('project/orderByOrderAsc', ['uses' => 'AdminController@orderByOrderAsc', 'as' => 'admin.projects.orderByOrderAsc']);
        Route::get('project/orderByOrderDesc', ['uses' => 'AdminController@orderByOrderDesc', 'as' => 'admin.projects.orderByOrderDesc']);
        Route::get('project/orderByPublicAsc', ['uses' => 'AdminController@orderByPublicAsc', 'as' => 'admin.projects.orderByPublicAsc']);
        Route::get('project/orderByPublicDesc', ['uses' => 'AdminController@orderByPublicDesc', 'as' => 'admin.projects.orderByPublicDesc']);
});

目前我打电话给这里的功能:

<th><a href="{{ route('admin.projects.orderByIdAsc')}}"><span class="glyphicon glyphicon-arrow-up" id="orderByIdAsc"></span></a>Id<a href="{{ route('admin.projects.orderByIdDesc')}}"><span class="glyphicon glyphicon-arrow-down" id="orderByIdDesc"></span></a></th>
<th>Slug</th>
<th><a href="{{ route('admin.projects.orderByOrderAsc')}}"><span class="glyphicon glyphicon-arrow-up" id="orderByOrderAsc"></span></a>Order<a href="{{ route('admin.projects.orderByOrderDesc')}}"><span class="glyphicon glyphicon-arrow-down" id="orderByOrderDesc"></span></a></th>
<th><a href="{{ route('admin.projects.orderByPublicAsc')}}"><span class="glyphicon glyphicon-arrow-up" id="orderByPublicAsc"></span></a>Public<a href="{{ route('admin.projects.orderByPublicDesc')}}"><span class="glyphicon glyphicon-arrow-down" id="orderByPublicDesc"></span></a></th>

使用ajax比使用6个div并隐藏/显示它更容易吗?

非常感谢!

1 个答案:

答案 0 :(得分:1)

对于这种情况,您有两种选择:

  • 第一个:使用路径参数

您可以将所有订购路线合并为一个:

Route::prefix('admin')->group(function () {

    Route::get('project/order/{field}/{order}', [
                'uses' => 'AdminController@order', 
                'as' => 'admin.projects.order'
            ]);

});

AdminController

public function order($field, $order) 
{
    $projects = DB::table('projects')->orderBy($field,$order)->get();
    return view('cms.public.views.projects.menu') 
               ->withProjects($projects);
}

在视图中:

<a href="{{ route('admin.projects.order', ['field' => 'id', 'order' => 'asc']) }}">Order by ID</a>
  • 第二个:使用查询字符串

路线:

Route::prefix('admin')->group(function () {

    Route::get('project/order', [
                'uses' => 'AdminController@order', 
                'as' => 'admin.projects.order'
            ]);

});

AdminController

public function order(\Illuminate\Http\Request $request) 
{
    $order = $request->query('order');
    $field = $request->query('field');
    $projects = DB::table('projects')->orderBy($field,$order)->get();
    return view('cms.public.views.projects.menu') 
               ->withProjects($projects);
}

在视图中:

<a href="{{route('admin.projects.order', ['field' => 'id', 'order' => 'desc'])}}">Order by ID</a>