mercredi 24 janvier 2018

Order by select box

I am running query's on my batsmen table and would like to sort the results A-Z and Z-A as well as High to Low on top score by using a select option box without reloading the page. I am having a problem working out how to get the AJAX js working with the php and select box any ideas on how to do this, i can't find any useful tutorials. I have this code so far:

Controller(PHP):

    $batsmenQuery = Batsmen::where('approved', '=', 1);

   switch ($request->SortbyList){
        case 0:
            $batsmenQuery = $batsmenQuery->orderBy('name', 'DESC');
            break;
        case 1:
            $batsmenQuery = $batsmenQuery->orderBy('name', 'ASC');
            break;
        case 2:
            $batsmenQuery = $batsmenQuery->orderBy('hs', 'ASC');
            break;
        case 3:
            $batsmenQuery = $batsmenQuery->orderBy('hs', 'DESC');
            break;
            default:
                $batsmenQuery = $batsmenQuery->orderBy('name', 'DESC');


    }

    $batsmen= $batsmenQuery->paginate(40);

HTML:

        <div class="row">
            <div class="sort">
                <select name="SortbyList" id="SortBy">
                    <option value="0">A to Z</option>
                    <option value="1">Z to A</option>
                    <option value="2">Highest Score</option>
                    <option value="3">Lowest Score</option>

                </select>
            </div>
        </div>

javascript:

 $('#SortBy').on('change', function(e){

 });

Any ideas on how to javascript for this work to switch through statements related on whats been clicked in the select box?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire