ajax attempt

This commit is contained in:
2019-07-13 02:50:07 -05:00
parent 772d091425
commit e6d8a7cb87
3 changed files with 95 additions and 0 deletions

View 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);
}
}
}

View 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

View File

@@ -25,6 +25,12 @@ Route::group(['middleware' => ['auth']], function(){
*/ */
Route::get('/dashboard', 'Dashboard\DashboardController@index'); 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 * Moon Controller display pages
*/ */