如何使用Bootstrap Select重建下拉菜单?

时间:2018-09-12 18:43:30

标签: jquery twitter-bootstrap bootstrap-select bootstrap-selectpicker

我正在尝试在应用程序中实现bootstrap-select。我已经在我的应用程序中使用了Bootstrap-twitter。我要在其中实现此功能的系统中没有几个下拉菜单。在页面加载时,将触发bootstrap-select并从json文件填充下拉菜单。我有一个可以更新JSON文件的系统。在这种情况下,我将重新加载JSON文件并调用函数以再次加载json。问题是在这种情况下引导选择会中断。我仍然可以单击并搜索选项,但同时所有选项都显示在选择输入字段下方。这是我的代码示例:

var appData = {
  Buildings : [
    {BLDGNAME : "Test 1", BLDGNUMBER : "2234"},
    {BLDGNAME : "Test 2", BLDGNUMBER : "2534"},
    {BLDGNAME : "Test 3", BLDGNUMBER : "2734"},
    {BLDGNAME : "Test 4", BLDGNUMBER : "2294"}
  ]
}

$(document).ready(function(){
  loadBuildings();
});

function loadBuildings(){
    populateBuildings();
    /* Just to show the way I import JSON data.
    $.getJSON("JSON/Buildings.json?"+ new Date().getTime(), function(json) {
        appData.Buildings = json;
    }).done(function() {
        populateBuildings();
    }).fail(function(jqXHR, textStatus, errorThrown) {
        console.log( "Error: " + errorThrown);
    });
    */
}

function populateBuildings(){
    var fldBldg = $(".bldg-menu");
    fldBldg.find("option:gt(0)").remove(); // Remove all but the first option in select menu.

    $.each(appData.Buildings, function (key, value) {
        fldBldg.append($("<option></option>").val(value.BLDGNUMBER).text(value.BLDGNAME));
    });
    fldBldg.selectpicker("refresh");
}

$("#add").on("click",addNew);
function addNew(){
  appData.Buildings.push({BLDGNAME : "Test 5", BLDGNUMBER : "6234"});
  loadBuildings();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.min.js"></script>
<form name="frmDemog" id="frmDemo">
  <div class="form-group">
    <label class="control-label" for="bldg"><span class="label label-primary">Building:</span></label>
    <select class="form-control bldg-menu selectpicker" name="frmDemo_bldg" id="frmDemo_bldg" data-live-search="true" required>
      <option value="">--Choose Building--</option>
    </select>
  </div>
</form>

<button type="button" name="add" id="add">Add New</button>

如果您在上面运行摘录并单击添加按钮,您将在选择菜单下方看到所有选项。我不确定如何重新加载selectpicker()以防止该行为。我尝试使用selectpicker("refresh"),但没有帮助。如果有人知道解决此问题的方法,请告诉我。谢谢。

1 个答案:

答案 0 :(得分:1)

此代码选择两个元素:由Bootstrap-Select和SELECT生成的DIV。

var fldBldg = $(".bldg-menu");

将其更改为:

var fldBldg = $("select.bldg-menu");

固定代码:

var appData = {
  Buildings : [
    {BLDGNAME : "Test 1", BLDGNUMBER : "2234"},
    {BLDGNAME : "Test 2", BLDGNUMBER : "2534"},
    {BLDGNAME : "Test 3", BLDGNUMBER : "2734"},
    {BLDGNAME : "Test 4", BLDGNUMBER : "2294"}
  ]
}

$(document).ready(function(){
  loadBuildings();
});

function loadBuildings(){
    populateBuildings();
    /* Just to show the way I import JSON data.
    $.getJSON("JSON/Buildings.json?"+ new Date().getTime(), function(json) {
        appData.Buildings = json;
    }).done(function() {
        populateBuildings();
    }).fail(function(jqXHR, textStatus, errorThrown) {
        console.log( "Error: " + errorThrown);
    });
    */
}

function populateBuildings(){
    var fldBldg = $("select.bldg-menu");
    fldBldg.find("option:gt(0)").remove(); // Remove all but the first option in select menu.

    $.each(appData.Buildings, function (key, value) {
        fldBldg.append($("<option></option>").val(value.BLDGNUMBER).text(value.BLDGNAME));
    });
    fldBldg.selectpicker("refresh");
}

$("#add").on("click",addNew);
function addNew(){
  appData.Buildings.push({BLDGNAME : "Test 5", BLDGNUMBER : "6234"});
  loadBuildings();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.min.js"></script>
<form name="frmDemog" id="frmDemo">
  <div class="form-group">
    <label class="control-label" for="bldg"><span class="label label-primary">Building:</span></label>
    <select class="form-control bldg-menu selectpicker" name="frmDemo_bldg" id="frmDemo_bldg" data-live-search="true" required>
      <option value="">--Choose Building--</option>
    </select>
  </div>
</form>

<button type="button" name="add" id="add">Add New</button>