hifiveのUIライブラリ紹介「localCombobox」
コンボボックス(select)はHTML標準で提供されます。選択肢が少ないときには問題ありませんが、数百の中から選択するとなると大変です。名前順に並んでいたとしても、ユーザビリティが高いとは言えないでしょう。
そこで使ってみたいのがlocalComboboxです。インクリメンタルな検索機能付きのコンボボックスです。
デモはJSFiddle上で公開しています。実際の動きは次のようになります。
作り方
HTML
HTMLは次のように inputのtype=”text” で定義します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="text" name="accountcode"> |
また、hifive本体とは別に以下のファイルを読み込みます。
JavaScript
JavaScriptはまず ComboBoxController を読み込みます。rootElementにはHTML側のインプットを指定します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var comboboxController = { | |
__name: 'ComboboxController', | |
/** コンボボックスコントローラ */ | |
_accountComboBoxController: h5.ui.components.combobox.ComboBoxController, | |
__meta: { | |
_accountComboBoxController: { | |
rootElement: 'input[name="accountcode"]' | |
} | |
}, | |
__ready: function() { | |
} | |
}; | |
h5.core.expose(comboboxController); |
__ready内ではコンボボックスコントローラに対してデータを適用します。これはAjaxを使っても問題ありません。デモでは分かりやすいように配列にしています。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = []; | |
for (var i = 0; i < 500; i++) { | |
data.push({ | |
value: ("0000"+i).slice(–5) | |
}) | |
} | |
this._accountComboBoxController.init({ | |
data: data | |
}); |
最後にコントローラをバインドして完了です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
h5.core.controller('body', ComboboxController); | |
}); |
これで文字列検索を使ってインクリメンタルに絞り込めるコンボボックスが使えます。データリソースはAjaxを使ってデータベースから取ってくることもできますので、大量のデータでも扱いやすくなるでしょう。
デモを使って体験してみてください。
コメントは受け付けていません。