カレンダーコンポーネントで特定の日付にラベルを追加する
hifiveのUIライブラリの一つ、カレンダーコンポーネントのTipsを紹介します。今回は特定の日付に対してラベルをつける方法です。
動作している様子は次のようになります。また、こちらでデモを確認できます。
作り方
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
<div id="container"></div> | |
<p> | |
<input type="text" id="txtContentDate" size="40" /> | |
<input type="button" id="btnSetTextDate" value="設定"> | |
</p> |
JavaScriptの実装
JavaScript側の実装です。this.calendarController.setTooltip を使って、日付と表示するラベルを指定します。
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
'#btnSetTextDate click': function(){ | |
var dates = this.calendarController.getSelectedDate(); | |
var data = this.$find('#txtContentDate').val(); | |
for (var i=0; i<dates.length; i++) { | |
this.calendarController.setTooltip(dates[i], data); | |
} | |
} |
全体の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 mainController = { | |
__name : 'h5.ui.container.MainController', | |
calendarController: h5.ui.components.Calendar, | |
__meta : { | |
calendarController: { | |
rootElement: '#container' | |
}, | |
}, | |
__init: function(){ | |
this.$find(".select:first").attr("checked", true); | |
}, | |
'#btnSetTextDate click': function(){ | |
var dates = this.calendarController.getSelectedDate(); | |
var data = this.$find('#txtContentDate').val(); | |
for (var i=0; i<dates.length; i++) { | |
this.calendarController.setTooltip(dates[i], data); | |
} | |
} | |
}; | |
h5.core.controller('body', mainController); | |
}); |
覚えておくべき日付を説明とともに表示したり、データベースから日付データをカレンダーに載せるような使い方が考えられます。カレンダーは業務システムでもよく使われますのでhifiveのカレンダーコンポーネントを使いこなしてください。
コメントは受け付けていません。