Ricardo,

The code it's still heavy, you are appending in each loop, this is not
a good code pratice for perfomance and if you read the reference I
passed, you will see that it's not so relevant. The code extract
states, it can deal with a lot of data and the perfomance should be a
item to consider...

You should manipulate the dom as few as possible, in extreme case it's
more faster append once the whole content that adding each part
desired...

The way you show, you should work with dom fragment -
http://ejohn.org/blog/dom-documentfragments/

Regards,

Alexandre Magno
Interface Developer
http://blog.alexandremagno.net

On 27 jun, 19:26, Ricardo <ricardob...@gmail.com> wrote:
> For adding a handful of options in a select performance is almost
> irrelevant, it's more than fast enough. You can improve code
> readability and performance (a bit) using the Option constructor:
>
> $.post('/path/to/json.php', {params: 'xxx'}, function(data){
>
>    var $select = $('#mySelectBox');
>
>    $.each(data, function(){
>        $select.append( new Option(this.State.name, this.State.name) );
>    });
>
> }, 'json');
>
> On Jun 26, 2:56 pm, Alexandre Magno <alexan...@gmail.com> wrote:
>
> > Hello,
>
> > Yeah, thats right, the code works but it's too heavy
>
> > You should concatenate one string with all option and then make a
> > append. A append inside the loop would break the perfomance:
>
> > > $.post('/path/to/json.php', {params: 'go here, if you need them'},
> > > function(data){
> > >     // "data" contains that JSON object you described
>
> > >    // Also, I'm assuming your select box looks something like this:
> > >    // <select id="mySelectBox"></select>
> > >    var select = $('#mySelectBox');
> > >    var options;
> > >    if (data.length) $.each(data, function(){
> > >     options += '<option 
> > > value="'+this.State.name+'">'+this.State.name+'</option>';
> > >    })
> > >    $(options).appendTo( select );
>
> > > }, 'json');
>
> > Reference:http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-cor...
>
> > Regards,
>
> > Alexandre Magno
> > Interface Developerhttp://blog.alexandremagno.net
>
> > On Jun 26, 11:59 am, Eric Garside <gars...@gmail.com> wrote:
>
> > > Well, lets assume you're getting the JSON you described from the
> > > following ajax call:
>
> > > $.post('/path/to/json.php', {params: 'go here, if you need them'},
> > > function(data){
> > >     // "data" contains that JSON object you described
>
> > >    // Also, I'm assuming your select box looks something like this:
> > >    // <select id="mySelectBox"></select>
> > >    var select = $('#mySelectBox');
> > >    if (data.length) $.each(data, function(){
> > >        $( '<option value="' + this.State.name + '">' + this.State.name
> > > + '</option>').appendTo( select );
> > >    })
>
> > > }, 'json');
>
> > > On Jun 26, 6:46 am, liaogz82 <liaog...@gmail.com> wrote:
>
> > > > Hi all,
>
> > > > I have this JSON file that does the encoding from the states database.
> > > > I want to extract it out using jquery and populate the select drop
> > > > down box. the json file is being encoded in this way:
>
> > > > [
> > > > {"State":{"name":"AUSTRALIA CAPITAL TERRITORY"}},
> > > > {"State":{"name":"NEW SOUTH WALES"}},
> > > > {"State":{"name":"NORTHERN TERRITORY"}},
> > > > {"State":{"name":"QUEENSLAND"}},
> > > > {"State":{"name":"SOUTH AUSTRALIA"}},
> > > > {"State":{"name":"TASMANIA"}},
> > > > {"State":{"name":"VICTORIA"}},
> > > > {"State":{"name":"Western Australia"}}
> > > > ]
>
> > > > not very sure how to extract out the values. Please help me. Thanks!

Reply via email to