LoginSignup
1
1

More than 3 years have passed since last update.

EasyAutocompleteを5行で日本語入力変換に対応させる

Last updated at Posted at 2020-02-12

オートコンプリート機能は日本語変換に対応していない

EasyAutocomplete:http://easyautocomplete.com/guide#sec-functions

ドキュメント通りに実装してみる。

hoge.js
  var options = {
    data: yakinikuData,
    getValue: "name",
    list: {
      match: {
        enabled: true
      },
    },
  };

  let autocomplete = $("#autoComplete");
  autocomplete.easyAutocomplete(options);

ローマ字だけなら問題ない。動く。

3.gif

日本語変換してしまうと、狙い通りに動かない。

変換後の「焼肉」で検索したいのに、「やきにく」で検索される。

4.gif

解決策

参考:https://github.com/pawelczak/EasyAutocomplete/issues/236
参考:https://developer.mozilla.org/ja/docs/Web/Reference/Events/compositionend
以下を追加する。

hoge.js
  var options = {
    data: gon.gurunavis,
    getValue: "name",
    list: {
      match: {
        enabled: true
      },
    },
  };

  let autocomplete = $("#autoComplete");
  autocomplete.easyAutocomplete(options);

  // 以下を追加
  autocomplete.on('compositionend', function () {
    let evt = jQuery.Event("keyup");
    evt.keyCode = 41;
    autocomplete.trigger(evt);
  });

動いた

5.gif

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1