【问题标题】:How to load the default selected values from ajax for bootstrap-select如何从 ajax 加载默认选定值以进行引导选择
【发布时间】:2022-11-10 04:09:41
【问题描述】:

我正在使用https://github.com/truckingsim/Ajax-Bootstrap-Select boostrap-select 插件来选择多选下拉菜单。

我可以做什么:

  • 从 ajax 请求中搜索 drop 淹没数据
  • 选择多个值

什么不见​​了:

  • 默认页面加载不设置数据库值。 (我有一个 id 列表,但我不知道如何为相应的 id 加载标签) 我可以通过下拉列表加载下拉数据搜索。 但我无法在编辑页面加载期间从 ajax 调用中设置/加载默认选定值
<link href="css/bootstrap-select.css" rel="stylesheet" type="text/css" />
<link href="css/bootstrap-theme.min.css" rel="stylesheet" type="text/css" />

<script src="js/bootstrap-select.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<script src="js/ajax-bootstrap-select.js" type="text/javascript"></script>

<div class="form-group row required">
<label for="u-list" class="col-md-6 col-form-label">Label</label>
<div class="col-md-6">
    <select class="form-control selectpicker with-ajax" id="u-list" name="u-list" multiple data-live-search="true">
    </select>           
</div>
</div>

    var list = {
            ajax          : {
                url     : 'ajaxhandle?action=getApps',
                type    : 'GET',
                dataType: 'json',
                // Use "{{{q}}}" as a placeholder and Ajax Bootstrap Select will
                // automatically replace it with the value of the search query.
                data    : {
                    term: '{{{q}}}'
                }
            },
            locale        : {
                emptyTitle: 'Select and Begin Typing'
            },
            log           : 3,
            preprocessData: function (data) {
                var i, l = data.length, array = [];
                if (l) {
                        console.log(data[i]);
                    for (i = 0; i < l; i++) {
                        array.push($.extend(true, data[i], {
                            text : data[i].label,
                            value: data[i].id,
                            data : {
                                subtext: data[i].label
                            }
                        }));
                    }
                }
                // You must always return a valid array when processing data. The
                // data argument passed is a clone and cannot be modified directly.
                return array;
            }
        };

        $('.selectpicker').selectpicker().filter('.with-ajax').ajaxSelectPicker(list);
        $('select').trigger('change');

【问题讨论】:

    标签: jquery ajax twitter-bootstrap bootstrap-select


    【解决方案1】:

    文档并没有让它看起来很明显,但我相信要显示默认选项所需要做的就是将它们添加到选择元素中。现在您的代码显示一个空的选择元素。

    <select class="form-control selectpicker with-ajax" id="u-list" name="u-list" multiple data-live-search="true">
        <option value="1">First</option>
        <option value="2">Second</option>
        <option value="3" selected>Third</option>
    </select>
    

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多