Skip to content
Permalink
Browse files

added ability to search on first or last name

  • Loading branch information...
SangeetAgarwal committed Apr 4, 2015
1 parent b01d550 commit 0cd7eb4cbaed3f68c28ffcb9a8b57def0dc2d2d4
@@ -19,42 +19,42 @@ public class StudentsApiController : ApiController
private readonly SchoolContext db = new SchoolContext();

// GET: api/Students
public StudentsContainer GetStudents(int currentPage, int recordsPerPage, string sortKey, string sortOrder)
public StudentsContainer GetStudents(int currentPage, int recordsPerPage, string sortKey, string sortOrder, string searchfor)
{

var pageNumber = currentPage;
var pageSize = recordsPerPage;
var begin = (pageNumber - 1) * pageSize;

var totalNumberOfRecords = db.Students.Count();
var totalNumberOfRecords = db.Students.Count(r => searchfor == "null" || r.LastName.Contains(searchfor) || r.FirstMidName.Contains(searchfor));
List<Student> results = null;
switch (sortOrder)
{
case "ASC":
switch (sortKey)
{
case "lastName":
results = db.Students.OrderBy(r => r.LastName).Skip(begin).Take(pageSize).ToList();
results = db.Students.Where(r => searchfor == "null" || r.LastName.Contains(searchfor) || r.FirstMidName.Contains(searchfor)).OrderBy(r => r.LastName).Skip(begin).Take(pageSize).ToList();
break;
case "firstName":
results = db.Students.OrderBy(r => r.FirstMidName).Skip(begin).Take(pageSize).ToList();
results = db.Students.Where(r => searchfor == "null" || r.LastName.Contains(searchfor) || r.FirstMidName.Contains(searchfor)).OrderBy(r => r.FirstMidName).Skip(begin).Take(pageSize).ToList();
break;
}
break;
case "DESC":
switch (sortKey)
{
case "lastName":
results = db.Students.OrderByDescending(r => r.LastName).Skip(begin).Take(pageSize).ToList();
results = db.Students.Where(r => searchfor == "null" || r.LastName.Contains(searchfor) || r.FirstMidName.Contains(searchfor)).OrderByDescending(r => r.LastName).Skip(begin).Take(pageSize).ToList();
break;
case "firstName":
results = db.Students.OrderByDescending(r => r.FirstMidName).Skip(begin).Take(pageSize).ToList();
results = db.Students.Where(r => searchfor == "null" || r.LastName.Contains(searchfor) || r.FirstMidName.Contains(searchfor)).OrderByDescending(r => r.FirstMidName).Skip(begin).Take(pageSize).ToList();
break;
}
break; ;
break;
}



var students =
results.Select(
@@ -11,13 +11,13 @@

var _students = [];

var deferred = $q.defer();

var _getStudents = function (options) {

var deferred = $q.defer();

$http.get("api/StudentsApi?currentPage=" + options.currentPage + "&" +
"recordsPerPage=" + options.recordsPerPage + "&" +
"sortKey=" + options.sortKeyOrder.key + "&" + "sortOrder=" + options.sortKeyOrder.order)
"sortKey=" + options.sortKeyOrder.key + "&" + "sortOrder=" + options.sortKeyOrder.order + "&searchfor=" + options.searchfor)
.then(function (result) {
angular.copy(result.data.students, _students);
deferred.resolve(result.data.recordCount);
@@ -36,20 +36,23 @@
}])
.controller("studentCtrl", ["$scope", "dataService", "localStorageService",
function ($scope, dataService, localStorageService) {

$scope.data = dataService.students;

var sortKeyOrder = {
key: '',
order: '',
};


if (localStorageService.get("searchfor") !== undefined) {
$scope.searchfor = localStorageService.get("searchfor");
}


$scope.totalItems = 0;
$scope.currentPage = 1;
$scope.maxSize = 5;
$scope.recordsPerPage = 5;
$scope.numberOfPageButtons = 5;


getData($scope, dataService, localStorageService);

@@ -67,12 +70,12 @@
localStorageService.set('sortKeyOrder', sortKeyOrder);

} else {

sortKeyOrder = {
key: col,
order: 'ASC',
};

localStorageService.set('sortKeyOrder', sortKeyOrder);

}
@@ -83,13 +86,24 @@
getData($scope, dataService, localStorageService);
};

$scope.search = function (searchfor) {

if (searchfor === undefined) {
$scope.searchfor = "";
}

localStorageService.set("searchfor", searchfor);

getData($scope, dataService, localStorageService);
}


}]);


var getData = function ($scope, dataService, localStorageService) {


$scope.data = dataService.students;
var sortKeyOrder = localStorageService.get('sortKeyOrder');

if (sortKeyOrder == null) {
@@ -98,20 +112,23 @@ var getData = function ($scope, dataService, localStorageService) {
order: 'ASC',
};
}


var searchfor = localStorageService.get('searchfor');

$scope.sortKeyOrder = sortKeyOrder;

var options = {

currentPage: $scope.currentPage,
recordsPerPage: $scope.recordsPerPage,
sortKeyOrder: sortKeyOrder,
searchfor: searchfor,

};


dataService.getStudents(options)
.then(function (totalItems) {

$scope.totalItems = totalItems;
},
function () {
@@ -1,4 +1,15 @@
<div class="row top-buffer">
<div class="row">
<div class="col-sm-3 col-md-3">
<div class="input-group">
<input type="search" class="form-control" placeholder="Search for..." ng-model="searchfor">
<div class="input-group-btn">
<button class="btn btn-default" type="button" ng-click="search(searchfor)"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</div>
</div>

<div class="row top-buffer">
<table class="table table-bordered table-striped table-responsive">
<thead>
<tr>

0 comments on commit 0cd7eb4

Please sign in to comment.
You can’t perform that action at this time.