bulidfer 2018-08-06 08:25 采纳率: 0%
浏览 4963
已结题

求一个select2 ajax请求后台数据搜索下拉框功能 并回显选中项的demo

软件后台是java编写的
求一个select2 ajax请求后台数据搜索下拉框功能 并回显选中项的demo

https://www.cnblogs.com/-mrl/p/7650156.html
这个帖子我已经试过了 搜索功能失效 不要在推荐了

强调一下 是用ajax请求的数据 而不是用本地的方式注入

  • 写回答

7条回答

  • bulidfer 2018-08-06 08:48
    关注

    图片说明

    这些都试了 不行的

     /* 1-本地注入方式 */
    
            /*$(".downList2").select2({
                // $("select[name='xuewei_id']").select2({
                // $('select[id="xuewei_id"]').select2({
                // $('#xuewei_id').select2({
    
                placeholder : "请至少选择一个人名",
                tags : true,
                createTag : function(decorated, params) {
                    return null;
                },
                width : '256px',
    
            });*/
            // 2- AJAX获取数据方式
          $(".downList2").select2({
              ajax: {
                type:'GET',
                url: 'http://localhost:8080/erzhentang/manage/forAjax.do?requestType=12.5_1',
                dataType: 'json',
                delay: 250,
                data: function (params) {
                  return {
                    q: params.term, // search term 请求参数
                    page: params.page
                  };
                },
                processResults: function (data, params) {
                  params.page = params.page || 1;
                  var itemList = [];
                  var arr = data.result.list
                  for(item in arr){
                      itemList.push({id: item, text: arr[item]})
                  }
                  return {
    //              results: data.items,//itemList
                    results: data,//itemList
                    pagination: {
                      more: (params.page * 2) < data.total_count
                    }
                  };
                },
                cache: true
              },
              placeholder:'请选择',//默认文字提示
              language: "zh-CN",
              tags: true,//允许手动添加
            createTag : function(decorated, params) {
                return null;
            },
              allowClear: true,//允许清空
              escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
              minimumInputLength: 0,
              formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
              formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
            });
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 优质github账号直接兑换rmb,感兴趣伙伴可以私信
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)