当前位置: 首页 > news >正文

Auto Complete -- Typeahead 和DateRangePicker

一个很好用的AutoComplete js:

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


这里是自己测试的一些demo:


<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>


另外,这个DateRangePicker可以考虑用来选择时间间隔,也是很不错的:

https://github.com/dangrossman/bootstrap-daterangepicker


相关文章:

  • 有多少域名被漏掉了?
  • C# Windows Azure Queue的操作
  • 移动设备管理(MDM)与OMA(OTA)DM协议向导(二)——WAP协议(1)
  • Submit disabled Dropdown
  • Validation failed for one or more entities. See 'EntityValidationErrors' property for more details
  • [Windows编程] 如何判断操作系统是64位还是32位
  • 使用Roslyn动态编译和执行
  • 利用Windows7内置功能管理虚拟磁盘
  • 使用cecil 完成 code injection
  • 善用属性
  • SQLServer任意列之间的聚合
  • [编程技巧] 巧用CPU缓存优化代码:数组 vs. 链表
  • 保存Bitmap到内存流中引发“GDI+中发生一般性错误”
  • Asp.net Mvc使用PagedList分页
  • [Web开发] PSD 转换成HTML/CSS 的工具网站
  • Angular 4.x 动态创建组件
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • Java IO学习笔记一
  • JavaScript 基础知识 - 入门篇(一)
  • javascript数组去重/查找/插入/删除
  • laravel5.5 视图共享数据
  • Linux后台研发超实用命令总结
  • PHP那些事儿
  • React-Native - 收藏集 - 掘金
  • SwizzleMethod 黑魔法
  • Vue2 SSR 的优化之旅
  • VuePress 静态网站生成
  • Vue实战(四)登录/注册页的实现
  • 百度小程序遇到的问题
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 离散点最小(凸)包围边界查找
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 前端代码风格自动化系列(二)之Commitlint
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • mysql面试题分组并合并列
  • 函数计算新功能-----支持C#函数
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • ​马来语翻译中文去哪比较好?
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (Redis使用系列) Springboot 使用redis实现接口幂等性拦截 十一
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (二)Linux——Linux常用指令
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (五)网络优化与超参数选择--九五小庞
  • (转) RFS+AutoItLibrary测试web对话框
  • .bat批处理(一):@echo off
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .NET Core日志内容详解,详解不同日志级别的区别和有关日志记录的实用工具和第三方库详解与示例
  • .net 验证控件和javaScript的冲突问题
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • /boot 内存空间不够