I have a 'sort by' select field that sorts all the data i get from a database table with search constraints. The problem i am having is that the sort by is being forgotten when i refresh the page despite trying to add it to the URL. How would i fix this so that when the page is refreshed on the URL that has been added by the push state, it would sort by the selected value that is in the url and not return to the default sort by?
Here is what i have so far
Html:
<select class="SortbyList sbl" id="SortBy">
<option value="0">Highest Rated</option>
<option value="1">Lowest Rated</option>
<option value="2">A-Z</option>
<option value="3">Z-A</option>
</select>
JS:
$('#SortBy').on('change', function(e) {
var urll = '/searchsort?page=1'+'&name='+name+'&category='+category+'&country='+country+'&city='+city+'&sortby='+this.value;
$.ajax({
type: 'get',
url: "", // This is the url you make the request
data: {SortbyList: this.value, name : name, city :city, country :country, category :category, sl:sl}, // Here you can send to the server the info you want in this case is only the value for the selected item
success: function ( data) {
history.pushState(null, null, urll);
$('.search-results-holder').html(data).load()
}
});
});
Controller:
$BattersQuery = Batsmen::where('approved', '=', 1)->leftJoin('comments', 'comments.batsmens_id', '=', 'batsmen.id')->select('batsmens.*', DB::raw('AVG(ratings) as ratings_average' ))->groupBy('batsmens.id');
switch ($request->SortbyList) {
case 0:
$batsmensQuery = $batsmensQuery->orderBy('ratings_average', 'DESC');
break;
case 1:
$batsmensQuery = $batsmensQuery->orderBy('ratings_average', 'ASC');
break;
case 2:
$batsmensQuery = $batsmensQuery->orderBy('batsmenname', 'ASC');
break;
case 3:
$batsmensQuery = $batsmensQuery->orderBy('batsmenname', 'DESC');
break;
default:
$batsmensQuery = $batsmensQuery->orderBy('ratings_average', 'DESC');
}
$batmens= $batsmensQuery->paginate(18);
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire