Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder array on javascript function
    text
    copied!<p>Hello i have this smarty code that generate a list of state in determinate country ordered by states ISO CODE</p> <pre><code>countries = new Array(); {foreach from=$countries item='country'} {if isset($country.states)} countries[{$country.id_country|intval}] = new Array(); {foreach from=$country.states item='state' name='states'} countries[{$country.id_country|intval}]['{$state.id_state|intval}'] = '{$state.name|escape:'htmlall':'UTF-8'}'; {/foreach} {/if} {/foreach} </code></pre> <p>Te restult is this and is ok for me because is ordered by ISO CODE</p> <pre><code>countries = new Array(); countries[10] = new Array(); countries[10]['53'] = 'AG'; countries[10]['54'] = 'AL'; countries[10]['55'] = 'AN'; countries[10]['56'] = 'AO'; countries[10]['58'] = 'AP'; countries[10]['93'] = 'AQ'; countries[10]['57'] = 'AR'; countries[10]['59'] = 'AT'; countries[10]['60'] = 'AV'; countries[10]['61'] = 'BA'; countries[10]['64'] = 'BG'; countries[10]['65'] = 'BI'; countries[10]['62'] = 'BL'; countries[10]['63'] = 'BN'; countries[10]['66'] = 'BO'; countries[10]['69'] = 'BR'; countries[10]['68'] = 'BS'; countries[10]['162'] = 'BT'; countries[10]['67'] = 'BZ'; countries[10]['70'] = 'CA'; countries[10]['72'] = 'CB'; countries[10]['73'] = 'CE'; countries[10]['76'] = 'CH'; countries[10]['156'] = 'CI'; countries[10]['71'] = 'CL'; countries[10]['81'] = 'CN'; countries[10]['77'] = 'CO'; countries[10]['79'] = 'CR'; countries[10]['78'] = 'CS'; countries[10]['74'] = 'CT'; countries[10]['75'] = 'CZ'; countries[10]['82'] = 'EN'; </code></pre> <p>In the same page i have this javascript that populate a select html tag but order the option tag by ID not by ISO CODE and i dont know why</p> <pre><code>$(document).ready(function(){ $('select#id_country').change(function(){ updateState(); }); updateState(); }); function updateState() { $('select#id_state option:not(:first-child)').remove(); var states = countries[$('select#id_country').val()]; if( typeof(states) != 'undefined' ) { for (indexState in states) { //ie bug fix if (indexState != 'indexOf') $('select#id_state').append('&lt;option value="'+indexState+'"'+ (idSelectedCountry == indexState ? ' selected="selected' : '') + '"&gt;'+states[indexState]+'&lt;/option&gt;'); } $('p.id_state:hidden').slideDown('slow'); } else $('p.id_state').slideUp('fast'); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload