Laravel Datatables自定义选择搜索

时间:2018-10-15 12:35:56

标签: laravel datatables yajra-datatable

我有一个带下拉菜单的选项,供您选择是否要查看在给定日期到期的圆顶。 Select for expiring in given date

但是一旦我选择了日期,我就会得到正确结果的纯json代码。 这是我在表格上方的表格

<form method="post" action="{{ action('DomenController@tabela') }}" id="klijent_rok" onchange="document.getElementById('klijent_rok').submit()">
    @csrf
    <div class="form-group row">
        <label for="istice_za" class="ml-md-auto col-form-label text-md-left" style="padding-right: 10px;">Ističu u roku od:</label>
            <select  name="istice_za" id="istice_za" class="form-control" style="width: 140px" size="1">
                <option value="99" @if($istice_za == '99') selected='selected' @endif>-neodređeno-</option>
                <option value="7" @if($istice_za == '7') selected='selected' @endif>za 7 dana</option>
                <option value="30" @if($istice_za == '30') selected='selected' @endif>za 30 dana</option>
                <option value="60" @if($istice_za == '60') selected='selected' @endif>za 60 dana</option>
                <option value="istekli" @if($istice_za == 'istekli') selected='selected' @endif>istekli</option>
            </select> 
        <a href="/lista-domena" class="btn btn-info" style="height: 37px; margin-left: 10px; margin-right: 15px">Poništite filtere</a>
    </div>
</form>

这是服务器端数据表的防雷器

if(!empty($request->istice_za) && $request->istice_za != 99) {
    $date = Carbon::now();
    $new_date = Carbon::now();
    if($request->istice_za != 'istekli') {
        if($request->istice_za == 7) {
            $istice_za = 7;
            $new_date->addDays(7);
        }
        if($request->istice_za == 30) {
            $istice_za = 30;
            $new_date->addDays(30);
        }
        if($request->istice_za == 60) {
            $istice_za = 60;
            $new_date->addDays(60);
        }
        $domen = Domen::where('datum_end', '<', $new_date)
            ->Where('datum_end', '>', $date)
            ->with('klijent');
        }
        else {
            $domen = Domen::where('datum_end', '<', $date)->with('klijent');
            $istice_za = 'istekli';
        }
    }
    else {
        $domen = Domen::with('klijent');
        $istice_za = 99;
    }
    return datatables()->of($domen)->make(true);

这是我的路线

Route::get('/lista-domena/tabela', 'DomenController@tabela');
Route::post('/lista-domena/tabela', 'DomenController@tabela');

我在做什么错了并且没有在表中获取这些数据?

3 个答案:

答案 0 :(得分:0)

首先,您的代码未运行查询,您需要添加->get()first()才能获取数据。

$domen = Domen::where('datum_end', '<', $date)->with('klijent')->get();

或者,

$domen = Domen::with('klijent')->get();

答案 1 :(得分:0)

我设法解决了。问题是我没有将数据从select发送到控制器。我将表单编辑为:

<form method="post" action="{{ action('DomenController@tabela') }}" id="klijent_rok">

然后将其添加到脚本中:

 $('#klijent_rok').on('change', function(e) {
    table.draw();
});

并将Ajax更改为此:

ajax: {
    url: '/lista-domena/tabela',
    data: function ( d ) {
        d.istice_za = $('#istice_za').val();
    },
}...

答案 2 :(得分:0)

HTML:

<table id="affected-requirement-table" class="table table-hover table-sm table-bordered">
                                    <!--Table head-->
                                    <thead>
                                    <tr>
                                        <th>Requirements</th>
                                        <th>Effect Type</th>
                                    </tr>
                                    </thead>
                                    <!--Table head-->
                                    <!--Table body-->
                                    <tbody>
                                    <!-- Ajax Load -->
                                    </tbody>
                                    <!--Table body-->
                                </table>

jQuery

    $(document).ready(function () {




        var affectedRequirementsTable = $('#affected-requirement-table').DataTable({
            "scrollY":        "200px",
            "scrollCollapse": true,
            "paging":         false,
            processing: true,
            serverSide: true,
            bFilter: false,
            "bInfo": false,
            "bPaginate": true,
            "ordering": false,
            "bLengthChange": false,

            ajax: {
                url: '{!! \Illuminate\Support\Facades\URL::to("cr/requirements-datatables") !!}'
            },
            columns: [

                {data: 'number'},
                {data: 'effect-type'}
            ],
            // "fnCreatedRow" : function (row, data, dataIndex) {
            //     var div = document.createElement("div");
            //     $(div).html('<select  name= "changeRequest[]" class="form-control effect-type-select" id="mySelect"> <option  selected="selected" value="None">None</option> <option  value="Addition">Addition</option> <option  value="Deletion">Deletion</option> </select>');
            //     $(row).children("td:last-child").append(div);
            // }


        });

        // draw the table
        affectedRequirementsTable.draw();


        $("#affected-requirement-table").on('change',".effect-type-select",function (e) {
            var optionSelected = $("option:selected", this);
            var valueSelected = this.value;
            console.log(valueSelected);

        });





    })

路线:

      Route::get('cr/requirements-datatables', "ChangeRequestController@requirementsAndEffectTypesDatatables");

控制器:

       public function requirementsAndEffectTypesDatatables(ChangeRequests $request)
{

    $changeRequest = DB::table('requirement')->select(['number','id']);

    return DataTables::of($changeRequest)->addColumn('effect-type', function ($changeRequest) {


        return '
                <select  name= "changeRequest[]" class="form-control effect-type-select" id="mySelect">
                   <option  selected="selected" value="None">None</option>
                    <option  value="Addition">Addition</option>

                     <option  value="Deletion">Deletion</option>
                </select>';
    })->rawColumns(['effect-type'])->make(true);


     }