ドロップダウンプラグイン(GcDropDown)を使用して、HTML要素をドロップダウン表示させる方法について解説します。
ドロップダウンオブジェクトの追加
ドロップダウンプラグインを使用する場合、まずcreateDropDownメソッドを使用して、ドロップダウンオブジェクトをInputManJSのコントロールに追加する必要があります。
テキストコントロールにドロップダウンオブジェクトを追加する場合、以下のようなコードになります。
なお、createDropDownメソッドでは、ドロップダウンボタンを配置する位置をパラメータとして指定することが可能です。設定可能な値(DropDownButtonAlignment列挙体)は、以下の通りです。
値
説明
LeftSide
ドロップダウンボタンをコントロールの左側に配置します。
RightSide
ドロップダウンボタンをコントロールの右側に配置します。(既定値)
HTML要素の表示
HTML要素をドロップダウン表示したい場合、ドロップダウンオブジェクトのgetElementメソッドを使用して、ドロップダウンのコンテナになるHTML要素を取得し、appendChildメソッドを使用して、そのHTML要素に表示したいHTML要素を追加します。
上記サンプルでは、"prefecture"というidのHTML要素をドロップダウンのコンテナに追加しています。
選択時の処理
上記のサンプルでは、"prefecture"要素内のspanタグがクリックされた時に、テキストコントロールにそのspanタグ内の文字(innerText)を表示し、closeメソッドでドロップダウンを閉じています。clickイベントを設定する箇所は、以下のように記述することも可能です。
import { bootstrapApplication } from '@angular/platform-browser';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { InputManModule } from "@mescius/inputman.angular";
import './styles.css';
import '@mescius/inputman/CSS/gc.inputman-js.css';
import { Component, enableProdMode } from '@angular/core';
import * as GC from "@mescius/inputman";
@Component({
selector: 'app-component',
templateUrl: './src/app.component.html',
imports: [InputManModule, BrowserModule, FormsModule],
standalone: true
})
export class AppComponent {
ngOnInit(): void {
GC.InputMan.appearanceStyle = GC.InputMan.AppearanceStyle.Modern;
}
public initTextBoxDropDown(gcTextBox: GC.InputMan.GcTextBox): void {
const gcDropDown = gcTextBox.createDropDown();
const dropdownContent = document.getElementById('prefecture');
document.getElementById('prefecture').addEventListener('click', (e) => {
if ((e.srcElement as HTMLElement).tagName.toLowerCase() == 'span') {
gcTextBox.setText((e.srcElement as HTMLElement).innerText);
gcDropDown.close();
}
});
gcDropDown.getElement().appendChild(dropdownContent);
}
}
enableProdMode();
bootstrapApplication(AppComponent).catch(err => console.error(err));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>ドロップダウンプラグイン - HTML要素の表示</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Polyfills -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/fesm2015/zone.min.js"></script>
<!-- SystemJS -->
<script src="node_modules/systemjs/dist/system.js"></script>
<script src="systemjs.config.js"></script>
<script>
// workaround to load 'rxjs/operators' from the rxjs bundle
window.onload = function() {
System.import('rxjs').then(function (m) {
System.import('@angular/compiler');
System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators));
System.import('./src/app.component');
});
}
</script>
</head>
<body>
<app-component></app-component>
</body>
</html>
都道府県<br>
<gc-text-box (onInitialized)="initTextBoxDropDown($event)"></gc-text-box>
<div id="prefecture">
<table>
<tr>
<th>北海道</th>
<td><span>北海道</span></td>
</tr>
<tr>
<th>東北</th>
<td><span>青森県</span><span>岩手県</span><span>宮城県</span><span>秋田県</span><span>山形県</span><span>福島県</span>
</td>
</tr>
<tr>
<th>関東</th>
<td><span>茨城県</span><span>栃木県</span><span>群馬県</span><span>埼玉県</span><span>千葉県</span><span>東京都</span><span>神奈川県</span>
</td>
</tr>
<tr>
<th>甲信越</th>
<td><span>山梨県</span><span>長野県</span><span>新潟県</span></td>
</tr>
<tr>
<th>北陸</th>
<td><span>富山県</span><span>石川県</span><span>福井県</span></td>
</tr>
<tr>
<th>東海</th>
<td><span>岐阜県</span><span>静岡県</span><span>愛知県</span><span>三重県</span></td>
</tr>
<tr>
<th>近畿</th>
<td><span>滋賀県</span><span>京都府</span><span>大阪府</span><span>兵庫県</span><span>奈良県</span><span>和歌山県</span>
</td>
</tr>
<tr>
<th>中国四国</th>
<td><span>鳥取県</span><span>島根県</span><span>岡山県</span><span>広島県</span><span>山口県</span><span>徳島県</span><span>香川県</span><span>愛媛県</span><span>高知県</span>
</td>
</tr>
<tr>
<th>九州</th>
<td><span>福岡県</span><span>佐賀県</span><span>長崎県</span><span>熊本県</span><span>大分県</span><span>宮崎県</span><span>鹿児島県</span>
</td>
</tr>
<tr>
<th>沖縄</th>
<td><span>沖縄県</span></td>
</tr>
</table>
</div>
body {
padding-bottom: 16rem;
}
#prefecture table {
background-color: azure;
border: darkgray 1px solid;
border-radius: 5px;
}
#prefecture th {
padding: 0 0.5rem;
border-right: steelblue 2px solid;
font-weight: bold;
}
#prefecture span {
margin: 0 0.5rem;
color: blue;
cursor : pointer;
}
body {
padding-bottom: 16rem;
}
[gcim-control-appearance="modern"] #prefecture table {
background-color: azure;
border: darkgray 1px solid;
border-radius: 5px;
}
[gcim-control-appearance="modern"] #prefecture th {
padding: 0 0.5rem;
border-right: steelblue 2px solid;
font-weight: bold;
}
[gcim-control-appearance="modern"] #prefecture span {
margin: 0 0.5rem;
color: blue;
cursor: pointer;
}
(function (global) {
System.config({
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
},
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'core-js': 'npm:core-js/client/shim.min.js',
'zone': 'npm:zone.js/fesm2015/zone.min.js',
//'reflect-metadata': 'npm:reflect-metadata/Reflect.js',
'rxjs': 'npm:rxjs/dist/bundles/rxjs.umd.min.js',
//'rxjs': 'npm:rxjs',
//'rxjs/operators': 'npm:rxjs/operators',
'@angular/core': 'npm:@angular/core/fesm2022',
'@angular/common': 'npm:@angular/common/fesm2022/common.mjs',
'@angular/compiler': 'npm:@angular/compiler/fesm2022/compiler.mjs',
'@angular/platform-browser': 'npm:@angular/platform-browser/fesm2022/platform-browser.mjs',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/fesm2022/platform-browser-dynamic.mjs',
'@angular/common/http': 'npm:@angular/common/fesm2022/http.mjs',
'@angular/router': 'npm:@angular/router/fesm2022/router.mjs',
'@angular/forms': 'npm:@angular/forms/fesm2022/forms.mjs',
'@mescius/inputman': 'npm:@mescius/inputman/index.js',
'@mescius/inputman.angular': 'npm:@mescius/inputman.angular/fesm2015/mescius-inputman.angular.mjs',
'@mescius/inputman.richtexteditor': 'npm:@mescius/inputman.richtexteditor/index.js',
'@mescius/inputman.richtexteditor.angular': 'npm:@mescius/inputman.richtexteditor.angular/fesm2015/mescius-inputman.richtexteditor.angular.mjs',
'@mescius/inputman.comment': 'npm:@mescius/inputman.comment/index.js',
'@mescius/inputman.comment.angular': 'npm:@mescius/inputman.comment.angular/fesm2015/mescius-inputman.comment.angular.mjs',
'@mescius/inputman/CSS': 'npm:@mescius/inputman/CSS',
"@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/CSS': 'npm:@mescius/inputman.comment/CSS',
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js',
'@mescius/spread-sheets/styles': 'npm:@mescius/spread-sheets/styles',
'@mescius/wijmo': 'npm:@mescius/wijmo/index.js',
'@mescius/wijmo.styles': 'npm:@mescius/wijmo.styles',
'@mescius/wijmo.cultures': 'npm:@mescius/wijmo.cultures',
'@mescius/wijmo.nav': 'npm:@mescius/wijmo.nav/index.js',
'@mescius/wijmo.grid': 'npm:@mescius/wijmo.grid/index.js',
'@mescius/wijmo.input': 'npm:@mescius/wijmo.input/index.js',
'bootstrap.css': 'npm:bootstrap/dist/css/bootstrap.min.css',
'jszip': 'npm:jszip/dist/jszip.min.js',
'typescript': 'npm:typescript/lib/typescript.js',
'ts': './plugin.js',
'tslib': 'npm:tslib/tslib.js',
'css': 'npm:systemjs-plugin-css/css.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'ts',
meta: {
"*.component.ts": {
loader: "system.component-loader.js"
}
}
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
},
"@mescius/inputman": {
defaultExtension: 'js'
},
"@mescius/inputman.richtexteditor": {
defaultExtension: 'js'
},
"@mescius/inputman.comment": {
defaultExtension: 'js'
},
"node_modules/@angular": {
defaultExtension: 'mjs'
},
"@mescius/inputman.angular": {
defaultExtension: 'mjs'
},
"@mescius/inputman.comment.angular": {
defaultExtension: 'mjs'
},
'@angular/core': {
defaultExtension: 'mjs',
main: 'core.mjs'
},
"@mescius/inputman.richtexteditor.angular": {
defaultExtension: 'mjs'
},
}
});
})(this);