anguswild
anguswild

Reputation: 323

create select html with javascript and ajax options

I have this form simple form

<form class="form-horizontal" method="post" action="pantalla1.php">
              <div class="box-body">
              <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <table id="vender" class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>Nro. Slide</th>
                        <th>Fondo</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><input type="text" name="slidenro[]" id="slidenro_1" value="1" class="form-control" readonly></td>
                        <td><select name="cbofon[]" id="cbofon_1" class="form-control select2">
                    </select></td>

                    </tr>
                </tbody>
            </table>
        </div>
    </div>
        <div class="col-xs-12 col-sm-3 col-md-3 col-lg-3">

</div>
            <button class="btn btn-danger delete" type="button">- Borrar</button>
            <button class="btn btn-success addmore" type="button">+ Agregar Slide</button>


              </div><!-- /.box-body -->
              <div class="box-footer">
                <button type="submit" id="btnact" name="btnact" class="btn btn-info pull-right">Actualizar</button>
              </div><!-- /.box-footer -->
            </form>

This is the function to populate the first select and it works fine

    $(function populate(){

$.ajax({                                      
  url: 'getfotos.php',                      
  data: "",                      
  dataType: 'json',                     
  success: function(data){          

   var sel = document.getElementById('cbofon_1');

    for(var i = 0; i < data.length; i++) {
        var opt = document.createElement('option');
        opt.innerHTML = data[i][1];
        opt.value = data[i][1];
        sel.appendChild(opt);

            }

  },
complete: function() {

} 

});
}); 

This is the function to add another row to the table

$(".addmore").on('click',function(){
var i=$('table tr').length;
html = '<tr>';
html += '<td><input type="text" name="slidenro[]" id="slidenro_'+i+'" value="'+i+'" class="form-control" readonly></td>';
html += '<td><select name="cbofon[]" id="cbofon_'+i+'" class="form-control select2"></select></td>';
html += '</tr>';
$('table').append(html);
i++;
});

But i have a problem, how can i populate every new select that i create with the .addmore button with the ajax content from the first select

I have tried everything i know but i am very noob with javascript and ajax

Upvotes: 0

Views: 372

Answers (3)

Cheezy Code
Cheezy Code

Reputation: 1715

Just add the previous select's innerhtml into the new select's

Modified your addmore click for it

$(".addmore").on('click',function(){
var i=$('table tr').length;
html = '<tr>';
html += '<td><input type="text" name="slidenro[]" id="slidenro_'+i+'" value="'+i+'" class="form-control" readonly></td>';
html += '<td><select name="cbofon[]" id="cbofon_'+i+'" class="form-control select2">' + $("#cbofon_1").html() + '</select></td>';
html += '</tr>';
$('table').append(html);
i++;
});

Maybe this helps.

Upvotes: 1

To do it with performance, you can first get the data once and store the data in a variable:

var fotos = [];

$.ajax({
    url: 'getfotos.php',
    data: "",
    dataType: 'json',
    success: function(data) {
        fotos = data;
        populateFotosSelect(1);
    }
});

Then create a function to populate your select:

function populateFotosSelect(index) {
    var sel = document.getElementById('cbofon_' + index);
    var opt;

    for (var i = 0; i < fotos.length; i++) {
        opt = document.createElement('option');
        opt.innerHTML = data[i][1];
        opt.value = data[i][1];
        sel.appendChild(opt);
    }
}

And create the listener for click event:

$(".addmore").on('click', function() {
    var i = $('table tr').length;
    html = '<tr>';
    html += '<td><input type="text" name="slidenro[]" id="slidenro_' + i + '" value="' + i + '" class="form-control" readonly></td>';
    html += '<td><select name="cbofon[]" id="cbofon_' + i + '" class="form-control select2"></select></td>';
    html += '</tr>';
    $('table').append(html);
    populateFotosSelect(i);
    i++;
});

All code together:

(function() {
    'use strict';
    var fotos = [];

    $.ajax({
        url: 'getfotos.php',
        data: "",
        dataType: 'json',
        success: function(data) {
            fotos = data;
            populateFotosSelect(1);
        }
    });

    $(".addmore").on('click', function() {
        var i = $('table tr').length;
        html = '<tr>';
        html += '<td><input type="text" name="slidenro[]" id="slidenro_' + i + '" value="' + i + '" class="form-control" readonly></td>';
        html += '<td><select name="cbofon[]" id="cbofon_' + i + '" class="form-control select2"></select></td>';
        html += '</tr>';
        $('table').append(html);
        populateFotosSelect(i);
        i++;
    });

    function populateFotosSelect(index) {
        var sel = document.getElementById('cbofon_' + index);
        var opt;

        for (var i = 0; i < fotos.length; i++) {
            opt = document.createElement('option');
            opt.innerHTML = data[i][1];
            opt.value = data[i][1];
            sel.appendChild(opt);
        }
    }

}());

Upvotes: 0

Blazemonger
Blazemonger

Reputation: 92893

Modify your populate() function to accept an element ID as an argument:

function populate(id) {
   id = id || 'cbofon_1'; // default value, if no id is passed in
   $.ajax({                                      
      url: 'getfotos.php',                      
      data: "",                      
      dataType: 'json',                     
      success: function(data){          
         var sel = document.getElementById(id);
   // etc.

Then call that function with the argument inside your click hander:

$(".addmore").on('click',function(){
   var id = 'cbofon_'+i;
   // ... 
   html += '<td><select name="cbofon[]" id="'+id+'"'
   // ...
   populate(id);

Upvotes: 1

Related Questions