InputManJSのドロップダウンプラグインを使用して、TreeViewコントロールをドロップダウン表示させる方法について解説します。
ドロップダウンオブジェクトの追加
ドロップダウンプラグインを使用する場合、まずcreateDropDownメソッドを使用して、ドロップダウンオブジェクトをInputManJSのコントロールに追加する必要があります。
テキストコントロールにドロップダウンオブジェクトを追加する場合、以下のようなコードになります。
なお、createDropDownメソッドでは、ドロップダウンボタンを配置する位置をパラメータとして、指定することが可能です。設定可能な値(DropDownButtonAlignment列挙体)は、以下の通りです。
値
説明
LeftSide
ドロップダウンボタンをコントロールの左側に配置します。
RightSide
ドロップダウンボタンをコントロールの右側に配置します。(既定値)
TreeViewの表示
TreeViewをドロップダウン表示したい場合、TreeViewをドロップダウンオブジェクトの子ノードとして追加します。
たとえば、以下のようなコードになります。
選択時の処理
選択時の処理は、TreeView側のイベントを使用して行うことになります。具体的には、アイテムが選択されたことを検知(selectedItemChangedイベント)し、テキストコントロールに選択された値を表示した上でcloseメソッドでドロップダウンを閉じるような処理を実装する必要があります。
たとえば、上記サンプルの場合、以下のコードがTreeView側で行が選択された時の処理になります。
import '@mescius/inputman/CSS/gc.inputman-js.css';
import '@mescius/wijmo.styles/wijmo.css';
import './styles.css';
import { InputMan } from '@mescius/inputman';
import { TreeView } from '@mescius/wijmo.nav';
import prefectures from './data';
InputMan.appearanceStyle = InputMan.AppearanceStyle.Modern;
// テキストコントロール
const gcTextBox = new InputMan.GcTextBox(document.getElementById('gcTextBox'));
// ドロップダウンプラグイン
const gcDropDown = gcTextBox.createDropDown();
// Wijmo TreeViewコントロール
const treeView = new TreeView(gcDropDown.getElement().appendChild(document.createElement('div')), {
itemsSource: prefectures,
displayMemberPath: 'name',
isAnimated: false,
selectedItemChanged: (s, e) => {
if (!s.selectedNode.hasChildren) {
gcTextBox.setText(s.selectedItem.name);
gcDropDown.close();
}
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ドロップダウンプラグイン - Wijmo TreeViewの表示</title>
<!-- SystemJS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.21.5/system.src.js" integrity="sha512-skZbMyvYdNoZfLmiGn5ii6KmklM82rYX2uWctBhzaXPxJgiv4XBwJnFGr5k8s+6tE1pcR1nuTKghozJHyzMcoA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="systemjs.config.js"></script>
<script>
if (!window.GCIM) {
window.GCIM = {};
}
window.GCIM.LicenseKey = "GrapeCity-Internal-Use-Only,935368846199268#B1135ayQUW44kQXhnViFHV7YmNX3iQjdnMk9EOIlnb0xWbxoVO6hlTxNzLQRTemZHWldEd8YFe59kVItCchlDaPVXc7YVUw2SV9Z6M0NTN03kaww4LVFWeTpndGZlaNh4TqpXQt94L6dURzxkNmNWQvITQrgjZwE6K9UzaWVlcRJEV7FUQEdzTPB5aChzZ5F7MxEWRrEVWwMlbTp6RQlmNaNEZIlVe8AnV9pHcIx6TmJkMjdkMGtkRFR5cuFEZotibRplMtJVbCB7VKp5QGJmNS56Mwd4LUNDSvRUNuJzbxcmI0IyUiwiIFFkR4UjM7UjI0ICSiwCO4kTOzATNxQTM0IicfJye#4Xfd5nIGh5SEJiOiMkIsISNWByUKBibh5Ed5BnbJJiOi8kI1tlOiQmcQJCLiUjM8IzNwACOwQDM5IDMyIiOiQncDJCLiAnauMXdpN6cl5mLqIiOiMXbEJCLiMbul1qukDClSiOt6muI0ISYONkIsICO6ITO9EjN4gDO6MTNzkjI0ICZJJye0ICRiwiI34zZ9FVRvhmM7Zka9IFUnZWMOJkSIV4S9UlYrlWezZXcrc4cLpWc9h6M5V6csFlVrp7dBRDZ9tGV79ET4FWeYRFTuFFeXd7KwkES9pldM3yLNhHT5RzUqNVTxQWWrUWQYNESLpUSwkFR5VFcrAlVKVkQqJFZMtkc";
</script>
<script>
window.onload = function() {
System.import('./src/app');
}
</script>
</head>
<body>
都道府県<br>
<input id="gcTextBox">
</body>
</html>
const items = [
{
name: '北海道', items: [
{ name: '北海道' },
]
},
{
name: '東北', items: [
{ name: '青森県' },
{ name: '岩手県' },
{ name: '宮城県' },
{ name: '秋田県' },
{ name: '山形県' },
{ name: '福島県' },
]
},
{
name: '関東', items: [
{ name: '茨城県' },
{ name: '栃木県' },
{ name: '群馬県' },
{ name: '埼玉県' },
{ name: '千葉県' },
{ name: '東京都' },
{ name: '神奈川県' },
]
},
{
name: '甲信越', items: [
{ name: '山梨県' },
{ name: '長野県' },
{ name: '新潟県' },
]
},
{
name: '北陸', items: [
{ name: '富山県' },
{ name: '石川県' },
{ name: '福井県' },
]
},
{
name: '東海', items: [
{ name: '岐阜県' },
{ name: '静岡県' },
{ name: '愛知県' },
{ name: '三重県' },
]
},
{
name: '近畿', items: [
{ name: '滋賀県' },
{ name: '京都府' },
{ name: '大阪府' },
{ name: '兵庫県' },
{ name: '奈良県' },
{ name: '和歌山県' },
]
},
{
name: '中国四国', items: [
{ name: '鳥取県' },
{ name: '島根県' },
{ name: '岡山県' },
{ name: '広島県' },
{ name: '山口県' },
{ name: '徳島県' },
{ name: '香川県' },
{ name: '愛媛県' },
{ name: '高知県' },
]
},
{
name: '九州', items: [
{ name: '福岡県' },
{ name: '佐賀県' },
{ name: '長崎県' },
{ name: '熊本県' },
{ name: '大分県' },
{ name: '宮崎県' },
{ name: '鹿児島県' },
]
},
{
name: '沖縄', items: [
{ name: '沖縄県' },
]
},
];
export default items;
body {
padding-bottom: 17rem;
}
.wj-treeview {
height: 14rem;
width: 220px !important;
border: 1px solid rgba(0,0,0,0.2) !important;
}
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
meta: {
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'@mescius/inputman': 'npm:@mescius/inputman/index.js',
'@mescius/inputman/CSS': 'npm:@mescius/inputman/CSS',
'@mescius/inputman.richtexteditor': 'npm:@mescius/inputman.richtexteditor/index.js',
"@mescius/inputman.richtexteditor/CSS": "npm:@mescius/inputman.richtexteditor/CSS",
'@mescius/inputman.richtexteditor/JS/plugins/advlist': 'npm:@mescius/inputman.richtexteditor/JS/plugins/advlist/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/all': 'npm:@mescius/inputman.richtexteditor/JS/plugins/all/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/autosave': 'npm:@mescius/inputman.richtexteditor/JS/plugins/autosave/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/charmap': 'npm:@mescius/inputman.richtexteditor/JS/plugins/charmap/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/directionality': 'npm:@mescius/inputman.richtexteditor/JS/plugins/directionality/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/emoticons': 'npm:@mescius/inputman.richtexteditor/JS/plugins/emoticons/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/fullscreen': 'npm:@mescius/inputman.richtexteditor/JS/plugins/fullscreen/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/htmlcode': 'npm:@mescius/inputman.richtexteditor/JS/plugins/htmlcode/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/image': 'npm:@mescius/inputman.richtexteditor/JS/plugins/image/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/link': 'npm:@mescius/inputman.richtexteditor/JS/plugins/link/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/lists': 'npm:@mescius/inputman.richtexteditor/JS/plugins/lists/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/media': 'npm:@mescius/inputman.richtexteditor/JS/plugins/media/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/pagebreak': 'npm:@mescius/inputman.richtexteditor/JS/plugins/pagebreak/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/preview': 'npm:@mescius/inputman.richtexteditor/JS/plugins/preview/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/save': 'npm:@mescius/inputman.richtexteditor/JS/plugins/save/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/searchreplace': 'npm:@mescius/inputman.richtexteditor/JS/plugins/searchreplace/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/table': 'npm:@mescius/inputman.richtexteditor/JS/plugins/table/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/template': 'npm:@mescius/inputman.richtexteditor/JS/plugins/template/plugin.js',
'@mescius/inputman.richtexteditor/JS/plugins/wordcount': 'npm:@mescius/inputman.richtexteditor/JS/plugins/wordcount/plugin.js',
'@mescius/inputman.comment': 'npm:@mescius/inputman.comment/index.js',
'@mescius/inputman.comment/CSS': 'npm:@mescius/inputman.comment/CSS',
'@mescius/wijmo': 'npm:@mescius/wijmo/index.js',
'@mescius/wijmo.styles': 'npm:@mescius/wijmo.styles',
'@mescius/wijmo.cultures': 'npm:@mescius/wijmo.cultures',
'@mescius/wijmo.input': 'npm:@mescius/wijmo.input/index.js',
'@mescius/wijmo.grid': 'npm:@mescius/wijmo.grid/index.js',
'@mescius/wijmo.nav': 'npm:@mescius/wijmo.nav/index.js',
'css': 'npm:systemjs-plugin-css/css.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
'systemjs-babel-build': 'npm:systemjs-plugin-babel/systemjs-babel-browser.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
},
}
});