Having multiple radio
buttons, I aim to filter results retrieved from a web API based on the selected radio
button.
HTML
<div class="row">
<div class="small-8 medium-9 large-10 columns">
<ul class="no-bullet">
<li data-ng-repeat="course in courses">
<a href="#/CoursesWillStart/{{ course.ID }}">{{ course.CourseName }}</a>
</li>
</ul>
</div>
<div class="small-4 medium-3 large-2 columns">
<ul class="no-bullet">
<li>
<label><input type="radio" name="filterRadio" value="RadioAll" data-ng-model="filterRadio" /> All</label>
</li>
<li>
<label><input type="radio" name="filterRadio" value="RadioToday" data-ng-model="filterRadio" /> Today</label>
</li>
<li>
<label><input type="radio" name="filterRadio" value="RadioThisWeek" data-ng-model="filterRadio" /> This Week</label>
</li>
<li>
<label><input type="radio" name="filterRadio" value="RadioThisMonth" data-ng-model="filterRadio" /> This Month</label>
</li>
<li>
<label><input type="radio" name="filterRadio" value="RadioSpecificDate" data-ng-model="filterRadio" /> Specific Date
<input type="date" name="from" data-ng-model="from" data-ng-show="filterRadio == 'RadioSpecificDate'" />
<input type="date" name="to" data-ng-model="to" data-ng-show="filterRadio == 'RadioSpecificDate'" />
</label>
</li>
<li>
<button class="my-button" data-ng-click="filterCourses(filterRadio)">Search</button>
</li>
</ul>
</div>
</div>
Javascript (relevant)
myApp.controller('CoursesWillStartCtrl', ['$scope', 'GetCoursesWillStart',
function ($scope, GetCoursesWillStart) {
$scope.filterRadio = 'RadioAll';
$scope.filterCourses = function (filterRadio) {
switch (filterRadio) {
case 'RadioToday':
$scope.courses = coursesStartToday();
break;
case 'RadioThisWeek':
$scope.courses = coursesThisWeek();
break;
case 'RadioThisMonth':
$scope.courses = coursesThisMonth();
break;
case 'RadioSpecificDate':
$scope.courses = coursesInSpecificDate($scope.from, $scope.to);
break;
default: //all
$scope.courses = GetCoursesWillStart.query();
break;
}
};
$scope.filterCourses($scope.filterRadio);
}
]);
This marks my debut web application in Angular. While the code above appears functional, I seek to avoid altering $scope.courses
to prevent unnecessary retrieval of all courses post each filtration, and to refrain from overusing the web API.
Considering crafting a custom filter, I came across this tutorial. However, uncertain how to tailor it to meet my filtering needs, may someone elucidate how to create a custom filter or suggest an alternative approach for achieving the desired outcome?