ajax attempt
This commit is contained in:
49
app/Http/Controllers/LiveSearch.php
Normal file
49
app/Http/Controllers/LiveSearch.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
use DB;
|
||||
|
||||
class LiveSearch extends Controller
|
||||
{
|
||||
public function index() {
|
||||
return view('ajax.live_search');
|
||||
}
|
||||
|
||||
public function action(Request $request) {
|
||||
if($request->ajax()) {
|
||||
$query = $request->get('query');
|
||||
if($query != '') {
|
||||
$data = User::where('name', 'like', '%' . $query . '%')->get();
|
||||
} else {
|
||||
$data = User::all()->orderBy('name', 'desc')->get();
|
||||
}
|
||||
|
||||
$total_row = $data->count();
|
||||
if($total_row > 0) {
|
||||
foreach($data as $row) {
|
||||
$output .= '
|
||||
<tr>
|
||||
<td>' . $row->name . '</td>
|
||||
</tr>
|
||||
';
|
||||
}
|
||||
} else {
|
||||
$output = '
|
||||
<tr>
|
||||
<td align="center" colspann="5">No Data Found</td>
|
||||
</tr>
|
||||
';
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'table_data' => $output,
|
||||
'total_data' => $total_data,
|
||||
);
|
||||
|
||||
echo json_encode($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
resources/views/ajax/live_search.blade.php
Normal file
40
resources/views/ajax/live_search.blade.php
Normal file
@@ -0,0 +1,40 @@
|
||||
@extends('layouts.b4')
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<input type="text" name="search" id="search" class="form-control" placeholder="search Users" />
|
||||
</div>
|
||||
<div class="table-responsive">
|
||||
<h3 align="center"> Total Data : <span id="total_records"></span></h3>
|
||||
</div>
|
||||
<table class="table table-striped table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>User</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
|
||||
fetch_user_data();
|
||||
|
||||
function fetch_user_data(query = '') {
|
||||
$.ajax({
|
||||
url:"{{ route('live_search.action')}}",
|
||||
method:'GET',
|
||||
data:{query:query},
|
||||
dataType:'json',
|
||||
success:function(data) {
|
||||
$('tbody').html(data.table_data);
|
||||
$('#total_records').text(data.total_data);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
@endsection
|
||||
@@ -25,6 +25,12 @@ Route::group(['middleware' => ['auth']], function(){
|
||||
*/
|
||||
Route::get('/dashboard', 'Dashboard\DashboardController@index');
|
||||
|
||||
/**
|
||||
* AJAX Test pages
|
||||
*/
|
||||
Route::get('/ajax', 'LiveSearch@index');
|
||||
Route::get('/ajax/action', 'LiveSearch@action')->name('live_search.action');
|
||||
|
||||
/**
|
||||
* Moon Controller display pages
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user