使用bootstrap typeahead插件完成自动补全

项目中需要输入框自动补全,搜索测试没少走弯路,终于成功,记录下来,共同学习,希望大家少进坑.


1.依次引入jQuery、bootstrap、typeahead依赖包。
2.创建容器,指定要实现自动补全的位置
3.使用typeahead函数,给参数赋值

typeaheadjar包的下载地址:https://github.com/bassjobsen/Bootstrap-3-Typeahead/

(使用环境:Bootstrap v4.3.1,Jquery v3.5.1,typeahead v4.0.2,Net:6.0 Razor Page)

<input class="typeahead" data-provide="typeahead" autocomplete="off" />

1.以下代码要放到 @section Scripts{}里

2.Create是代码所在页面,换页面没调通,Search处理

3.input:hidden[name="__RequestVerificationToken 页面中要有form表单才有这个值

4.关键字注意大小写,如datatype写成dataType,autoSelect写成autoselect都会报错

<script type="text/javascript" src="/lib/typeahead/bootstrap3-typeahead.min.js"></script>
<script type="text/javascript">
    var item=[{id:"1",name:"err_1"+result.code},{id:"2",name:"err_2"}];
     $(".typeahead").typeahead({
         autoselect:true,
         source:function(query,process){
             $.ajax({
                 type:"post",
                 url:"Create?handler=Search", 
                 headers:{
                     "RequestVerificationToken":$('input:hidden[name="__RequestVerificationToken"]').val()
                 },
                 data:{query},
                 datatype:"json",
                 success:function(result,code) //result返回的数据,code状态"success"
                 {
                     return process(result);
                 },
                 //complete:function(result)
                 //{
                 //    return process(item);
                 //},
                 error:function(result)
                 {
                     alert("error");
                     return process(item);
                 },

             }).done(function(result){
                 //结束后的处理
             });
         },
          autoSelect:true,
     });
</script>

后台返回JosnResult类型值:

List<AppUser> appUsers = await _dbContext.AppUsers.Where(x => x.Name.Contains(query) || x.PhoneNumber.Contains(query)).ToListAsync();
List<UserList> userlist = new();
foreach (AppUser u in appUsers)
{
    userlist.Add(new UserList { Id = u.Id, Name = u.Name + "," + u.PhoneNumber});
}
var result = new JsonResult(userlist);
result.ContentType = "application/json";
return result;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bootstrap Table 是一个基于 Bootstrap 的可扩展的表格插件,支持多种功能扩展、多种数据源以及自定义格式化等。如果要在 Bootstrap Table 中使用日期控件,可以使用 Bootstrap Datepicker 插件。 下面是一个使用 Bootstrap Table 和 Bootstrap Datepicker 的例子: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Bootstrap Table Demo</title> <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/bootstrap-table/1.18.1/bootstrap-table.min.css"> <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css"> </head> <body> <div class="container"> <table id="table" data-toggle="table" data-url="data.json" data-pagination="true" data-search="true" data-show-refresh="true" data-show-toggle="true" data-show-columns="true"> <thead> <tr> <th data-field="id">ID</th> <th data-field="name">Name</th> <th data-field="date" data-formatter="dateFormat">Date</th> </tr> </thead> </table> </div> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-table/1.18.1/bootstrap-table.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script> <script> function dateFormat(value, row, index) { return value ? moment(value).format('YYYY-MM-DD') : ''; } $(document).ready(function() { $('#table').bootstrapTable(); $('#table').on('editable-save.bs.table', function(e, field, row, oldValue, $el) { if (field === 'date') { row.date = moment(row.date).toISOString(); } }); $('.datepicker').datepicker({ format: 'yyyy-mm-dd', autoclose: true }); }); </script> </body> </html> ``` 在这个例子中,我们首先引入了 BootstrapBootstrap Table 和 Bootstrap Datepicker 的 CSS 和 JavaScript 文件。然后在 HTML 中创建一个表格,并指定了数据源、分页、搜索、刷新、切换列等选项。 表格中的日期列使用了 `data-formatter` 属性来指定自定义格式化函数 `dateFormat`,这个函数使用了 Moment.js 库来将日期格式化成 YYYY-MM-DD 的形式。 在 JavaScript 中,我们使用了 jQuery 的 `$(document).ready()` 函数来初始化 Bootstrap Table,并在表格保存编辑后将日期格式化成 ISO 8601 格式。我们还使用Bootstrap Datepicker 来创建一个日期控件,以便用户可以方便地选择日期。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值