jQuery Autocomplete 实现搜索框自动提示功能

jQuery Autocomplete 实现搜索框自动提示功能


有个需求要求输入运营商名称去自动提示含输入框关键字的运营商名称,有点类似百度的搜索框,找了一些资料,用jQuery Autocomplete插件,在获取数据的时候异步去调接口。记录一下以后方便自己查找。

前端(关键代码):
记得引入js

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<div class="flex-center" th:if="${role == '1' || role == '2'}">
 	<label for="merchantName">运营商名:</label>
    <input type="text" style="width: 335px;" class="form-control" name="merchantName" id="merchantName">
</div>
('#merchantName').keyup(function(){
        $("#merchantName").autocomplete({
            minLength: 1,
            source: function( request, response ) {
                var merchantName = $("#merchantName").val();
                $.ajax({
                    url: "xxx",
                    dataType: "json",
                    type:"post",
                    data: {"merchantName": merchantName},
                    success: function( data ) {
                        response($.map( data, function( item ) {
                            return {
                            	//数据库中用remarks字段存的merchantName
                                value:item.remarks
                            }
                        }));
                    }
                });
            }
        });
    });

后端(关键代码):

@RequestMapping(value = "/autoQueryMerchant")
@ResponseBody
public String autoQueryMerchant(@RequestParam String merchantName){
	return JSON.toJSONString(asOperatorRepository.queryByMerchantNameAuto(merchantName));
}

要json转换一下

mapper代码:

<select id="queryByMerchantNameAuto" resultMap="BaseResultMap">

    SELECT ao.REMARKS FROM `tb_user` as tu
    LEFT JOIN as_operator as ao
    ON tu.ID = ao.USER_ID
    WHERE
        tu.ROLE = '3'
    <if test="remarks != null and remarks != ''">
        AND ao.REMARKS like concat ('%',#{remarks},'%')
    </if>
</select>

参考:
https://www.runoob.com/jqueryui/api-autocomplete.html
https://blog.csdn.net/Hero_QHZ/article/details/65630992

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值