如何在 yajra 数据表中搜索 "concatenated column"?
How to search "concatenated column" in yajra datable?
我在我的控制器上使用数据表 class 并用它来绘制数据表,其中我有一个连接的 "Name" 列。所以默认搜索 dosent 工作。在这种情况下我找不到自定义过滤器的示例
//这是数据表。我尝试在 dataTable 函数上使用 filterColumn(),但它说找不到方法。
namespace App\DataTables;
use App\User;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Services\DataTable;
class UsersDatatable extends DataTable
{
public function dataTable($query)
{
return datatables($query)
->addColumn('action', function ($users) {
return '<a href="'.route('users.edit',$users->id).'" class="btn btn-sm btn-primary" style="margin:3px"><i
class="glyphicon glyphicon-edit"></i> Edit</a></a> <a href="'.route('users.destroy',$users->id).'" class="btn btn-sm btn-danger"><i class="glyphicon glyphicon-remove"></i> Delete</a>';
});
}
public function query(User $model)
{
return $model->newQuery()->select('id', DB::raw('CONCAT(FirstName," ", Surname ) AS name'), 'email', 'phone_no');
}
public function html()
{
//
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
->addAction(['width' => '130px'])
->parameters($this->getBuilderParameters());
}
protected function getColumns()
{
return [
'id',
'name',
'email',
'phone_no',
];
}
protected function filename()
{
return 'Users_' . date('YmdHis');
}
}
这是控制器
namespace App\Http\Controllers\Backend;
use App\DataTables\UsersDatatable;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;
class RegisterController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(UsersDatatable $user)
{
return $user->render('backend.Users.indexUser');
}
//这是视图
<div class="card-header">
Index User
</div>
<div class="card-body">
<h5 class="card-title">Table</h5>
{!! $dataTable->table(['class' => 'table table-striped ']) !!}
</div>
</div>
@endsection
@section('scripts')
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<!-- from dataTables push-->
<script src="https://cdn.datatables.net/buttons/1.4.2/js/dataTables.buttons.min.js"></script>
<script src="{{asset('vendor/datatables/buttons.server-side.js')}}"></script>
{!! $dataTable->scripts() !!}```
在 addColumn
或 editColumn
之后添加您的 filterColumn
(或在 datatables($query)
之后的其他地方):
datatables($query)
->addColumn('action', function ($obj) {
return '';
})
->filterColumn('name', function ($query, $keyword) {
$keywords = trim($keyword);
$query->whereRaw("CONCAT(first_name, sur_name) like ?", ["%{$keywords}%"]);
});
我在我的控制器上使用数据表 class 并用它来绘制数据表,其中我有一个连接的 "Name" 列。所以默认搜索 dosent 工作。在这种情况下我找不到自定义过滤器的示例
//这是数据表。我尝试在 dataTable 函数上使用 filterColumn(),但它说找不到方法。
namespace App\DataTables;
use App\User;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Services\DataTable;
class UsersDatatable extends DataTable
{
public function dataTable($query)
{
return datatables($query)
->addColumn('action', function ($users) {
return '<a href="'.route('users.edit',$users->id).'" class="btn btn-sm btn-primary" style="margin:3px"><i
class="glyphicon glyphicon-edit"></i> Edit</a></a> <a href="'.route('users.destroy',$users->id).'" class="btn btn-sm btn-danger"><i class="glyphicon glyphicon-remove"></i> Delete</a>';
});
}
public function query(User $model)
{
return $model->newQuery()->select('id', DB::raw('CONCAT(FirstName," ", Surname ) AS name'), 'email', 'phone_no');
}
public function html()
{
//
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
->addAction(['width' => '130px'])
->parameters($this->getBuilderParameters());
}
protected function getColumns()
{
return [
'id',
'name',
'email',
'phone_no',
];
}
protected function filename()
{
return 'Users_' . date('YmdHis');
}
}
这是控制器
namespace App\Http\Controllers\Backend;
use App\DataTables\UsersDatatable;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;
class RegisterController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(UsersDatatable $user)
{
return $user->render('backend.Users.indexUser');
}
//这是视图
<div class="card-header">
Index User
</div>
<div class="card-body">
<h5 class="card-title">Table</h5>
{!! $dataTable->table(['class' => 'table table-striped ']) !!}
</div>
</div>
@endsection
@section('scripts')
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<!-- from dataTables push-->
<script src="https://cdn.datatables.net/buttons/1.4.2/js/dataTables.buttons.min.js"></script>
<script src="{{asset('vendor/datatables/buttons.server-side.js')}}"></script>
{!! $dataTable->scripts() !!}```
在 addColumn
或 editColumn
之后添加您的 filterColumn
(或在 datatables($query)
之后的其他地方):
datatables($query)
->addColumn('action', function ($obj) {
return '';
})
->filterColumn('name', function ($query, $keyword) {
$keywords = trim($keyword);
$query->whereRaw("CONCAT(first_name, sur_name) like ?", ["%{$keywords}%"]);
});