コンボボックスの有効/無効を切り替える
入力のサジェストができるコンボボックスコントローラで、入力の有効/無効を切り替える方法です。
実際のデモはJSFiddle上で試せます。
作り方
HTMLの実装
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
<p> | |
Combobox: <input type="text" name="accountcode"> | |
</p> | |
<p> | |
<div> | |
<input type="button" name="disable" value="disable" /> | |
<input type="button" name="enable" value="enable" /> | |
</div> | |
</p> |
JavaScriptの実装
JavaScript側では、this._accountComboBoxController の enable/disableを実行して有効/無効を切り替えます。
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[name="disable"] click': function() { | |
this._accountComboBoxController.disable(); | |
}, | |
'input[name="enable"] click': function() { | |
this._accountComboBoxController.enable(); | |
}, |
JavaScript全体のコードは次のようになりなす。
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($) { | |
var comboboxController = { | |
__name: 'ComboboxController', | |
/** コンボボックスコントローラ */ | |
_accountComboBoxController: h5.ui.components.combobox.ComboBoxController, | |
__meta: { | |
_accountComboBoxController: { | |
rootElement: 'input[name="accountcode"]' | |
} | |
}, | |
__ready: function() { | |
var data = []; | |
for (var i = 0; i < 500; i++) { | |
data.push({ | |
value: ("0000"+i).slice(–5) | |
}) | |
} | |
this._accountComboBoxController.init({ | |
data: data | |
}); | |
}, | |
'input[name="disable"] click': function() { | |
this._accountComboBoxController.disable(); | |
}, | |
'input[name="enable"] click': function() { | |
this._accountComboBoxController.enable(); | |
}, | |
}; | |
h5.core.expose(comboboxController); | |
})(jQuery); | |
$(function() { | |
h5.core.controller('body', ComboboxController); | |
}); |
今回のデモはこちらにあります。入力を制限する際に使ってください。
コメントは受け付けていません。