I'm messing around with laravel and datatables and everything has been fine... until now. I hope some of you might be able to help me :)
I got a Select dropdown menu, that search one of the columns in the datatable (on change). The filtering works fine, except the default value on pageload.
The datatable does not filter on pageload. The default value from the Select menu does not work and I cant find a solution for this and it's starting to drive me crazy.
My dropdown/select:
<select class="ml-2" id="filter_effect">
@foreach($effects as $effect)
<option value="{{ $effect->title }}"
@if($effect->id == $configurator->motor->effect_id)
selected
@endif
>{{ $effect->title }} kW</option>
@endforeach
</select>
Datatables code:
$(document).ready(function() {
var table = $('#changeMotorForm').DataTable({
columnDefs: [
{ orderable: false, targets: -1 }
]}
});
$('#filter_effect').on('change', function () {
table.columns(1).search( this.value ).draw();
});
});
Just to make it clear. The filtering of the datatable works fine, out of the box. Same with the select input filtering - works great. It's the default value that is causing problems. It does not filter/search the datatable on pageload, only when I select another <option>.
UPDATE: Working code:
<script>
$(document).ready(function() {
var table = $('#changeMotorForm').DataTable({
columnDefs: [
{ orderable: false, targets: -1 }
]
});
table.columns(1).search( $('#filter_effect').val() ).draw();
$('#filter_effect').on('change', function () {
table.columns(1).search( this.value ).draw();
} );
});
</script>
onloadevent. so, it won't filter datatable. add filtering after datatable initialization