0

I am showing ajax response in select options as below code. I want to sort the select options in alphabetical order of values.

Please provide a solution to sort below data on values basis. My data is like {123:"green",124:"blue"}

function ajax_post(params, append_to, appent_type) {
 $.ajax({
 type: "GET",
 url: "<?= $this->getBaseUrl()?>" + "customcontroller/index/index/" + params,
 dataType: "json",
 success: function (data) {
 if (append_to != 0) {
 $(append_to).html('');
 $(append_to).append('<option value="0">Select ' + appent_type + '</option>');
 $.each(data, function (key, value) {
 $(append_to).append('<option value="' + key + '">' + value + '</option>');
 });
 }
 }
 })
 }

Please provide a solution. I have tried to sort response in controller but when ajax loads it, it shows unsorted data.

asked Nov 18, 2019 at 9:55
6
  • share your options array. Commented Nov 18, 2019 at 9:57
  • @RakeshVarma my options array is like {551:"green", 552:"blue", 553:"red"} Commented Nov 18, 2019 at 10:08
  • try asort() in your controller. Commented Nov 18, 2019 at 10:20
  • In my controller I have below code in function $finalModel= asort(array_combine(array_values($modelValues),array_values($modelNames))); $result = json_encode($finalModel); return $result; Commented Nov 18, 2019 at 10:34
  • But it's not working. Commented Nov 18, 2019 at 10:35

2 Answers 2

0

You can try something like below.

function ajax_post(params, append_to, appent_type) {
 $.ajax({
 type: "GET",
 url: "<?= $this->getBaseUrl()?>" + "customcontroller/index/index/" + params,
 dataType: "json",
 success: function (data) {
 if (append_to != 0) {
 $(append_to).html('');
 $(append_to).append('<option value="0">Select ' + appent_type + '</option>');
 $.each(sortReponse(data), function (key, value) {
 $(append_to).append('<option value="' + value.key + '">' + value.value + '</option>');
 });
 }
 }
 })
}
function sortReponse(data)
{
 var sorted = [];
 $(data).each(function(k, v) {
 for(var key in v) {
 sorted.push({key: key, value: v[key]})
 }
 });
 return sorted.sort(function(a, b){
 if (a.value < b.value) return -1;
 if (a.value > b.value) return 1;
 return 0;
 });
}

Try if that works.

answered Nov 19, 2019 at 10:03
0

Try below code.

$finalModel=array_combine(array_values($modelValues),array_values($modelNames)); 
asort($finalModel); 
$result = json_encode($finalModel); 
return $result;
answered Nov 18, 2019 at 10:41
9
  • This gives error - syntax error, unexpected 'asort' (T_STRING) in line asort($finalModel); Commented Nov 18, 2019 at 10:49
  • check your code if you forget ; in first line. Commented Nov 18, 2019 at 10:53
  • I have fixed there was some extra content but it runs as before without sorting anything. Commented Nov 18, 2019 at 10:56
  • can you post your controller code and some value of $modelValues and $modelNames Commented Nov 18, 2019 at 11:01
  • asort was not working, so I used sort. It did the work . Thanks for help. Commented Nov 18, 2019 at 11:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.