Browse files

MDL-35776 user ajax: fix sort order in chrome.

It seems that Chrome orders fields of objects in order of array key.
Therefore we must stop using user.id in the PHP arrays, and instead
ensure that we use sequential numbers.

This commit fixes the user selector.

Conflicts:
	user/selector/module.js
	user/selector/search.php
  • Loading branch information...
1 parent aaed6d7 commit ea29e3141ad81256c847a4da70f554784f6640f7 @timhunt timhunt committed Oct 3, 2012
Showing with 16 additions and 11 deletions.
  1. +8 −6 user/selector/module.js
  2. +8 −5 user/selector/search.php
View
14 user/selector/module.js
@@ -221,8 +221,9 @@ M.core_user.init_user_selector = function (Y, name, hash, extrafields, lastsearc
// Output each optgroup.
var count = 0;
- for (var groupname in data.results) {
- this.output_group(groupname, data.results[groupname], selectedusers, true);
+ for (var key in data.results) {
+ var groupdata = data.results[key];
+ this.output_group(groupdata.name, groupdata.users, selectedusers, true);
count++;
}
if (!count) {
@@ -248,13 +249,14 @@ M.core_user.init_user_selector = function (Y, name, hash, extrafields, lastsearc
output_group : function(groupname, users, selectedusers, processsingle) {
var optgroup = Y.Node.create('<optgroup></optgroup>');
var count = 0;
- for (var userid in users) {
- var user = users[userid];
- var option = Y.Node.create('<option value="'+userid+'">'+user.name+'</option>');
+ for (var key in users) {
+ var user = users[key];
+ var option = Y.Node.create('<option value="'+user.id+'">'+user.name+'</option>');
if (user.disabled) {
option.set('disabled', true);
- } else if (selectedusers===true || selectedusers[userid]) {
+ } else if (selectedusers===true || selectedusers[user.id]) {
option.set('selected', true);
+ delete selectedusers[user.id];
} else {
option.set('selected', false);
}
View
13 user/selector/search.php
@@ -78,17 +78,20 @@
$userselector = new $classname($name, $options);
// Do the search and output the results.
-$users = $userselector->find_users($search);
-foreach ($users as &$group) {
- foreach ($group as $user) {
+$results = $userselector->find_users($search);
+$json = array();
+foreach ($results as $groupname => $users) {
+ $groupdata = array('name' => $groupname, 'users' => array());
+ foreach ($users as $user) {
$output = new stdClass;
$output->id = $user->id;
$output->name = $userselector->output_user($user);
if (!empty($user->disabled)) {
$output->disabled = true;
}
- $group[$user->id] = $output;
+ $groupdata['users'][] = $output;
}
+ $json[] = $groupdata;
}
-echo json_encode(array('results' => $users));
+echo json_encode(array('results' => $json));

0 comments on commit ea29e31

Please sign in to comment.