views:

34

answers:

2

I am generating a dropdown on a completely separate page by jquery append function. I was getting duplicate rows of data if I just use append

  if(params.totalRecords > 50){
         var i, j;
    j = 0;              
    for(i=0; i < params.totalRecords; i++){                 
    if(i%50==0){
    $('#startRecord').append( 
    $('<option></option>').val(i).html((j+1)+'-'+(j+=50)));          
    }
  }             
    $('#dropDownSpan').css('visibility', 'visible'); 
}  

so now when I was adding the values to drop down it was adding duplicate rows like this

<option value=0>1-50</option>
<option value=50>51-100</option>
<option value=0>1-50</option>

depending what option I would choose, it would just make it duplicate.

Now to avoid that I did the following

  if(params.totalRecords > 50){
    $('#startRecord').val(0).html("1-50");
    var i, j;
    j = 0;              
    for(i=0; i < params.totalRecords; i++){                 
    if(i%50==0){
    $('#startRecord').append( 
    $('<option></option>').val(i).html((j+1)+'-'+(j+=50)));          
    }
  }             
    $('#dropDownSpan').css('visibility', 'visible'); 
}    

Now the problem is that it alway restes it to 1-50 records cause of

$('#startRecord').val(0).html("1-50");

How could I show the last selected one there. thanks

+1  A: 

I'm going to suggest a very different, hopefully simpler loop approach here:

if(params.totalRecords > 50){
  for(var i=0; i < params.totalRecords; i+=50) {
    $('<option></option>', { value: i, html: (i+1)+'-'+Math.min(i+50, params.totalRecords) })
      .appendTo('#startRecord');
  }             
  $('#dropDownSpan').css('visibility', 'visible'); 
} 

You can give it a try here, this simplifies how the looping's done overall. I'm still not 100% sure how you're getting that extra appended row, are you sure your current code isn't getting called twice somehow, with a different totalRecords count?

One side note about the above, it has slightly different output, for say 121 records instead of "101-150" it'll put "101-121" for the last item which is a bit more accurate, I hope that's what you're after, the overall output looks like this:

<select id="startRecord">
  <option value="0">1-50</option>
  <option value="50">51-100</option>
  <option value="100">101-121</option>
</select>
Nick Craver
Nick when I replace my code with yours, it shows the dro down, but no option / values are there. its just an empty drop down.thanks
@user295189 - Can you post the surrounding code to your current question? You can see from the example that this approach works, but there must be something else at play in your full page, we just need to figure out what it is.
Nick Craver
hi Nick, my other code is working just fine. only problem is that its adding it twice. Like in the first time it shows perfect this<option value=0>1-50</option> <option value=50>51-100</option> <option value=100>100-150</option> but if I select again in the second run any of the option above, it duplicates it the options in drop down to this<option value=0>1-50</option> <option value=50>51-100</option> <option value=100>100-150</option> <option value=0>1-50</option> <option value=50>51-100</option> <option value=100>100-150</option> and so on for third time.
is ther any way that I can check if the option is there then dont append it?thanks
i was thinking may be there could be a check that if there are already more than one option dont run this part $('#startRecord').append( $('<option></option>').val(i).html((j+1)+'-'+(j+=50))); cause most probably its already there
A: