使用 jQuery.TypeAhead 让文本框自动完成 (四)(自定义模板)
2019-04-30 22:24 音乐让我说 阅读(513) 评论(0) 编辑 收藏 举报项目地址:https://github.com/twitter/typeahead.js
直接贴代码了:
@section headSection { <script type="text/javascript"> $(document).ready(function () { var bestPictures = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), queryTokenizer: Bloodhound.tokenizers.whitespace, identify: function(obj) { return obj.id; } /* 这个函数是告诉引擎对象的 ID */, remote: { url: '@Url.Action("Trade_Code_List_Search_By_Trad_Cod")?searchTradCode=%QUERY', wildcard: '%QUERY' } }); $('#trad_cod_textbox').typeahead(null,{ name: 'best-pictures', source: bestPictures, display: 'value', limit: 10, templates: { suggestion: Handlebars.compile('<div><strong>{{id}}</strong> – {{value}}</div>'), empty: [ '<div class="empty-message">', 'unable to find any Best Picture winners that match the current query', '</div>' ].join('\n') } }).bind('typeahead:select', function(ev, suggestion) { alert("you select: " + "(id=" + suggestion.id + ", value="+ suggestion.value +")"); //绑定自定义事件 }); }); </script> } @Html.Partial("UCjQueryTypeAheadJsScript") <!-- 这里就是文本框 --> <input id="trad_cod_textbox" class="text-input" type="text" value="">
UCjQueryTypeAheadJsScript.cshtml(注意:多个一个JS)
<!-- both bloodhound.js and typeahead.jquery.js have a dependency on jQuery 1.9+. --> <!-- 下面这个 handlebars.js 当需要用 typeahead 的 templates suggestion 时必须要,否则可选 --> <script src="~/resources/plugins/jQuery-type-ahead.js/handlebars.js"></script> <script src="~/resources/plugins/jQuery-type-ahead.js/typeahead.bundle.min.js"></script> <link href="~/resources/plugins/jQuery-type-ahead.js/examples.css" rel="stylesheet" />
Controller.cs
public ActionResult Trade_Code_List_Search_By_Trad_Cod(string searchTradCode) { List<Trd_Mas_Simple_Info> simpleTradMasList = new Trd_MasService().GetSimpleListByTradCode(searchTradCode); return Json(simpleTradMasList.Select(c => new { value = c.trd_cod + " - " + c.trd_des, id = c.trd_cod }), JsonRequestBehavior.AllowGet); }
examples.css
.tt-query { -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); } .tt-hint { color: #999 } .tt-menu { width: 422px; margin: 12px 0; padding: 8px 0; background-color: #fff; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.2); -webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px; -webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2); -moz-box-shadow: 0 5px 10px rgba(0,0,0,.2); box-shadow: 0 5px 10px rgba(0,0,0,.2); } .tt-suggestion { padding: 3px 20px; font-size: 18px; line-height: 24px; } .tt-suggestion:hover { cursor: pointer; color: #fff; background-color: #0097cf; } .tt-suggestion.tt-cursor { color: #fff; background-color: #0097cf; } .tt-suggestion p { margin: 0; } .gist { font-size: 14px; }
运行效果
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步