select2加载远程数据示例

核心js

$("#query_pack_code").select2({
            language: "zh-CN",
            allowClear: true,
            width: "150px",
            placeholder: "请选择",
            ajax: {
                url: "monitor/historyQuery/getPackCodeOptions.mvc",
                dataType: 'json',
                delay: 250,
                data: function (params) {
                    params.offset = 10;  //显示十条
                    params.page = params.page || 1; //页码
                    return {
                        name: params.term,
                        page: params.page,
                        offset: params.offset
                    };
                },
                cache: false,
                /*
                *@params res 返回值
                *@params params 参数
                */
                processResults: function (res, params) {
                        var users = res.data;
                        var options = [];
                        for (var i = 0, len = users.length; i < len; i++) {
                            var option = {
                                "id": users[i]["serialNo"],
                                "text": (users[i]["serialNo"])
                            };
                            options.push(option);
                        }
                        return {
                            results: options,
                            pagination: {
                                more: (params.page * params.offset) < res.total
                            }
                        };
                },
                escapeMarkup: function (markup) {
                    return markup;
                },
                minimumInputLength: 1
            }
        });

后台使用的springmvc+mybatis 分页使用的插件com.github.miemiedev.mybatis.paginator.domain.PageBounds;

 /**
 * 
 * <p>Description: 下拉框异步加载</p>
 * @param res 请求
 * @return 结果集
 */
@RequestMapping(value="/getPackCodeOptions.mvc")
@ResponseBody
public Object getPackCodeOptions(HttpServletRequest res){
    //分页
    PageBounds pageBounds;
    pageBounds = new PageBounds();
    //查询条件
    String name = res.getParameter("name");
    //每页显示条数  
    Integer offset = Integer.valueOf(res.getParameter("offset"));  
    //当前页码
    Integer page = Integer.valueOf(res.getParameter("page"));  
    if (page == 1) {  
        page = 0;  
    } else {  
        page = (page - 1) * offset;  
    }
    pageBounds.setLimit(offset);
    pageBounds.setPage(page);
    Map<String, Object> params;
    params = new HashMap<String, Object>();
    params.put("name", name);
    EntityPageBean<TmPackSerial> pageBean;
    Map<String, Object> dataMap=new HashMap<String, Object>();
    try {
        pageBean = this.tmPackSerialService.getPackCodeOptions(params, pageBounds);
        dataMap.put("total", pageBean.getiTotalRecords());
        dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
        dataMap.put("data", pageBean.getResults());
    } catch (DaoException e) {
        logger.error("查询出错:", e);
        return null;
    }
    return dataMap;  
}

效果图:
795862-20170801105730802-1155604496.png

参考资料: select2主页

转载于:https://www.cnblogs.com/xieshuang/p/7267340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在vue2 select中进行远程搜索数据回显,需要使用v-model来实现数据的双向绑定。 首先,在vue组件的data中定义一个数组来存储选中的数据: ``` data() { return { selectedData: [] } } ``` 然后,在vue2 select中使用v-model绑定该数组: ``` <template> <v-select v-model="selectedData" :items="items" :search-input.sync="search" label="name" item-value="id" item-text="name" :loading="loading" @search="searchData"></v-select> </template> ``` 在上面的代码中,items表示从远程获取的数据,search表示搜索关键字,loading表示数据是否正在加载。@search是vue2 select的一个事件,可以在搜索框输入内容时触发,我们可以在这里进行远程搜索。 最后,在searchData方法中,我们可以通过远程接口获取数据,并将已选中的数据回显到vue2 select中: ``` methods: { async searchData() { this.loading = true; // 调用远程接口获取数据 const res = await axios.get(`/api/search?keyword=${this.search}`); this.items = res.data; this.loading = false; // 将已选中的数据回显到vue2 select中 this.selectedData.forEach((item) => { const index = this.items.findIndex((i) => i.id === item.id); if (index !== -1) { this.$set(this.items[index], 'selected', true); } }); } } ``` 在上面的代码中,我们首先调用远程接口获取数据,并将数据存储到items数组中。然后,遍历已选中的数据,通过findIndex方法找到对应的数据在items数组中的索引,然后使用$set方法将selected属性设置为true,从而实现回显。 完整的代码示例: ``` <template> <v-select v-model="selectedData" :items="items" :search-input.sync="search" label="name" item-value="id" item-text="name" :loading="loading" @search="searchData"></v-select> </template> <script> import axios from 'axios'; export default { data() { return { items: [], search: '', loading: false, selectedData: [] } }, methods: { async searchData() { this.loading = true; // 调用远程接口获取数据 const res = await axios.get(`/api/search?keyword=${this.search}`); this.items = res.data; this.loading = false; // 将已选中的数据回显到vue2 select中 this.selectedData.forEach((item) => { const index = this.items.findIndex((i) => i.id === item.id); if (index !== -1) { this.$set(this.items[index], 'selected', true); } }); } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值