返回介绍

基本使用

发布于 2019-05-19 12:43:40 字数 5698 浏览 1391 评论 0 收藏 0

Basic usage

$('.atwho-inputor').atwho({
  at: "@",
  data: ["one", "two", "three"],
}).atwho({
  at: ":",
  data: ["+1", "-1", "smile"]
});

Settings

Here are the default settings:

$('.atwho-inputor').atwho({
  // key char for observing such as `@`
  at: void 0,
  /*
    alias name of `at`
    it would be an id attribute of the popup view.
  */
  alias: void 0,
  /*
     should be a plain object *Array* or a *URL*
     would save *Array* directly.
     would load and save remote JSON data by *URL*
   */
  data: null,
  /*
     Would render the passed value at the top of the selector popup.
  */
  headerTpl: "<h3>Select a user</h3>",
  /*
     would eval it and assign value of the key contained in `${}`
     key-value ( {'name': "one"} ) is an item in `data` Array.
     Alternatively, this can be a function accepting one data item as a parameter.
  */
  displayTpl: "<li>${name}</li>",
  /*
     It will be evaluated and inserted in the inputor.
     `atwho-at` is the `at` for runtime by default.
     You change it into anything you want.
  */
  insertTpl: "${atwho-at}${name}",
  /*
    There are several data processors that can be overriden here such as `filter`.
    we will cover it later.
  */
  callbacks: DEFAULT_CALLBACKS,
  /*
    would matching item by test against the value of this `search_key` with query string.
  */
  searchKey: "name",
  /*
    limit number of items to show in popup list.
  */
  limit: 5,
  /*
    setting the max length of the string after `at` that would be matched
    It will stop matching if the query string is longer than `max_len`.
  */
  maxLen: 20,
  /*
    if `yes`, At.js will match the query with a spaaace before the `at`.
  */
  startWithSpace: true,
  // time in ms to persist the popup menu for after blurring from the invoking text field
  displayTimeout: 300,
  // highlight_first suggestion in popup menu
  highlightFirst: true,
  // delay time trigger At.js while typing. For example: delay: 400
  delay: null,
  // suffix for inserting string.
  suffix: undefined,
  // don't show dropdown view without `suffix`
  hideWithoutSuffix: false,
  // Add attribute to the inserted element for contenteditable mode
  // check out the issue: https://github.com/ichord/At.js/issues/253#issuecomment-75694945
  editableAtwhoQueryAttrs: {}
});

Examples

Custom Template

You can customize what will be shown in popup's item , such as user’s avatar.

A valid template should meet the following requirements:

  • It should be a li HTML element.
<li>anything here</li>

You can put any HTML element into the template such as img. Its just a HTML element.

var emojis = ["smile", "iphone", "girl", "smiley", "heart", "kiss", "copyright", "coffee"];
var emojisList = $.map(emojis, function(value, i) {
  return {'id':i, 'name':value};
});
//http://a248.e.akamai.net/assets.github.com/images/icons/emoji/8.png
$(".container textarea").atwho({
  at: ':',
  displayTpl: "<li><img src='http://a248.e.akamai.net/assets.github.com/images/icons/emoji/${name}.png' height='20' width='20'/> ${name} </li>",
  insertTpl: ":${name}:",
  data: emojisList
});

Register multiple at keys

At.js can listen to multiple at and every Keyword can have its own settings.

var emojis = ["smile", "iphone", "girl", "smiley", "heart", "kiss", "copyright", "coffee"];

var names = ["Jacob", "Isabella", "Ethan", "Emma", "Michael", "Olivia", "Alexander", "Sophia", "William", "Ava", "Joshua", "Emily", "Daniel", "Madison", "Jayden", "Abigail", "Noah", "Chloe", "你好", "你你你"];

var emojisList = $.map(emojis, function(value, i) {
  return {'id':i, 'name':value};
});

var issues = [
  { name: "1", content: "stay foolish"},
  { name: "2", content: "stay hungry"},
  { name: "3", content: "stay heathly"},
  { name: "4", content: "this happiess"},
];

//http://a248.e.akamai.net/assets.github.com/images/icons/emoji/8.png
$(".container textarea")
  .atwho({
  at: "@",
  data: names
  })
  .atwho({
  at: "#", 
  displayTpl: '<li>${name} <small>${content}</small></li>',
  data: issues
  })
  .atwho({
  at: ":", 
  displayTpl: "<li><img src='http://a248.e.akamai.net/assets.github.com/images/icons/emoji/${name}.png' height='20' width='20'/> ${name} </li>",
  insertTpl: ':${name}:',
  data: emojisList
  });

Load remote data

When you set data as URL string, At.js will launch Ajax request to the server to get the JSON data

   $('textarea').atwho({
    at: "@", 
    data: "http://www.atjs.com/users.json",
    limit: 7
  });

Custom Query matcher

If you want to support unicode characters, you can customize the Query matcher.

The matcher callback look like this:
BTW: You should check Callbacks settings for more details

$('#inputor').atwho({
    at: "@", 
    callbacks: {
      matcher: function(flag, subtext) {
        var match, matched, regexp;
        regexp = new XRegExp('(\\s+|^)' + flag + '(\\p{L}+)

You can read issue #30 for more details.

当搜索条件同时包含中文和英文字符时,无法正确搜索到,filter获取到的query也不正确

, 'gi'); match = regexp.exec(subtext); // ... get matched result return matched; } //, ... others callbacks } });

You can read issue #30 for more details.

当搜索条件同时包含中文和英文字符时,无法正确搜索到,filter获取到的query也不正确

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文