Double Dropdown Sorting

cymak 20 Aug, 2015
Hi Again,

I have the dynamic drop down working from your example "How do I build a Double Drop-down in CFv5 " but the second drop down is not sorted(I have set the order by on the db read). Can this be fixed? Looks like the json_encode is meeing up the sorting.
GreyHead 21 Aug, 2015
Hi cymak,

The json_encode won't change the order of the array. More likely that the order by is working correctly. Have you checked the output to see if it is sorted?

Bob
cymak 21 Aug, 2015
Hi, Yeah its definately not sorting, here is the read setting

Model ID -> GetSchData

Fields -> ID,School_Name

Order -> School_Name ASC

and the code below

<?php
$options = array();
$options[''] = 'Select School';
foreach ( $form->data['GetSchData'] as $d ) {
    $options[$d['ID']] =  $d['School_Name'];
 };
$options['ZZZ'] = 'Not Listed, Add New School to List';
echo json_encode($options);
?>


the results are attached. Any ideas? Thanks
GreyHead 21 Aug, 2015
Hi cymak,

I don't understand what is happening here, it's possible to debug this but not easy to explain. By all means PM me the site URL, the form name, and a SuperAdmin login and I'll take a quick look.

Bob
GreyHead 21 Aug, 2015
Hi cymak,

Well, the data returned from the server is OK
{"":"Select School","12":"Carolan School of Irish Dancing","16":"Downey Crawley Sch of ID","14":"Kiernan School of Irish Dance","13":"Leonard School of irish Dance","32":"Mak School of ID","15":"O’Neill School of Irish Dancing","ZZZ":"Not Listed, Add New School to List"}
and it's still OK after json_decode
Array ( [] => Select School [12] => Carolan School of Irish Dancing [16] => Downey Crawley Sch of ID [14] => Kiernan School of Irish Dance [13] => Leonard School of irish Dance [32] => Mak School of ID [15] => O’Neill School of Irish Dancing [ZZZ] => Not Listed, Add New School to List )
so I guess that the problem is somewhere after that.

I'll dig a bit further later on.

Bob
cymak 21 Aug, 2015
Yeah its strange, The order is exactly the same as the Indexed primary ID field in the table. i.e if I select the last dropdown option and added a new school, it will appear at the bottom of the list and the 2 manually added array items(Select School and Not Listed, Add New School to List) always appears at the below that ??
cymak 24 Aug, 2015
In your example It looks ok after the json because there is no key in the array, if there was one it sorts it by the key instead, not sure why, anyway to get around that? how would I code it not to have to use json? Thanks
GreyHead 25 Aug, 2015
Hi cymak,

I dug some more into this and found that the problem is with the parseJSON() method that is used to un-pack the JSON array and convert it to options. This method is what is changing the order (actually it re-sorts in the order of the values). I have experimented with a structuring the JSON array differently, I got the school order right but the options and values were broken - the text came out like 19,Schoolname :-(

I think that the only answer is to replace some of that JavaScript but I haven't yet had time to look at it again.

Bob
cymak 25 Aug, 2015
Hi,

thanks, if you could have a look at it when you can, it doesn't make sense if a drop down can't be sorted alphabetically when it's a long list😟 will have to get you some beer if you can get it to work, thanks!!
cymak 28 Aug, 2015
Hi,

Any luck on getting the list sorted alphabetically? Thanks!
GreyHead 29 Aug, 2015
Hi cymak,

Still on the To-Do list . . .

Bob
cymak 29 Aug, 2015
Thanks !
cymak 09 Sep, 2015
Hi,

Can you give me a rough idea on time, suppose to launch this weekend, Thanks
GreyHead 10 Sep, 2015
HI cymak,

Sorry this has taken a while, I think I now have a method that works but it involves hacking the core CF code :-(

Open the file /administrator/components/com_chronoforms5/chronoforms/actions/html/html.php and look around line 665 for this block of code
					"success" : function(res){
						$.each($.parseJSON(res), function(id, val){
							'.$target_field.'.append(\'<option value="\'+id+\'">\'+value+\'</option>\');
						});
					},
Change the second and third lines so they are like this
					"success" : function(res){
						$.each($.parseJSON(res), function(id, option){
							'.$target_field.'.append(\'<option value="\'+option.key+\'">\'+option.text+\'</option>\');
						});
					},

NOTE!!! changing this will affect all dynamic drop-downs on the site and hence the corresponding Custom Code will need to be updated.

Edit the Custom Code in your Ajax event so that it is like this
<?php
$options = array();
$options[] = array('key' => '', 'text' => 'Select');
foreach ( $form->data['GetSchData'] as $d ) {
  $options[] = array('key' => $d['ID'], 'text' => $d['School_Name']);
 };
$options[] = array('key' => 'ZZZ', 'text' => 'Not Listed, Add New School to List');
echo json_encode($options);
?>
Instead of a simple array of key+text pairs this creates an array of arrays each of which has named key and text entries. The parseJSON() method in the JavaScript will then keep the original sort order,

Please check the various parameter names carefully, I used different names in my testing and may not have corrected them all here.

Bob
cymak 11 Sep, 2015
Hi,

That worked perfectly!! Six Pack on the way to you, Thanks!
This topic is locked and no more replies can be posted.