我创建了一个搜索数据表的列,但是我不知道如何使用CRUD操作添加Action列。
这是我的刀片:users.blade.php。我没有发送html部分,因为我没有发现任何困难。
<script type="text/javascript">
$(document).ready(function(){
fetch_data();
function fetch_data(department = '')
{
$('#users_table').DataTable({
processing: true,
serverSide: true,
ajax: {
url:"{{ route('users.index') }}",
data: {department:department}
},
columns:[
{
data: 'id',
name: 'id'
},
{
data: 'name',
name: 'name'
},
{
data: 'deptName',
name: 'deptName',
orderable: false
},
{
data:'email',
name:'email'
},
{
data: 'action',
name: 'action',
orderable: false,
searchable: false
}
]
});
}
</script>
这是我的控制器:UsersController。请提供帮助,因为它确实很重要。
function index(Request $request)
{
if(request()->ajax())
{
if($request->department)
{
$data = DB::table('users')
->join('department', 'department.deptName', '=', 'users.department')
->select('users.id', 'users.name', 'users.email', 'department.deptName')
->where('users.department', $request->department);
}
else
{
$data = DB::table('users')
->join('department', 'department.deptName', '=', 'users.department')
->select('users.id', 'users.name', 'users.email', 'department.deptName');
}
return datatables()->of($data)->make(true);
}
$department = DB::table('department')
->select("*")
->get();
return view('users', compact('department'));
$data = User::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" class="edit btn btn-primary btn-sm">Read</a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
答案 0 :(得分:0)
您正在使用ajax数据表,我将用代码更新它,它可以正确使用过滤器并在laravel中搜索完全可自定义的服务器端代码以查找ajax数据表
请不要忘记包括数据表库和其他与数据表相关的
docker run -it --rm -p 8888:8080 <imageid or image name>
然后在您的控制器中为此添加两个操作
in your routes/web.php
Route::any('/admin/viewallregisteredusers', 'FrontusersController@viewAllRegisteredClients');
Route::any('/admin/viewallusers_datatable', 'FrontusersController@getAllUsers');
和
public function viewAllRegisteredClients(){
return view('frontusers.viewallusers');
}
您的视图文件如下所示
public function getAllUsers(Request $request){
$columns = array(
0 => 'id',
1 => 'user_name',
2 => 'user_email',
3 => 'user_type',
4 => 'created_at',
5 => 'updated_at',
);
$limit = $request->input('length');
$start = $request->input('start');
$order = $columns[$request->input('order.0.column')];
$dir = $request->input('order.0.dir');
$query = new Frontuser();
$totalTitles = $query->count();
if (empty($request->input('search.value'))) {
$titles = Frontuser::select('id','user_name','user_email','user_type','created_at','updated_at')->offset($start)
->limit($limit)
->orderBy($order, $dir)
->get();
$totalFiltered = $totalTitles;
$data = array();
}else{
$search = $request->input('search.value');
$titless = Frontuser::where(function ($q) use ($search)
{
$q->where('user_name', 'LIKE', "%{$search}%")
->orWhere('user_email', 'LIKE', "{$search}%")
->orWhere('user_type', 'LIKE', "{$search}%")
->orWhere('created_at', 'LIKE', "{$search}%");
});
$titles = $titless->offset($start)
->limit($limit)
->orderBy($order, $dir)
->get();
$totalFiltered = Frontuser::where(function ($q) use ($search)
{
$q->where('user_name', 'LIKE', "%{$search}%")
->orWhere('user_email', 'LIKE', "{$search}%")
->orWhere('user_type', 'LIKE', "{$search}%")
->orWhere('created_at', 'LIKE', "{$search}%");
})->count();
}
if (!empty($titles)) {
$data = array();
$count = 1;
foreach ($titles as $title) {
$id= base64_encode(serialize($title->id));
$nestedData['sno'] = $count;
$nestedData['user_name'] = ucfirst($title->user_name);
$nestedData['user_email'] = $title->user_email;
$nestedData['user_type'] = $title->user_type;
$nestedData['created_at'] = date('d-m-Y', strtotime($title->created_at));
$nestedData['updated_at'] = '<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Actions
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href='.action("FrontusersController@editUser",$id).'>Edit</a></li>
<li><a href='.action("FrontusersController@adminChangeUserPassword",$id).'>Change Password</a></li>
</ul>
</div>';
$data[] = $nestedData;
$count++;
}
}
$json_data = array(
"draw" => intval($request->input('draw')),
"recordsTotal" => intval($totalTitles),
"recordsFiltered" => intval($totalFiltered),
"data" => $data,
);
echo json_encode($json_data);
}
您可以根据需要进行修改