10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【AngularJS】日付範囲の選択を実現するカスタムフィルタ

Last updated at Posted at 2014-07-30

ググっても上手く見つからず、自分で作ってみました。

// 日付範囲選択用のフィルタ
app.filter('dateSelectFrom', function() {
	return function(input, from) {
		var arr = [];
		input.some(function(v, i) {
			if (v.date >= Date.parse(from)) {
				arr.push(input[i]);
			}
		});
		return arr;
	};
});
app.filter('dateSelectTo', function() {
	return function(input, to) {
		var arr = [];
		input.some(function(v, i) {
			if (v.date <= Date.parse(to)) {
				arr.push(input[i]);
			}
		});
		return arr;
	};
});

こんな感じでフィルタを適用できます。

<tr ng-repeat="item in items|dateSelectFrom:dateFrom|dateSelectTo:dateTo>

ユーザに日付を選択させる場合は、HTML5の

<input type="date" ng-model="dateFrom">

を使うと楽。

動くサンプルはこちらから。

【AngularJS】日付範囲の選択を実現するカスタムフィルタ - jsdo.it

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?