I want to do a search in several columns. Something like that.
But I do not know how to do it in Laravel since it is managed through MVC.
The DATATABLES page suggests that you enter this code.
$(document).ready(function() {
// Setup - add a text input to each footer cell
$('#example tfoot th').each( function () {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
// DataTable
var table = $('#example').DataTable();
// Apply the search
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
});
The table I currently have looks like this.
Driver's code.
<?php
/**
* Controller genrated using LaraAdmin
* Help: http://laraadmin.com
*/
namespace App\Http\Controllers\LA;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use DB;
use Validator;
use Datatables;
use Collective\Html\FormFacade as Form;
use Dwij\Laraadmin\Models\Module;
use Dwij\Laraadmin\Models\ModuleFields;
use App\Models\Computer;
class ComputersController extends Controller
{
public $show_action = true;
public $view_col = 'n_serie';
public $listing_cols = ['id', 'empresa', 'departamento', 'codigo', 'marca', 'modelo', 'n_serie', 'tipo', 'procesador', 'vel_procesador'];
public function __construct() {
// Field Access of Listing Columns
if(\Dwij\Laraadmin\Helpers\LAHelper::laravel_ver() == 5.3) {
$this->middleware(function ($request, $next) {
$this->listing_cols = ModuleFields::listingColumnAccessScan('Computers', $this->listing_cols);
return $next($request);
});
} else {
$this->listing_cols = ModuleFields::listingColumnAccessScan('Computers', $this->listing_cols);
}
}
/**
* Display a listing of the Computers.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$module = Module::get('Computers');
if(Module::hasAccess($module->id)) {
return View('la.computers.index', [
'show_actions' => $this->show_action,
'listing_cols' => $this->listing_cols,
'module' => $module
]);
} else {
return redirect(config('laraadmin.adminRoute')."/");
}
}
/**
* Show the form for creating a new computer.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created computer in database.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
if(Module::hasAccess("Computers", "create")) {
$rules = Module::validateRules("Computers", $request);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$insert_id = Module::insert("Computers", $request);
return redirect()->route(config('laraadmin.adminRoute') . '.computers.index');
} else {
return redirect(config('laraadmin.adminRoute')."/");
}
}
/**
* Display the specified computer.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
if(Module::hasAccess("Computers", "view")) {
$computer = Computer::find($id);
if(isset($computer->id)) {
$module = Module::get('Computers');
$module->row = $computer;
return view('la.computers.show', [
'module' => $module,
'view_col' => $this->view_col,
'no_header' => true,
'no_padding' => "no-padding"
])->with('computer', $computer);
} else {
return view('errors.404', [
'record_id' => $id,
'record_name' => ucfirst("computer"),
]);
}
} else {
return redirect(config('laraadmin.adminRoute')."/");
}
}
/**
* Show the form for editing the specified computer.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
if(Module::hasAccess("Computers", "edit")) {
$computer = Computer::find($id);
if(isset($computer->id)) {
$module = Module::get('Computers');
$module->row = $computer;
return view('la.computers.edit', [
'module' => $module,
'view_col' => $this->view_col,
])->with('computer', $computer);
} else {
return view('errors.404', [
'record_id' => $id,
'record_name' => ucfirst("computer"),
]);
}
} else {
return redirect(config('laraadmin.adminRoute')."/");
}
}
/**
* Update the specified computer in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
if(Module::hasAccess("Computers", "edit")) {
$rules = Module::validateRules("Computers", $request, true);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();;
}
$insert_id = Module::updateRow("Computers", $request, $id);
return redirect()->route(config('laraadmin.adminRoute') . '.computers.index');
} else {
return redirect(config('laraadmin.adminRoute')."/");
}
}
/**
* Remove the specified computer from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
if(Module::hasAccess("Computers", "delete")) {
Computer::find($id)->delete();
// Redirecting to index() method
return redirect()->route(config('laraadmin.adminRoute') . '.computers.index');
} else {
return redirect(config('laraadmin.adminRoute')."/");
}
}
/**
* Datatable Ajax fetch
*
* @return
*/
public function dtajax()
{
$values = DB::table('computers')->select($this->listing_cols)->whereNull('deleted_at');
$out = Datatables::of($values)->make();
$data = $out->getData();
$fields_popup = ModuleFields::getModuleFields('Computers');
for($i=0; $i < count($data->data); $i++) {
for ($j=0; $j < count($this->listing_cols); $j++) {
$col = $this->listing_cols[$j];
if($fields_popup[$col] != null && starts_with($fields_popup[$col]->popup_vals, "@")) {
$data->data[$i][$j] = ModuleFields::getFieldValue($fields_popup[$col], $data->data[$i][$j]);
}
if($col == $this->view_col) {
$data->data[$i][$j] = '<a href="'.url(config('laraadmin.adminRoute') . '/computers/'.$data->data[$i][0]).'">'.$data->data[$i][$j].'</a>';
}
// else if($col == "author") {
// $data->data[$i][$j];
// }
}
if($this->show_action) {
$output = '';
if(Module::hasAccess("Computers", "edit")) {
$output .= '<a href="'.url(config('laraadmin.adminRoute') . '/computers/'.$data->data[$i][0].'/edit').'" class="btn btn-warning btn-xs" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-edit"></i></a>';
}
if(Module::hasAccess("Computers", "delete")) {
$output .= Form::open(['route' => [config('laraadmin.adminRoute') . '.computers.destroy', $data->data[$i][0]], 'method' => 'delete', 'style'=>'display:inline']);
$output .= ' <button class="btn btn-danger btn-xs" type="submit"><i class="fa fa-times"></i></button>';
$output .= Form::close();
}
$data->data[$i][] = (string)$output;
}
}
$out->setData($data);
return $out;
}
}
View code
@extends("la.layouts.app")
@section("contentheader_title", "Computers")
@section("contentheader_description", "Computers listing")
@section("section", "Computers")
@section("sub_section", "Listing")
@section("htmlheader_title", "Computers Listing")
@section("headerElems")
@la_access("Computers", "create")
<div class="btn-group">
<button class="btn btn-success btn-sm pull-right" data-toggle="modal" data-target="#AddModal">Add Computer</button>
<button class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggole Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu" id="export-menu">
<li id="export-to-excel"> <a href="#"></a></li>
</ul>
</div>
@endla_access
@endsection
@section("main-content")
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="box box-success">
<!--<div class="box-header"></div>-->
<div class="box-body">
<table id="example1" class="display table">
<thead>
<tr class="success">
@foreach( $listing_cols as $col )
<th>{{ $module->fields[$col]['label'] or ucfirst($col) }}</th>
@endforeach
@if($show_actions)
<th>Actions</th>
@endif
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
@la_access("Computers", "create")
<div class="modal fade" id="AddModal" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Computer</h4>
</div>
{!! Form::open(['action' => 'LA\ComputersController@store', 'id' => 'computer-add-form']) !!}
<div class="modal-body">
<div class="box-body">
@la_form($module)
{{--
@la_input($module, 'empresa')
@la_input($module, 'departamento')
@la_input($module, 'codigo')
@la_input($module, 'marca')
@la_input($module, 'modelo')
@la_input($module, 'n_serie')
@la_input($module, 'tipo')
@la_input($module, 'procesador')
@la_input($module, 'vel_procesador')
@la_input($module, 'memoria_ram1')
@la_input($module, 'memoria_ram2')
@la_input($module, 'memoria_ram3')
@la_input($module, 'disco_duro1')
@la_input($module, 'disco_duro2')
@la_input($module, 'sistema_operativo')
@la_input($module, 'g_inicio')
@la_input($module, 'g_final')
--}}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
{!! Form::submit( 'Submit', ['class'=>'btn btn-success']) !!}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
@endla_access
@endsection
@push('styles')
<link rel="stylesheet" type="text/css" href="{{ asset('la-assets/plugins/datatables/datatables.min.css') }}"/>
@endpush
@push('scripts')
<script src="{{ asset('la-assets/plugins/datatables/datatables.min.js') }}"></script>
<script>
$(function () {
$("#example1").DataTable({
processing: true,
serverSide: true,
ajax: "{{ url(config('laraadmin.adminRoute') . '/computer_dt_ajax') }}",
language: {
lengthMenu: "_MENU_",
search: "_INPUT_",
searchPlaceholder: "Search"
},
@if($show_actions)
columnDefs: [ { orderable: false, targets: [-1] }],
@endif
});
$("#computer-add-form").validate({
});
});
</script>
@endpush