Recap
In my earlier article on this series, we went through the Angular Material card based interface, and how we can use various types of cards and their layouts. If you have not, then please visit my article at,
- Angular Material: Card Based Interface - Part One
How can we use Angular Material Cards for displaying search results?
Let’s consider, I would like to search books based on some keyword and upon searching, all the books are displayed in a card based interface. Now, to implement this, the following approach can be considered:
Use Case – Searching Books based on keyword and displaying them in card interface.
- User enters keyword for a book search.
- User is displayedthe top five books in a card based layout.
Before I get into the details, here is how card interface for the top five books will be shown:
Searching books data using REST API
For my use case, I will be using the following REST API.
https://www.googleapis.com/books/v1/volumes?q=SharePoint&maxResults=5
Building Card Interface for our Book Search
For our Book Search Input criteria, I will be using input directives and button provided by Angular Material. And for search results, I will be using large cards.
Now, let's follow the below steps for our interface which will display our information in card format,
- Create an index.html by using the following code.
- <!DOCTYPE html>
- <html lang="en">
-
- <head>
- <title>Angular Material Card</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel='stylesheet' href='angular-material.min.css'>
- <style>
- .book-img {
- height: 170px;
- width: 130px;
- }
- </style>
- </head>
-
- <body>
- <div ng-controller="appController" ng-cloak="" ng-app="MyApp">
- <div layout="column" ng-cloak class="lg-inline-form">
- <md-content class="md-padding" layout-lg layout="row">
- <md-input-container class="md-block md-input-has-value"> <label>Enter Keywords</label> <input ng-model="keyword" md-maxlength="30" required> </md-input-container>
- <md-input-container>
- <md-button class="md-raised md-primary" ng-click="searchBooks()">Search Top 5 Books</md-button>
- </md-input-container>
- </md-content>
- <md-content class="md-padding" layout-xs="column" layout="row">
- <div flex-xs="" flex-gt-xs="50" layout="column">
- <div ng-repeat="book in books">
- <md-card>
- <md-card-title>
- <md-card-title-text> <span class="md-headline">{{book.volumeInfo.title}}</span> <span class="md-subhead">{{book.volumeInfo.description|limitTo:200}}...</span> </md-card-title-text>
- <md-card-title-media>
- <div class="book-img"> <img data-ng-src="{{book.volumeInfo.imageLinks.thumbnail}}" /> </div>
- </md-card-title-media>
- </md-card-title>
- <md-card-actions layout="row" layout-align="end center">
- <md-button class="md-raised md-button">Shortlist</md-button>
- <md-button class="md-raised md-primary">Add To Cart</md-button>
- </md-card-actions>
- </md-card>
- </div>
- </div>
- </md-content>
- </div>
- </div>
- <script src='angular.min.js'></script>
- <script src='angular-animate.min.js'></script>
- <script src='angular-route.min.js'></script>
- <script src='angular-aria.min.js'></script>
- <script src='angular-messages.min.js'></script>
- <script src='angular-material.min.js'></script>
- <script src='services.js'></script>
- <script src='app.js'></script>
- </body>
-
- </html>
The above code can be explained here.
Input directives and their UI rendering can be seen in the following image.
Card directives and their UI rendering can be seen here.
- Create a new app.js and copy the following code to it.
- var myapp = angular.module('MyApp', ['ngMaterial', 'bookServices']);
- myapp.controller('appController', function($scope, BookService)
- {
- $scope.searchBooks = function()
- {
- if (($scope.keyword.length) > 0) {
- BookService.getBooks($scope.keyword).then(function(response) {
- $scope.books = response.data.items;
- });
- }
- }
- })
- Create services.js and write this code in it.
- var bookServices = angular.module('bookServices', []);
- bookServices.factory('BookService', function($http)
- {
- var bookapi = {};
- bookapi.getBooks = function(keyword) {
- return $http({
- method: 'get',
- url: 'https://www.googleapis.com/books/v1/volumes?q=' + keyword + '&maxResults=5'
- }).success(function(data) {
- bookapi.books = data;
- });
- }
- return bookapi;
- });
- Open index.html in browser and see how our Angular Material based interface looks.
- Now, let’s enter a keyword ‘SharePoint’ and click on button ‘SEARCH TOP 5 BOOKS’. This will execute search API and will present the results in card interface, listing top five books based on the search criteria or keyword entered by user.
Now, let’s resize the window and see how book search results become responsive. I am using Developer Tools for testing the responsiveness of the search results page.
This way, we can have any search results presented. If you are searching for blogs or books or any other content, such elegant interfaces can be used to present search results.