bootstrap-select icon indicating copy to clipboard operation
bootstrap-select copied to clipboard

AJax does not work with class=“selectpicker form-control” in drop-down menus

Open mictadlo opened this issue 12 years ago • 9 comments

I found flask-jquery-ajax example where the user selects an item from the vehicle "Make" drop down menu, the vehicle "Model" drop down menu is populated by making an AJAX request for a list of models for the make selected.

I tried to replace the drop-down menus by bootstrap-select and as soon as I include class="selectpicker form-control" in the second drop-down menu it does not get any more populated after the first drop-down has been chosen.

This is the HTML template:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Flask Jquery AJAX Drop Down Menu Example</title>

    <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" >
    <link rel="stylesheet" type="text/css" href=//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.2/css/bootstrap-select.min.css>
    <link rel="stylesheet" href="{{ url_for('static', filename='web.css') }}">

    <!-- Custom styles for this template -->
    <link href="http://getbootstrap.com/examples/non-responsive/non-responsive.css" rel="stylesheet">
</head>
<body>
    <h1>Select Vehicle</h1>

    <form class="form-horizontal" action="/" method="POST" >
      <div class="input-group">
    <span class="input-group-addon">Make:</span>
    {{ form.make(id="make_select", class="selectpicker form-control") }}
      </div>

      <div class="input-group">
    <span class="input-group-addon">Model:</span>
    {{ form.model(id="model_select", class="selectpicker form-control") }}
      </div>
        <button type="submit">Submit</button>
    </form>

    {% if chosen_make %}
        <h2>You selected:</h2>
        <ul>
            <li>Make ID: {{ chosen_make }}</li>
            <li>Model ID: {{ chosen_model }}</li>
        </ul>
    {% endif %}

  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
  <script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.2/js/bootstrap-select.min.js"></script>
  <script>
    $('.selectpicker').selectpicker();
  </script> 
  <script src="/assets/vehicle.js"></script>
</body>
</html>

This is the JavaScript code responsible for populating the drop-down menus:

$("#make_select").change(function() {
    var make_id = $(this).find(":selected").val();
    var request = $.ajax({
        type: 'GET',
        url: '/models/' + make_id + '/',
    });
    request.done(function(data){
        var option_list = [["", "--- Select One ---"]].concat(data);

        $("#model_select").empty();
        for (var i = 0; i < option_list.length; i++) {
            $("#model_select").append(
                $("<option></option>").attr(
                    "value", option_list[i][0]).text(option_list[i][1])
            );
        }
    });
});

Why does class="selectpicker form-control" from bootstrap-select cause that the second drop-down menu get not anymore populated?

I also posted it on stackoverflow.

mictadlo avatar Oct 05 '14 00:10 mictadlo

You need to refresh the list of results after adding the options to the select so your JS should look like this:

$("#make_select").change(function() {
    var make_id = $(this).find(":selected").val();
    var request = $.ajax({
        type: 'GET',
        url: '/models/' + make_id + '/',
    });
    request.done(function(data){
        var option_list = [["", "--- Select One ---"]].concat(data);

        $("#model_select").empty();
        for (var i = 0; i < option_list.length; i++) {
            $("#model_select").append(
                $("<option></option>").attr(
                    "value", option_list[i][0]).text(option_list[i][1])
            );
        }
        $('#model_select').selectpicker('refresh');
    });
});

truckingsim avatar Oct 06 '14 19:10 truckingsim

Thank you.

mictadlo avatar Oct 13 '14 13:10 mictadlo

this is a good solution :)

anyway111 avatar Mar 12 '15 04:03 anyway111

$('#model_select').selectpicker('refresh'); good solution!thank you

pengqiuyuan avatar Apr 16 '15 02:04 pengqiuyuan

hi, am facing same problem. $('.js-example-data-array12').selectpicker('refresh'); is not working after ajax form submit

vidyashree09 avatar Jul 07 '17 07:07 vidyashree09

@vidyashree09 to help you would need to see the JS that shows the ajax request and the refresh and everything. Can't really help you without that.

truckingsim avatar Jul 07 '17 09:07 truckingsim

Hiya. Generating a selectpicker from a PHP call thru AJAX. Code works fine but selectpicker does not appear. I execute a refresh and picker appears and functions normally, but it loses all styling. Ideas?

ceolsine avatar Feb 14 '18 01:02 ceolsine

You need to refresh the list of results after adding the options to the select so your JS should look like this:

$("#make_select").change(function() {
    var make_id = $(this).find(":selected").val();
    var request = $.ajax({
        type: 'GET',
        url: '/models/' + make_id + '/',
    });
    request.done(function(data){
        var option_list = [["", "--- Select One ---"]].concat(data);

        $("#model_select").empty();
        for (var i = 0; i < option_list.length; i++) {
            $("#model_select").append(
                $("<option></option>").attr(
                    "value", option_list[i][0]).text(option_list[i][1])
            );
        }
        $('#model_select').selectpicker('refresh');
    });
});

thank you

AshwiniGLS28 avatar Dec 05 '19 12:12 AshwiniGLS28

Hi all, I have set the bootstrap-select value from ajax for my crud operation, the value has been set in the select but it does not show in the UI.

$.ajax({

url: "/get-item", type:"GET", data:{ "_token": "{{ csrf_token() }}", id:data.id, }, success:function(response){ $('#type').val(response.item.type_id)
}, error: function(response) { }, complete: function() {
$('.selectpicker').selectpicker('refresh');
}

});

If I use settimeout function then it works. ie setTimeout(() => {$('.selectpicker').selectpicker('refresh');}, 1000);

Can you guys please help me with this?

sarojmaharjan561 avatar Dec 31 '22 04:12 sarojmaharjan561