Live search

This commit is contained in:
Neerholt 2020-07-07 19:29:21 +02:00
parent 4e69768ae8
commit 9c17623518
6 changed files with 69 additions and 26 deletions

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use App\Contact; use App\Contact;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\Types\Context; use phpDocumentor\Reflection\Types\Context;
class ContactController extends Controller class ContactController extends Controller
@ -132,4 +133,33 @@ class ContactController extends Controller
$contact->delete(); $contact->delete();
return redirect()->route("contacts.index"); return redirect()->route("contacts.index");
} }
public function search(Request $request){
if($request->ajax()){
$output = "";
$users = DB::table('users')->where('name_first', 'LIKE','%'.$request->search.'%')
->orWhere('name_last','LIKE', '%'. $request->search.'%')
->orWhere('phone','LIKE', '%' . $request->search.'%')
->orWhere('mail','LIKE','%'.$request->serach. '%')->get();
if($users){
foreach ($users as $key => $user){
$output.='<tr>'.
'<td>' . $user->name_first . '</td>'.
'<td>' . $user->name_last . '</td>'.
'<td>' . $user->mail . '</td>'.
'<td>' . $user->phone .'</td>'.
'</tr>';
}
return Response($output);
}
}
}
} }

View File

@ -7,6 +7,7 @@ use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use App\User; use App\User;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Role;
@ -254,19 +255,5 @@ class UserController extends Controller
return Response::detect("users.editpass"); return Response::detect("users.editpass");
} }
public function liveSearch(Request $request){
if($request->ajax()){
$output="";
$results=DB::table('users')->where('name_first','LIKE','%'.$request->search."%")->get();
if(!$results){
echo "idk kev";
}else{
return Response($output);
}
}
}
} }

View File

@ -5070,6 +5070,12 @@
} }
} }
}, },
"jquery": {
"version": "3.5.1",
"resolved": "https://registry.npmjs.org/jquery/-/jquery-3.5.1.tgz",
"integrity": "sha512-XwIBPqcMn57FxfT+Go5pzySnm4KWkT1Tv7gjrpT1srtf8Weynl6R273VJ5GjkRb51IzMp5nbaPjJXMWeju2MKg==",
"dev": true
},
"js-tokens": { "js-tokens": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",

View File

@ -12,6 +12,7 @@
"devDependencies": { "devDependencies": {
"axios": "^0.19", "axios": "^0.19",
"cross-env": "^7.0", "cross-env": "^7.0",
"jquery": "^3.5.1",
"laravel-mix": "^5.0.1", "laravel-mix": "^5.0.1",
"lodash": "^4.17.13", "lodash": "^4.17.13",
"resolve-url-loader": "^2.3.1", "resolve-url-loader": "^2.3.1",

View File

@ -10,8 +10,16 @@
@endsection @endsection
@section("content") @section("content")
<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<div class="row align-items-center"> <div class="row align-items-center">
<a class="btn btn-inline btn-sde-blue mb-0" href="{{ route('contacts.create') }}"><img src="{{ asset('/images/icons/plus.svg') }}" alt="Create">Opret Kontakt</a> <a class="btn btn-inline btn-sde-blue mb-0" href="{{ route('contacts.create') }}"><img src="{{ asset('/images/icons/plus.svg') }}" alt="Create">Opret Kontakt</a>
<form method="post" action="{{ route("users.store") }}">
@csrf
<input type="text" class="form-controller" id="search" name="search" placeholder="Søg på navn,email,telefon"></input>
</form>
</div> </div>
<table class="tbl mt-2"> <table class="tbl mt-2">
<tr> <tr>
@ -39,4 +47,23 @@
</tr> </tr>
@endforeach @endforeach
</table> </table>
<script>
$('#search').on('keyup', function () {
//alert('Test');
$value = $(this).val();
$.ajax({
type: 'get',
url: '{{URL::to('contacts')}}',
data: {'search':$value},
success:function (data) {
$('tbody').html(data);
console.log(data);
}
});
})
</script>
@endsection @endsection

View File

@ -10,19 +10,11 @@
@endsection @endsection
@section("content") @section("content")
<div class="row align-items-center"> <div class="row align-items-center">
<a class="btn btn-inline btn-sde-blue mb-0" href="{{ route('users.create') }}"><img src="{{ asset('/images/icons/plus.svg') }}" alt="Create">Opret Bruger</a> <a class="btn btn-inline btn-sde-blue mb-0" href="{{ route('users.create') }}"><img src="{{ asset('/images/icons/plus.svg') }}" alt="Create">Opret Bruger</a>
<form method="post" action="{{ route("users.store") }}">
@csrf
<input type="text" class="form-controller" id="search" name="search"></input>
<input type="submit" class="btn btn-dark text-white" value="Søg">
</form>
</div> </div>
<table class="tbl mt-2"> <table class="tbl mt-2">
<tr> <tr>
@ -41,6 +33,7 @@
<td>{{ $user->email }}</td> <td>{{ $user->email }}</td>
<td>{{ $user->phone }}</td> <td>{{ $user->phone }}</td>
<td> <td>
@for($i = 0; $i < count($user->roles); $i++) @for($i = 0; $i < count($user->roles); $i++)
@if(count($user->roles)-1 != $i) @if(count($user->roles)-1 != $i)
{{$user->roles[$i]->name}}, {{$user->roles[$i]->name}},
@ -60,6 +53,5 @@
</tr> </tr>
@endforeach @endforeach
</table> </table>
{{ $users->links() }} {{ $users->links() }}
@endsection @endsection