selectpicker 多选使用

本文详细介绍Bootstrap Select组件的使用方法,包括基本操作如单选、多选、模糊搜索、动态赋值等。通过具体实例,展示了如何引入必要的CSS和JS文件,生成下拉框选项,动态填充选项值,以及如何进行值的回填和获取已选项目。

附上官网api链接,http://silviomoreto.github.io/bootstrap-select/.

下拉框的使用上基本操作一般是:单选、多选、模糊搜索、动态赋值等,下面来看如何使用:

1.首先需要引入的CSS和JS

bootstrap.css
bootstrap-select.min.css
jquery-1.11.3.min.js
bootstrap.min.js
bootstrap-select.min.js

2. option在html中生成

html代码:

<select  multiple  class="form-control" name="dutylogtype[]" id="dutylogtype" data-live-search="true" value='{$dutylogconfig}' required>
	 <option value="voiceinfo">语音系统</option>
	 <option value="vedioinfo">视频系统</option>
	 <option value="businessinfo">业务网格系统</option>
	 <option value="otherinfo">其他</option>
</select>	

设置multiple时为多选,data-live-search="true"时显示模糊搜索框,不设置或等于false时不显示。

JS代码:

//配置项
var  $dutylogconfig = '{$dutylogconfig}';
 $('select[name="dutylogtype[]"]').selectpicker({
     noneSelectedText: '请选择配置选项' //默认显示内容
 });

//value值回填,用split分隔字符串
 $('select[name="dutylogtype[]"]').selectpicker('val',$dutylogconfig.split(","));

3. 动态生成option

html代码:

<select multiple class="form-control"  name="dutyuserid" id="dutyuserid"  data-live-search="true"  required></select> 

JS代码:

$('select[name="dutyuserid"]').selectpicker({
    noneSelectedText: '请选择人员...' //默认显示内容
});

//获取值班人员信息
function getUserId() {
    $.ajax({
        url: "__APP__/Mainten/Userinfoext/getListForCombobox",
        type:"get",
        dataType: 'json',
        async: false,
        success: function (data) {
            data.forEach(function(element){
                // alert(element.id);
                $('select[name="dutyuserid"]').append('<option value="'+element.id + '" >' + element.personname + '</option>');
            });
            // $('select[name="dutyuserid"]').selectpicker('refresh');
        }
    })
}

4.其他方法

获取已选的项:

var selectedValues = [];    
$("#dutyuserid:selected").each(function(){
    selectedValues.push($(this).val()); 
});

选择指定项(编辑回显使用):

单选:$(’.selectpicker’).selectpicker(‘val’, ‘列表id’);

多选:var arr=str.split(’,’); $(’.selectpicker’).selectpicker(‘val’, arr);

<think>Hmm, the user is asking about implementing multi-select functionality with the uv-picker component. Let me piece together the clues from their query and the provided references. First, looking at reference [1], I see the basic uv-picker implementation with @confirm and @change handlers. Reference [2] shows a more complex implementation using cm-picker-popup wrapper with value binding and API integration. Then reference [3] mentions display issues when multiple pickers are present - which is relevant because multi-select often involves multiple picker instances. The core challenge seems to be that uv-picker itself doesn't have built-in multi-select. From what I recall about Vue/uni-app patterns, we'd need to implement this through either: - Multiple single-select pickers (as mentioned in my solution) - Forking/modifying the component source - Finding a wrapper component like cm-picker-popup I notice reference [2]'s cm-picker-popup has search capability and configurable options - this might be extendable for multi-select if we can bind an array instead of single value. But since the user specifically asked about uv-picker, I should focus on that. The key implementation points would be: Managing array state for selected values Modifying the confirm handler to push to array instead of replacing Adding visual indicators for selected items (checkboxes? tags?) Implementing deselection logic The display issue in reference [3] is particularly important - when dealing with multiple pickers, we must ensure each maintains independent selection state. The solution should emphasize using refs or unique keys per picker instance. The user seems comfortable with technical details since they're referencing specific components. I'll include both basic and advanced approaches - the axios mock shows how they might handle API-fetched options which is common in real projects. Should also mention common pain points like scroll position resetting between selections. </think>根据引用内容和uv-ui组件特性,uv-picker实现功能的方法如下(需结合自定义逻辑): ### 基础实现方案 ```html <template> <!-- 模式需添加mode="mutil-column" --> <u-picker :show="showMultiPicker" mode="mutil-column" :columns="multiColumns" @confirm="confirmMulti" ></u-picker> <!-- 显示已项 --> <u-tag v-for="item in selectedItems" :text="item.label" /> </template> <script> export default { data() { return { showMultiPicker: true, selectedItems: [], // 存储结果 multiColumns: [ // 列数据结构 [ { label: '项A', value: 1 }, { label: '项B', value: 2 }, { label: '项C', value: 3 } ], [ { label: '子项1', value: 'a' }, { label: '子项2', value: 'b' } ] ] } }, methods: { confirmMulti(e) { // 确认处理 const [firstCol, secondCol] = e.value this.selectedItems.push({ label: `${firstCol.label}-${secondCol.label}`, value: `${firstCol.value}_${secondCol.value}` }) } } } </script> ``` ### 关键配置说明 1. **模式设置**: ```html mode="mutil-column" <!-- 启用择模式 --> range-key="label" <!-- 指定显示的字段名 --> ``` 2. **数据结构**: ```javascript multiColumns: [ [{label:'水果',value:0}, {label:'蔬菜',value:1}], // 第一列 [{label:'苹果',value:10}, {label:'香蕉',value:11}] // 第二列 ] ``` 3. **取值逻辑**: ```javascript // 获取择结果 confirmMulti(e) { const selectedValues = e.value.map(item => item.value) console.log(selectedValues) // [0, 10] 格式 } ``` ### 高级技巧 1. **联动择**(引用[2]): ```html <cm-picker-popup v-model="selectedValues" :multiple="true" <!-- 启用模式 --> :max-count="3" <!-- 限制最大择数 --> /> ``` 2. **显示优化**(解决引用[3]的高亮问题): ```javascript watch: { selectedValues(val) { this.$refs.uPicker.refresh() // 强制刷新择器状态 } } ``` 3. **异步加载项**(引用[2]扩展): ```javascript :async-load="true" @load="async options => await fetchOptions()" ``` ### 注意事项 1. 官方文档未直接提供属性,需通过`multi-column`模式+数组存储实现 2. 跨列择时需处理数据结构一致性 3. 移动端建议配合`<u-checkbox-group>`使用 4. 当存在个picker时,需为每个实例设置独立`ref`(引用[3]问题解决方案) > 完整实现可参考uv-ui的`picker`组件文档中**择**章节[^1],实际开发中建议使用`<uv-checkbox>`+弹出层组合实现更直观的交互[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值