John Davidson

javascript - Sorted response becomes unsorted om front end jquery laravel

0 comments
Message:


I am sending response from laravel controller after sorting. an response is sorted.


 public function search(Request $request)
{


$providers = Provider::get()->map(function($item) use($request){
$item->setAttribute('distance',$this->addDistanceToProvider($item,$request)) ;

return $item;
})->sortBy('distance');

return $providers;

}


response


{"1":{"id":2,"owner_name":"hjjk","owner_email":"[email protected]","owner_phone":"4567898765432","name":"hgjjj","email":"[email protected]","phone":"","password":"$2y$10$9KmwSmKcxQNHxN6\/KmViQOVgGOIbsTxnPDd.prkTpK9BfVMJ0CXpm","rfc":"12344","tax":"67","location":"Nezahualc\u00f3yotl, State of Mexico, Mexico","longitude":"-98.9896643","latitude":"19.3994934","comission":76,"status":1,"distance":314.703},"2":{"id":3,"owner_name":"abc","owner_email":"[email protected]","owner_phone":"06767672626","name":"Bilal Arshad","email":"[email protected]","phone":"","password":"$2y$10$mLRpt7O2o0wdg8AecgpWA.4xfN9hV4VaG.JRSZyr\/hUoKPjkCkdvm","rfc":"asd","tax":"234","location":"Cuernavaca, Morelos, Mexico","longitude":"-99.22156590000002","latitude":"18.9242095","comission":113,"status":1,"distance":369.312},"0":{"id":1,"owner_name":"bilal","owner_email":"[email protected]","owner_phone":"0213123132131","name":"Bilal Arshad","email":"[email protected]","phone":"","password":"$2y$10$BUgsN4Qknk2M\/LNQwQoOrOOad.pu9dFQUiylFSSTgaRrQwDIbDhqG","rfc":"12344","tax":"dasdasd","location":"Nuevo Laredo, Tamaulipas, Mexico","longitude":"-99.68859527909136","latitude":"27.784235863652583","comission":877,"status":1,"distance":1034.684}}

But when i see preview in brwoser it is not sorted and Jquery render data in unsorted form


Jquery function


    $.each(data, function(index, provider) {

embedData(index,provider)

});

function embedData(index,provider){
$('#provider_area').append(` <div class="kt-widget5__item" ><div class="kt-widget5__content">

<div class="kt-widget5__section">
<a href="#" class="kt-widget5__title">
${provider.name}
</a>
<p class="kt-widget5__desc">
${provider.location}
</p>
<div class="kt-widget5__info">
<span>Distance:</span>
<span class="kt-font-info" >${provider.distance}</span>

</div>
</div>
</div>
<div class="kt-widget5__content">

</div></div>`)
}



My Answer:
>sortBy('name');_ return response()->json($providers);_}_In the front end, you can use jQuery to display the sorted response. Here's an example of how you can do this:javascript$.ajax({ url: '/search', type: 'GET', success: function(response) { // Display the sorted response response.forEach(function(provider) { $('#results').append('
  • ' + provider.name + '
  • '); }); }});
    In this example, the response from the Laravel controller is fetched using an AJAX request and then displayed in a list on the front end. The response is already sorted when it is sent from the controller, so it will remain sorted when displayed on the front end.

    Rate this post

    4 of 5 based on 9850 votes

    Comments




    © 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
    Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us