我正在使用带有 ajax 和服务器端脚本的 yajra 数据表。 我的搜索工作正常,但当我点击排序按钮时什么都不会发生。
我的控制器
$suppliers = DB::table("suppliers")
->select(
'suppliers.id',
'suppliers.created_at',
'suppliers.email',
'suppliers.status',
'companies.id as company_id',
'companies.name',
'company_categories.title',
DB::raw('
(select COUNT(promotions.company_id) FROM promotions WHERE promotions.status = "pending" AND promotions.company_id = companies.id) as total')
)
->leftJoin('companies','companies.supplier_id','=','suppliers.id')
->leftJoin('company_sub_categories','company_sub_categories.id','=','companies.sub_category_id')
->leftJoin('company_categories','company_categories.id','=','company_sub_categories.company_category_id')
->groupBy('suppliers.id', 'suppliers.created_at', 'suppliers.first_name','suppliers.last_name',
'suppliers.phone','suppliers.email','suppliers.status','suppliers.image_path','companies.name','companies.id','companies.name','company_categories.title')
->orderBy('suppliers.id','DESC');
return Datatables::of($suppliers)
->add_column('next_payment_date', function($suppliers){
return date(config('constant.DATE_FORMAT'), strtotime($suppliers->created_at));
})
->filter(function ($query) use ($request) {
if (@$request->search['value']) {
$query->where('suppliers.email', 'like', "%{$request->search['value']}%")
->orWhere('companies.name', 'like', "%{$request->search['value']}%")
->orWhere('company_categories.title', 'like', "%{$request->search['value']}%")
->orWhere('suppliers.id', 'like', "%{$request->search['value']}%");
}
})
->make(true);
我的js
$(function() {
oTable = $('#supplier').DataTable({
"processing": true,
serverSide: true,
"pageLength": 25,
ordering: true,
searching: true,
"sDom":"tpr",
"dom": 'difrtp',
ajax: '{!! route('get-supplier') !!}',
columns: [
{ data: 'name', name: 'name' },
{ data: 'email', name: 'email' },
{ data: 'title', name: 'title' },
{ data: 'created_at', name: 'created_at' },
{ data: 'status', name: 'first_name' },
{ data: 'next_payment_date', name: 'next_payment_date' },
{ data: 'action', name: 'action' }
]
});
}
你有一个命令的命令。 它将覆盖所有排序功能。 尝试删除它并评论你的结果。
->orderBy('suppliers.id','DESC');
解决了。 谢谢@ChaosPower
我在这个查询中遇到了同样的问题:
return self::select($select)
->where('language', app()->getLocale())
->leftJoin('content_translations', 'content_translations.content_id', 'contents.id')
->latest();
我通过在最后添加 get() 函数来更改我的,这也解决了我的问题。
return self::select($select)
->where('language', app()->getLocale())
->leftJoin('content_translations', 'content_translations.content_id', 'contents.id')
->latest()
->get();
最有用的评论
你有一个命令的命令。 它将覆盖所有排序功能。 尝试删除它并评论你的结果。