`
mybwu_com
  • 浏览: 192743 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

使用typeahead js 做quick search

 
阅读更多
js references :


[Handlebar]


http://handlebarsjs.com/


[typeahead]


https://twitter.github.io/typeahead.js/




demo html :


<script src="jquery-1.11.1.min.js"></script>
<script src="typeahead.bundle.js"></script>
<script src="handlebars-v1.3.0.js"></script>
<p>
1.Basic
</p>


<div id="the-basics">
  <input class="typeahead" type="text" placeholder="States of USA">
</div>




<script>


var substringMatcher_basic = function(strs) {
  return function findMatches(q, cb) {
    var matches, substrRegex;
 
    // an array that will be populated with substring matches
    matches = [];
 
    // regex used to determine if a string contains the substring `q`
    substrRegex = new RegExp(q, 'i');
 
    // iterate through the pool of strings and for any string that
    // contains the substring `q`, add it to the `matches` array
    $.each(strs, function(i, str) {
      if (substrRegex.test(str)) {
        // the typeahead jQuery plugin expects suggestions to a
        // JavaScript object, refer to typeahead docs for more info
        matches.push({ value: str });
      }
    });
 
    cb(matches);
  };
};
 
var _data_states_basic = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];
 
$('#the-basics .typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'states',
  displayKey: 'value',
  source: substringMatcher_basic(_data_states_basic)
});


</script>


<p>
2.Using Bloodhound
</p>


<div id="bloodhound">
  <input class="typeahead" type="text" placeholder="States of USA">
</div>


<script>


var _data_states_bloodhound = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];


var states_bloodhound = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  local: $.map(_data_states_bloodhound, function(state) { return { value: state }; })
});
 
// kicks off the loading/processing of `local` and `prefetch`
states_bloodhound.initialize();
 
$('#bloodhound .typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'states',
  displayKey: 'value',
  // `ttAdapter` wraps the suggestion engine in an adapter that
  // is compatible with the typeahead jQuery plugin
  source: states_bloodhound.ttAdapter()
});


</script>


<p>
3. Pre-fetch (need to replace json URL)
</p>


<div id="prefetch">
  <input class="typeahead" type="text" placeholder="Countries">
</div>


<script>
var prefetch_countries = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  limit: 10,
  prefetch: {
    url: 'your json url here',
    filter: function(list) {
      return $.map(list, function(country) { return { name: country }; });
    }
  }
});
 
// kicks off the loading/processing of `local` and `prefetch`
prefetch_countries.initialize();
 
// passing in `null` for the `options` arguments will result in the default
// options being used
$('#prefetch .typeahead').typeahead(null, {
  name: 'prefetch_countries',
  displayKey: 'name',
  // `ttAdapter` wraps the suggestion engine in an adapter that
  // is compatible with the typeahead jQuery plugin
  source: prefetch_countries.ttAdapter()
});


</script>


<p>4.Custom Template</p>


<style>
#custom-templates .empty-message {
  padding: 5px 10px;
 text-align: center;
}


</style>


<div id="custom-templates">
  <input class="typeahead" type="text" placeholder="Oscar winners for Best Picture">
</div>




<script>
var _data_states_template_countries = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];


var custome_template_countries = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  limit: 10,
  local: $.map(_data_states_template_countries, function(state) { return { name: state }; })
});
 
// kicks off the loading/processing of `local` and `prefetch`
custome_template_countries.initialize();


$('#custom-templates .typeahead').typeahead(null, {
  
  displayKey: 'name',
  source: custome_template_countries.ttAdapter(),
  templates: {
    empty: [
      '<div class="empty-message">',
      'unable to find a country',
      '</div>'
    ].join('\n'),
    suggestion: Handlebars.compile('<p><strong>{{name}}</strong></p>')
  }
});
</script>


<p>5.multiple data set</p>


<style>
#multiple-datasets .league-name {
  margin: 0 20px 5px 20px;
  padding: 3px 0;
  border-bottom: 1px solid #ccc;
}
</style>


<div id="multiple-datasets">
  <input class="typeahead" type="text" placeholder="NBA and NHL teams">
</div>


<script>
var _data_nhlteam = [{ "team": "New Jersey Devils" },{ "team": "New York Islanders" },{ "team": "New York Rangers" },{ "team": "Philadelphia Flyers" },{ "team": "Pittsburgh Penguins" },{ "team": "Chicago Blackhawks" },{ "team": "Columbus Blue Jackets" },{ "team": "Detroit Red Wings" },{ "team": "Nashville Predators" },{ "team": "St. Louis Blues" },{ "team": "Boston Bruins" },{ "team": "Buffalo Sabres" },{ "team": "Montreal Canadiens" },{ "team": "Ottawa Senators" },{ "team": "Toronto Maple Leafs" },{ "team": "Calgary Flames" },{ "team": "Colorado Avalanche" },{ "team": "Edmonton Oilers" },{ "team": "Minnesota Wild" },{ "team": "Vancouver Canucks" },{ "team": "Carolina Hurricanes" },{ "team": "Florida Panthers" },{ "team": "Tampa Bay Lightning" },{ "team": "Washington Capitals" },{ "team": "Winnipeg Jets" },{ "team": "Anaheim Ducks" },{ "team": "Dallas Stars" },{ "team": "Los Angeles Kings" },{ "team": "Phoenix Coyotes" },{ "team": "San Jose Sharks" }];


var _data_nbateam = [{ "team": "Boston Celtics" },{ "team": "Dallas Mavericks" },{ "team": "Brooklyn Nets" },{ "team": "Houston Rockets" },{ "team": "New York Knicks" },{ "team": "Memphis Grizzlies" },{ "team": "Philadelphia 76ers" },{ "team": "New Orleans Hornets" },{ "team": "Toronto Raptors" },{ "team": "San Antonio Spurs" },{ "team": "Chicago Bulls" },{ "team": "Denver Nuggets" },{ "team": "Cleveland Cavaliers" },{ "team": "Minnesota Timberwolves" },{ "team": "Detroit Pistons" },{ "team": "Portland Trail Blazers" },{ "team": "Indiana Pacers" },{ "team": "Oklahoma City Thunder" },{ "team": "Milwaukee Bucks" },{ "team": "Utah Jazz" },{ "team": "Atlanta Hawks" },{ "team": "Golden State Warriors" },{ "team": "Charlotte Bobcats" },{ "team": "Los Angeles Clippers" },{ "team": "Miami Heat" },{ "team": "Los Angeles Lakers" },{ "team": "Orlando Magic" },{ "team": "Phoenix Suns" },{ "team": "Washington Wizards" },{ "team": "Sacramento Kings" }];


var nhlTeams = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  local: _data_nhlteam
});
 
var nbaTeams = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  local: _data_nbateam
});
 
nhlTeams.initialize();
nbaTeams.initialize();


$('#multiple-datasets .typeahead').typeahead({
  highlight: true
},
{
  displayKey: 'team',
  source: nbaTeams.ttAdapter(),
  templates: {
  header: '<h3 class="league-name">NBA Teams</h3>'
  }
},
{
  displayKey: 'team',
  source: nhlTeams.ttAdapter(),
  templates: {
   header: '<h3 class="league-name">NHL Teams</h3>'
  }
});


</script>


<p>
6. scrollable drop down
</p>


<style>
#scrollable-dropdown-menu .tt-dropdown-menu {
  max-height: 100px;
  width:600px;
  overflow-y: auto;
}
</style>


<div id="scrollable-dropdown-menu">
  <input class="typeahead" type="text" placeholder="Countries">
</div>


<script>
var _data_scrollable_dropdown_countries = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];


var _data_scrollable_dropdown_countries = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  limit: 10,
  local: $.map(_data_states_template_countries, function(state) { return { name: state }; })
});
 
// kicks off the loading/processing of `local` and `prefetch`
_data_scrollable_dropdown_countries.initialize();


$('#scrollable-dropdown-menu .typeahead').typeahead(null, {
  name: 'countries',
  displayKey: 'name',
  source: _data_scrollable_dropdown_countries.ttAdapter()
});


</script>


分享到:
评论

相关推荐

    typeahead.js-master

    通过CSS和JavaScript,可以完全定制Typeahead.js的样式和行为。例如,修改提示框的样式、添加动画效果,或者扩展功能如自动填充等。此外,还可以通过插件系统扩展Typeahead.js的功能,如添加高亮、分页等功能。 ...

    bootstrap-typeahead.js

    Bootstrap 中的 Typeahead 组件就是通常所说的自动完成 AutoComplete,功能很强大,上传的包括两个文件一个bootstrap-typeahead.js和一个underscore-min.js,前者基本上可以满足一般需求了,后一个js是使用对象数据时...

    typeahead基本使用

    Typeahead通常是由JavaScript库实现的,例如Twitter的Bootstrap框架中的`bootstrap-typeahead.js`。源码分析可能包括理解其工作原理,如何与HTML元素交互,以及如何通过AJAX请求获取并展示预测数据。 1. **初始化:...

    前端项目-typeahead.js-bootstrap-css.zip

    Typeahead.js是Twitter开源的一个JavaScript库,它提供了自动完成的功能,可以在用户输入时快速显示匹配的数据建议。Typeahead.js的核心功能包括异步数据加载、分组结果和自定义模板,使得创建交互式搜索体验变得...

    typeahead.js:typeahead.js是一个快速且功能齐全的自动完成库

    入门如何获取typeahead.js取决于您: 用安装: $ bower install corejs-typeahead 使用安装: $ npm install corejs-typeahead 使用安装: $ composer require corejavascript/typeahead.js 单独下载最新的dist文件...

    bootstrap-typeahead 3

    要使用Bootstrap Typeahead 3,首先需要确保在项目中包含了Bootstrap CSS和JavaScript库,以及Typeahead的JavaScript文件。通常,这些文件可以通过CDN链接或本地文件系统引入。接着,通过HTML标记和适当的类来创建一...

    typeahead.js

    typeahead.js实现边输入边查询

    ember-typeahead:使用typeahead.js的EmberJS自动完成组件

    Ember Typeahead.js组件 用于自动填充部件使用 安装 使用Bower安装 bower install ember-typeahead 用法 包括typeahead.js和ember-typeahead.js [removed][removed] [removed][removed] 在您的车把模板中添加...

    flutter_typeahead.zip

    要使用 `flutter_typeahead`,首先需要将插件添加到项目的 `pubspec.yaml` 文件中,然后通过 `import` 导入相应的库。接着,可以创建一个 `TypeAheadField` 实例,配置其样式、输入回调、数据源过滤函数等属性。例如...

    Bootstrap3使用typeahead插件实现自动补全功能

    在bootstrap中使用typeahead插件,完成自动补全 相关的文档:https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md 数据源: Local:数组 prefectch:json remote等方式 ——————...

    typeaheadjs快速和功能强大的JavaScript输入自动完成库

    **Typeahead.js:快速与强大的JavaScript输入自动完成库** Typeahead.js是由Twitter开发的一个轻量级但功能丰富的JavaScript库,专为实现输入自动补全功能而设计。它为Web开发者提供了一种简单、灵活的方式来增强...

    typeaheadjs:[READONLY] typeahead.js的凉亭

    **标题解析:** “typeaheadjs:[READONLY] typeahead.js的凉亭”这个标题表明我们...通过这个压缩包,开发者不仅可以了解并使用`typeahead.js`,还可以深入研究其内部工作原理,提升自己的JavaScript和前端开发技能。

    twitter-bootstrap-typeahead-master

    Bootstrap 的 Typeahead 依赖于其自身的 JavaScript 组件和 jQuery 库,因此在使用前需确保已引入这两个库。 1. **基本使用** 在 HTML 结构中,你需要为输入框添加 `data-provide="typeahead"` 属性,以标记这是一...

    typeahead-addresspicker-rails-源码.rar

    4. JavaScript和CSS:Typeahead Addresspicker的核心在于前端的JavaScript文件,它定义了如何使用Typeahead.js和Geocoding API,以及样式文件,用于美化组件的视觉效果。 四、源码分析 1. `app/assets/javascripts...

    BootstrapPlugin - typeahead-ex 使用笔记

    此外,还需要引入typeahead-ex的JS和CSS文件。 2. **HTML结构**:创建一个input元素,为其添加`data-provide="typeahead"`属性,以及可能需要的其他配置属性,如`data-source-url`(用于异步数据)。 3. **初始化...

    前端项目-react-bootstrap-typeahead.zip

    **React-Bootstrap-Typeahead** 是一个前端项目,它利用了React框架和Bootstrap的样式,提供了一个功能丰富的Typeahead组件。Typeahead通常用于实现输入框自动补全的功能,它能够极大地提升用户在网页上的交互体验。...

    meteor-bs-typeahead:带有 Bootstrap 的 Typeahead.js 流星包

    "meteor-bs-typeahead:带有 Bootstrap 的 Typeahead.js 流星包" 这个标题指的是一款在 Meteor 框架中使用的插件,它集成了 Bootstrap 的 Typeahead.js 功能。Meteor 是一个全栈的 JavaScript 开发框架,而 Bootstrap...

    bootstrap4整合tagsinput和typeahead 的静态代码,保证可用

    例如,如果使用静态数组,可以在 JavaScript 中设置如下: ```javascript var suggestions = ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry']; $('#tagInput').tagsinput({ typeahead: { source: ...

    Quora-Typeahead-Search

    在Quora-Typeahead-Search-master这个项目中,我们可以看到源码实现这些概念的具体方式,通过学习和分析这些代码,可以深入理解Typeahead搜索的实现原理,以及如何在Java中有效地应用这些技术。

Global site tag (gtag.js) - Google Analytics