Carlos Sanchez Carlos Sanchez - 3 months ago 20
jQuery Question

Hide records from datatables based on database value 0/1

I'm trying to hide the records fetched from my database based on their value if its 0 or 1, its displaying all records i just want to display the ones with value 1, I'm using Laravel 5.2 with Yajra/Datatables.

this is my controller:

public function get() {
if(!access()->allow('customers.read')) {
return redirect()->route('admin.dashboard')->withFlashDanger(trans('customers.general.messages.denied'));
} else {
$customers = Customer::select(array(
'id', 'code', 'name', 'rnc', 'ced', 'phone', 'email', 'active'
));

return Datatables::of($customers->get('status'), $customers->get('trashed'))
->addColumn('actions', function($customers) {
return $customers->action_buttons;

})->addColumn('check', '<input type="checkbox" name="selected_users[]" value="{{ $id }}">')
->make(true);
}
}


This is my view:

@extends('backend.layouts.master')

@section('title', app_name() . ' | ' . trans('customers.general.customers_title'))

@section('after-styles-end')
{{ Html::style("css/backend/plugin/datatables/dataTables.bootstrap.min.css") }}
@stop

@section('page-header')
<h1>
{{ trans('customers.general.customers_title') }} <small>- {{ trans('customers.general.general') }}</small>
</h1>
@endsection

@section('content')
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title">{{ trans('customers.general.customers_list') }}</h3>
<div class="box-tools pull-right">
<a href="{{ route('admin.customers.create') }}" class="btn btn-sm btn-primary">{{ trans('customers.general.buttons.new_customer') }}</a>
</div>
</div><!-- /.box-header -->
<!-- Begins Customers Page -->
<div class="box-body">
<div class="table-responsive">
<table id="customers-table" class="table table-condensed table-hover">
<thead>
<tr>
<th><input type="checkbox" name="select_all" value="1" id="example-select-all"></th>
<th class="">{{ trans('customers.general.table.code') }}</th>
<th class="">{{ trans('customers.general.table.business_name') }}</th>
<th class="">{{ trans('customers.general.table.document') }}</th>
<th class="">{{ trans('customers.general.table.phone') }}</th>
<th class="">{{ trans('customers.general.buttons.email') }}</th>
<th>{{ trans('labels.general.actions') }}</th>
</tr>
</thead>
</table>
</div><!--table-responsive-->
</div><!-- /.box-body -->
</div><!--box-->
@endsection

@section('after-scripts-end')
{{ Html::script("js/backend/plugin/datatables/jquery.dataTables.min.js") }}
{{ Html::script("js/backend/plugin/datatables/dataTables.bootstrap.min.js") }}
<script>
$(document).ready(function() {
var table = $('#customers-table').DataTable({
processing: true,
serverSide: true,
ajax: {
url: '{{ route("admin.customers.get") }}',
type: 'get',
data: {active: 1}
},
columns: [
{data: 'check', name: 'check', orderable: false, searchable: false},
{data: 'code', name: 'code'},
{data: 'name', name: 'name'},
{data: 'rnc', name: 'rnc'},
{data: 'phone', name: 'phone'},
{data: 'email', name: 'email'},
{data: 'actions', name: 'actions', orderable: false, searchable: false}
],
'columnDefs': [{
'targets': 0,
'searchable':false,
'orderable':false,
'className': 'dt-body-center',
'render': function (data, type, full, meta){
return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
}
}],
"iDisplayLength": 10,
"aLengthMenu": [
[10, 20, 30, 60, -1],
[10, 20, 30, 60, "{{ trans('strings.backend.general.table.all') }}"]
],
language: {
"sProcessing": "{{ trans('strings.backend.general.table.processing') }}",
"sSearchPlaceholder": "{{ trans('strings.backend.general.search_placeholder') }}",
"search": "",
"lengthMenu": "{{ trans('strings.backend.general.table.show') }} _MENU_ {{ trans('strings.backend.general.table.records_by_page') }}",
"zeroRecords": "{{ trans('strings.backend.general.table.no_results') }}",
"info": "{{ trans('strings.backend.general.table.showing_page') }} _PAGE_ {{ trans('strings.backend.general.table.of') }} _PAGES_",
"infoEmpty": "{{ trans('strings.backend.general.table.empty') }}",
"infoFiltered": "({{ trans('strings.backend.general.table.filtered_of') }} _MAX_ {{ trans('strings.backend.general.table.total_records') }})",
"sInfo": "{{ trans('strings.backend.general.table.showing_from') }} _START_ {{ trans('strings.backend.general.table.to') }} _END_ {{ trans('strings.backend.general.table.of') }} _TOTAL_ {{ trans('strings.backend.general.table.records') }}",
"paginate": {
"previous": "{{ trans('strings.backend.general.table.previous') }}",
"next": "{{ trans('strings.backend.general.table.next') }}"
}
},
order: [[0, "asc"]],
searchDelay: 500
});
// Handle click on "Select all" control
$('#example-select-all').on('click', function(){
// Get all rows with search applied
var rows = table.rows({ 'search': 'applied' }).nodes();
// Check/uncheck checkboxes for all rows in the table
$('input[type="checkbox"]', rows).prop('checked', this.checked);
});

// Handle click on checkbox to set state of "Select all" control
$('#example tbody').on('change', 'input[type="checkbox"]', function(){
// If checkbox is not checked
if(!this.checked){
var el = $('#example-select-all').get(0);
// If "Select all" control is checked and has 'indeterminate' property
if(el && el.checked && ('indeterminate' in el)){
// Set visual state of "Select all" control
// as 'indeterminate'
el.indeterminate = true;
}
}
});

// Handle form submission event
$('#frm-example').on('submit', function(e){
var form = this;

// Iterate over all checkboxes in the table
table.$('input[type="checkbox"]').each(function(){
// If checkbox doesn't exist in DOM
if(!$.contains(document, this)){
// If checkbox is checked
if(this.checked){
// Create a hidden element
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', this.name)
.val(this.value)
);
}
}
});
});
});
</script>
@stop


in my view im retreiving all the results but its also showing the ones that i have disabled with value 1, i just want to show the ones with value 0 in the list, any help would be really appreciated.

thanks all

Answer

You can use scopes or global scopes in your model, so those records are not retrieved at all. In your model:

public function apply(Builder $builder, Model $model)
    {
        return $builder->where('parameter', 1);
    }

This will return only the records where the parameter = 1.

Comments