Laravel数据表自定义过滤日期范围

时间:2017-04-28 09:52:13

标签: php mysql laravel datatables

你好我正在研究laravel datatable yajrna包Everythings工作完美但现在我想实现一个日期范围选择器并显示这两个datei之间的数据使我的控制器中的功能和当我点击网址(即时使用) GET请求)即时获取正确的数据返回json。

这是我的控制者:

<?php

namespace App\Http\Controllers;

use App\DataTables\KinisisDataTable;
use App\Http\Requests;
use App\Kinisi;
use Auth;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Yajra\Datatables\Datatables;

class KinisisController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index(KinisisDataTable $dataTable)
{   
    return $dataTable->render('back.kinisis.index');
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('back.kinisis.create');
}

public function test(Request $request)
{
    $test = explode('-', $request->range);

    $date1 = strtotime(trim($test[0]));
    $date2 = strtotime(trim($test[1]));

    $start = date('Y-m-d',$date1);
    $end = date('Y-m-d',$date2);

    $kinisis = Kinisi::whereBetween('created_at',  array($start, $end))->with('user')->get(['user_id', 'onoma', 'til', 'eteria', 'tmima', 'thema', 'perigrafi', 'created_at']);

    return Datatables::of($kinisis)->make(true);
}

这是我的DataTable:

<?php

namespace App\DataTables;

use App\Kinisi;
use Form;
use Yajra\Datatables\Services\DataTable;

class KinisisDataTable extends DataTable
{
/**
 * Display ajax response.
 *
 * @return \Illuminate\Http\JsonResponse
 */
public function ajax()
{
    return $this->datatables
        ->eloquent($this->query())
        ->addColumn('action', 'back.kinisis.actions')
        ->editColumn('perigrafi', function ($row) {
            $str = strip_tags($row->perigrafi);
            if (strlen($str) > 55) {
                return substr($str, 0, 25) . '...';
            }
            return $str;

        })
        // ->editColumn('created_at', function ($row) {
        //     return $row->created_at->toFormattedDateString();
        // })
        ->editColumn('tmima', function ($row) {
            $tmima = [
                '1' => 'Τεχνικό',
                '2' => 'Πωλήσεις',
                '3' => 'Λογιστήριο',
                '4' => 'Διοίκηση',
                '5' => 'Νομική',
                '6' => 'Αλλό..'
            ];
            return $tmima[$row->tmima];
        })
        ->make(true);
}

/**
 * Get the query object to be processed by dataTables.
 *
 * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
 */
public function query()
{
    $kinisis = Kinisi::with('user')->orderBy('created_at', 'desc');

    return $this->applyScopes($kinisis);
}

/**
 * Optional method if you want to use html builder.
 *
 * @return \Yajra\Datatables\Html\Builder
 */
public function html()
{
    return $this->builder()
                ->columns($this->getColumns())
                ->ajax('')
                ->addAction(['width' => '80px', 'title' => 'Ενέργειες'])
                ->parameters($this->getBuilderParameters());
}

/**
 * Get columns.
 *
 * @return array 
 */
protected function getColumns()
{
    return [
        'user_id' => ['name' => 'user_id', 'data' => 'user.name', 'title' => 'Δημιουργός'],
        'onoma' => ['title' => 'Ονοματεπώνυμο'],
        'til' => ['title' => 'Τηλέφωνο'],
        'eteria' => ['title' => 'Εταιρεία'],
        'tmima' => ['title' => 'Τμήμα'],
        'thema' => ['title' => 'Θέμα'],
        'perigrafi' => ['title' => 'Περιγραφή'],
        // 'created_at' => ['title' => 'Δημιουργήθηκε'],
    ];
}

/**
 * Get filename for export.
 *
 * @return string
 */
protected function filename()
{
    return 'kinisisdatatables_' . time();
}
}

这是我的索引视图:

@extends('layouts.app')

@section('css')
   <link rel="stylesheet" type="text/css" href="{{ 
asset('css/daterangepicker.css') }}">
@include('layouts.datatables_css')
@stop

@section('content')
<section class="content-header" style="margin-bottom: 20px;">
    <h1>
       Κίνησης
<small>Διαχείριση</small>
</h1>
<ol class="breadcrumb">
<li><a href="{{ route('home') }}"><i class="fa fa-dashboard"></i> Home</a>
</li>
<li class="active">Κίνησης</li>
</ol>
</section>

<div class="container-fluid">
<a class="btn btn-primary pull-right" style="margin-top: -10px;margin-bottom: 5px" href="{!! route('kinisis.create') !!}">Καταχωρηση</a>
</div>

<div class="container-fluid">
<div class="box box-primary">
    <div class="box-body">  
        <div class="form-group">
            <label>Date range:</label>

            <div class="input-group">
              <div class="input-group-addon">
                <i class="fa fa-calendar"></i>
              </div>
              <input type="text" class="form-control pull-right" id="reservation">

                <span class="input-group-btn">
                    <button class="btn btn-default" type="button" id="range"><i class="fa fa-search"></i></button>
                </span>
            </div>
          </div>

        {!! $dataTable->table() !!}
    </div>
</div>
</div>

@stop

@section('scripts')
<script type="text/javascript" src="{{ asset('js/moment.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/daterangepicker.js') }}"></script>
@include('layouts.datatables_js')
{!! $dataTable->scripts() !!}

<script type="text/javascript">
    $(document).ready(function(){
        $('#reservation').daterangepicker();
    });
</script>
@stop

以下是来自网址

的json输出

Json output

Index view

  

所以我的问题是当我使用ajax时,正如我在yajrna包的文档中看到的那样,我得到了一个错误的数据表,告诉我This error

如果有人知道的话请帮助!!

1 个答案:

答案 0 :(得分:0)

我不知道这是否为时已晚,但我遇到了类似的问题,这就是我为解决这个问题所做的。 如果我想使用它,我必须擦除表格然后加载它:

$('#datatableid').dataTable( {

     "bDestroy": true   

});

$('#datatableid').dataTable().fnDestroy();

$('#datatableid').empty();