easyUi combobox如何实现下拉模糊查询功能,前后端实现动态加载数据

网上很多分享静态数据的下拉模糊查询,我在这儿再补充下动态数据的场景。

模糊下拉单选查询功能UI:

1.前端代码:

1.1 html语句:

<div id="div1" name="div1" class="easyui-dialog" style="width:800px;height:220px;padding:10px 10px"
     closed="true" modal=true>
    <tr>
        <td><span>选择:</span></td>
        <td>
            <input id="queryUser" name="queryUser" type="text" style="width:125px;padding-left: 10px;"/>
        </td>
    </tr>
</div>

1.2 combobox

js语句:

<script type="text/javascript">
 $(function () {
        //此处queryUser是我的input标签的id
        BindBuildingDictItem('queryUser');
    });

    function BindBuildingDictItem(comboid) {
        $.ajax({
            type: 'post',
            url: sys.rootPath + "/sf/manager/queryAll",//这是访问后端的接口,其中sys.rootPath的值为:${request.contextPath}${contextPath}
            dataType: 'json',
            success: function (data) {

                $('#' + comboid).combobox({
                    valueField: 'uid',//真正要存储到后台的内容
                    textField: 'userName', //选择框显示的内容
                    panelHeight: 'auto',
                    required: true, //必须输入
                    editable: true,//可编辑
                    data: data.rows
                });
                $('#' + comboid).combobox('select', "");
            }
        });
    }

</script>

2.后端springboot 返回的数据:

controller 代码:

@Controller
@RequestMapping("/sf/manager")
public class ManagerController {

    @ResponseBody
    @RequestMapping(value = "/queryAll")
    public R queryAll() {
  
            List<userVo> list = userService.queryAll();
            int total = fofUserInfoService.count();
            return R.ok(list);
    }
}

R的代码:

@Data
public class R {

    /**
     * 信息代码
     */
    private String code;
    /**
     * 信息说明
     */
    private String message;
    /**
     * 返回数据或jqgrid中的root
     */
    private Object rows;

    public R(String code, String message, Object rows) {
        this.code = code;
        this.message = message;
        this.rows = rows;
    }

    public static R ok(Object rows) {
        return new R("0000", "success", rows);
    }
}

返回结果:

{
    "code": "0000", 
    "message": "success", 
    "rows": [
        {
            "uid": 9, 
            "userName": "test0001", 
            "password": null, 
        },
        {
            "uid": 9, 
            "userName": "test001", 
            "password": null, 
        },
        {
            "uid": 9, 
            "userName": "xxctest1", 
            "password": null, 
        }
    ]
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
easyui combobox下拉框默认是单选的,如果要实现多选,需要做以下修改: 1. 在combobox的options中添加multiple:true,表示启用多选模式。 2. 在combobox的options中添加onSelect和onUnselect两个事件,用于在选择和取消选择时更新选中的值。 3. 在combobox的panel中添加checkbox或者复选框,用于选择多个选项。 以下是实现多选的示例代码: HTML代码: ``` <input id="cc" class="easyui-combobox" style="width:200px;"> ``` JavaScript代码: ``` $('#cc').combobox({ url: 'get_data.php', valueField: 'id', textField: 'text', multiple: true, panelHeight: 'auto', onSelect: function(record){ var value = $(this).combobox('getValues'); value.push(record.id); $(this).combobox('setValues', value); }, onUnselect: function(record){ var value = $(this).combobox('getValues'); var index = value.indexOf(record.id); if (index >= 0){ value.splice(index, 1); } $(this).combobox('setValues', value); }, onLoadSuccess: function(){ var data = $(this).combobox('getData'); var value = $(this).combobox('getValues'); var panel = $(this).combobox('panel'); panel.find('.combo-panel-checkbox').remove(); $.each(data, function(index, item){ var checkbox = $('<input type="checkbox" class="combo-panel-checkbox">').val(item.id); if (value.indexOf(item.id) >= 0){ checkbox.prop('checked', true); } checkbox.insertBefore(panel.find('.combo-panel-list')); }); }, onShowPanel: function(){ var data = $(this).combobox('getData'); var value = $(this).combobox('getValues'); var panel = $(this).combobox('panel'); panel.find('.combo-panel-checkbox').prop('checked', false); $.each(data, function(index, item){ if (value.indexOf(item.id) >= 0){ panel.find('.combo-panel-checkbox[value="'+item.id+'"]').prop('checked', true); } }); } }); ``` 注:示例代码中的get_data.php是一个返回JSON格式数据的接口,可以根据实际情况修改。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值