NipGeihou's blog NipGeihou's blog
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档

NipGeihou

我见青山多妩媚,料青山见我应如是
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档
  • 【经验分享】快速使用typeahead

    • 效果图
      • 准备
        • 开始
          • 导入js库
          • HTML
          • CSS
          • JavaScript(重点)
        • 最后说"几"句
          • 参考文章
          NipGeihou
          2019-7-24
          JavaScript
          目录

          【经验分享】快速使用typeahead

          # 效果图

          实现功能: 在 input 输入框输入字符串时,通过 typeahead 异步请求服务器接口,返回 json 数据,呈现类始于下拉框的样子,选择后自动补全相关输入框,提升用户体验。

          # 准备

          • jquery.js (opens new window)
          • handlebars.js (opens new window) - 用于结果(下拉框)显示样式
          • typeahead.bundle.js (opens new window)

          # 开始

          # 导入 js 库

          在需要用到 typeahead 的 html 页面,依次导入 jquery.js、handlebars.js、typeahead.js,顺序错误会导致无法调用 jQuery

          <!-- 最好把bootstrap也一同加上 -->
          <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.css" rel="stylesheet"> 
          <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
          <script src="https://cdn.bootcss.com/handlebars.js/4.1.2/handlebars.js"></script>
          <script src="https://cdn.bootcss.com/typeahead.js/0.11.1/typeahead.bundle.js"></script>
          

          # HTML

              <input type="text" id="typeahead_get" class="form-control">
              <input type="text" id="typeahead_post" class="form-control">
          

          HTML 部分不需要过多的操作,只要添加一个 input:text 元素即可,并给它一个 id 或者 class,这里我给它一个 typeahead 的 class,也可以是其他,并不限制。

          # CSS

          typeahead 默认样式是一个透明背景的,甚至让你有种是不是哪里出错了的错觉

          <style>
              /*typeahead显示样式*/
              #custom-templates .empty-message {
                  padding: 5px 10px;
                  text-align: center;
              }
          
              .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 { /* used to be tt-dropdown-menu in older versions */
                  width: 100%;
                  margin-top: 4px;
                  padding: 4px 0;
                  background-color: #fff;
                  border: 1px solid #ccc;
                  border: 1px solid rgba(0, 0, 0, 0.2);
                  -webkit-border-radius: 4px;
                  -moz-border-radius: 4px;
                  border-radius: 4px;
                  -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;
                  line-height: 24px;
              }
          
              .tt-suggestion.tt-cursor, .tt-suggestion:hover {
                  color: #fff;
                  background-color: #0097cf;
              }
          
              .tt-suggestion p {
                  margin: 0;
              }
          
              .twitter-typeahead {
                  width: 100%;
              }
          </style>
          

          # JavaScript(重点)

          服务器响应的 JSON 数组

          [
              {"name":1,"title":"辣条"},
              {"name":2,"title":"涪陵榨菜"},
              {"name":3,"title":"茶叶蛋"},
              {"name":4,"title":"橙汁"},
              {"name":5,"title":"来自Post请求响应,你输入的值为s"}
          ]
          
              // Get请求
              var get_food = new Bloodhound({
                  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('title'),
                  queryTokenizer: Bloodhound.tokenizers.whitespace,
                  remote: {
                      url: "food?f=%QUERY",
                      wildcard: '%QUERY'
                  }
              });
          
              //Post 请求
              var post_food = new Bloodhound({
                  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('title'),
                  queryTokenizer: Bloodhound.tokenizers.whitespace,
          
                  remote: {
                      url: "food#%QUERY",
                      wildcard: '%QUERY',
                      transport: function (opts, onSuccess, onError) {
                          var url = opts.url.split("#")[0];
                          var query = opts.url.split("#")[1];
                          $.ajax({
                              url: url,
                              data: {f:query},
                              type: "POST",
                              success: onSuccess,
                              error: onError,
                          });
                      }
                  }
              });
          
          
              //Get触发方法
              $('#typeahead_get').typeahead({
                      hint: false,
                      highlight: true,
                      minLength: 1
                  }
                  , {
                      name: 'get_food',
                      display: 'title',
                      source: get_food,
                      templates: {
                          suggestion: Handlebars.compile('<div><strong>{{name}}</strong> – {{title}}</div>')
                      },
                  });
          
          
              //Post触发方法
              $('#typeahead_post').typeahead({
                      hint: false,
                      highlight: true,
                      minLength: 1
                  }
                  , {
                      name: 'post_food',
                      display: 'title',
                      source: post_food,
                      templates: {
                          suggestion: Handlebars.compile('<div><strong>{{name}}</strong> – {{title}}</div>')
                      },
                  });
          

          # 最后说 "几" 句

          待编辑


          # 参考文章

          typeahead.js 使用教程小结 - 前端菜鸟 - SegmentFault 思否 (opens new window)

          Twitter 的 typeahead.js 建议不是样式 (没有边框,透明背景等) - 码客 (opens new window)

          【选择后获取传递的其他值】Example of typeahead:selected event・Issue #300・twitter/typeahead.js (opens new window)

          【解决返回结果小于默认 5 条不显示问题】Remote completion・Issue #1218・twitter/typeahead.js (opens new window)

          【解决动态添加 input,typeahead 不起作用问题】jquery - Twitter typeahead autocomplete to dynamically added inputs - Stack Overflow (opens new window)

          typeahead 插件基础知识 - 明洋的专栏 - CSDN 博客 (opens new window)

          typeahead.js 官网 (opens new window)

          #JavaScript#typeahead
          上次更新: 2024/03/11, 22:37:05
          最近更新
          01
          Docker Swarm
          04-18
          02
          安全隧道 - gost
          04-17
          03
          Solana最佳实践
          04-16
          更多文章>
          Theme by Vdoing | Copyright © 2018-2025 NipGeihou | 友情链接
          • 跟随系统
          • 浅色模式
          • 深色模式
          • 阅读模式