我试图隐藏从我的数据库中获取的记录,如果它的值为0或1,它显示所有记录我只想显示值为1的记录,我正在使用Laravel 5.2和Yajra / Datatables
这是我的控制者:
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);
}
}
这是我的观点:
@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
在我的视图中我正在检索所有结果,但它也显示了我已禁用值1的那些,我只是想在列表中显示值为0的那些,任何帮助都会非常感激。
感谢所有
答案 0 :(得分:2)
您可以在模型中使用范围或全局范围,因此根本不会检索这些记录。 在你的模型中:
public function apply(Builder $builder, Model $model)
{
return $builder->where('parameter', 1);
}
这将仅返回参数= 1的记录。