Search code examples
laraveldatatablesyajra-datatable

Laravel Datatables custom select search


I have a dropdown with option to select if you want to see domens that are expiring in given date. Select for expiring in given date

But once i pick date i will get pure json code with right result. This is my form above table

<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>

And this is mine controller for server side Datatable

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);

And these are my routes

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

What am I doing wrong and not getting that data in my table?


Solution

  • I managed to solve it. The problem was that i didn't send data from select to my controller. I edited my form to be like this:

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

    And then added this in script:

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

    and changed Ajax to this:

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