Wednesday, July 20, 2016

AngularJS And ASP.NET MVC Movie Library Application - Part Three- EventBinding with UI Elements

I hope you have had a chance to look into my last tutorial. Here, I will talk about the Event binding with UI elements of Movie.html page within module and controller.

And this will the outcome after binding all control with controller’s function as depicted below:


Kindly find links here as shown below:

Moving ahead in this article, we’ll focus on how to bind UI controls events and link with the defined controller.
$scope plays a vital role whenever you bind the controller value with the specific view. Here, we are going to learn it. After using this, I believe that it would be a kind of ice-breaker and will ease all the further learnings. Let's get our hands dirty with AngularJS & ASP.NET MVC Movie Library Application Tutorials part three.
So far, we’ve designed the screen shot, given below:
screen shot
Hope, you will have also reached up to this level. Moving ahead, let's bind the fancy bootstrap glyphicon icon into an action.
Add the ng-click event, one by one, on each respective control.There are thee HTML button controls that exist. 
First – Add button with + symol
Second – Edit button with pencil symbol
Third - Cross with * Symbol.
Kindly paste the code segment, given below, into the movie.html page.
Code segment Movie.html

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <h2>Movie Rating Portal</h2>
  5. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  6. <style>
  7.         .selected 
  8.         { 
  9.             background-color: lightyellow; 
  10.             color: red; 
  11.             font-weight: bold; 
  12.         } 
  13. </style>
  14. </head>
  15. <body ng-app="routingApp" class="jumbotron">
  16. <div ng-controller="MovieController" class="container">
  17. <div ng-show="success" class="alert-success">Record has been upddated for movie :  {{updatedMovie.title}}</div>
  18. <div ng-show="clear" class="alert-success">Record has been deleted for movie :  {{updatedMovie.title}}</div>
  19. <div class="row col-md-8">
  20. <table class="table table-striped ">
  21. <thead class="thead-inverse">
  22. <th style="background-color: Highlight">Title</th>
  23. <th style="background-color: Highlight">Year of Release</th>
  24. <th style="background-color: Highlight">Rating</th>
  25. <th style="background-color: Highlight">Plot</th>
  26. <th style="background-color: Highlight">Actions</th>
  27. </thead>
  28. <tr>
  29. <!-- <td>EmployeeID:</td>-->
  30. <td>
  31. <input type="text" ng-model="movie.title" class="form-control" />
  32. </td>
  33. <td>
  34. <input type="text" ng-model="movie.year" class="form-control" />
  35. </td>
  36. <td>
  37. <input type="text" ng-model="movie.rating" class="form-control" />
  38. </td>
  39. <td>
  40. <input type="text" ng-model="movie.plot" class="form-control" />
  41. </td>
  42. <td>
  43. <button type="button" ng-click="AddMovie(movie)" class="btn btn-primary">
  44. <span class="glyphicon glyphicon-plus"></span>
  45. </button>
  46. </td>
  47. </tr>
  48. <tbody>
  49. <tr ng-repeat="movie in movies" ng-class="{'selected':$index == selectedRow}">
  50. <td>{{movie.title}} 
  51. </td>
  52. <td>{{movie.year}} 
  53. </td>
  54. <td>{{movie.rating}} 
  55. </td>
  56. <td>{{movie.plot }} 
  57. </td>
  58. <td>
  59. <button type="button" ng-click="EditMovie(movie,$index)" class="btn btn-warning">
  60. <span class="glyphicon glyphicon-pencil"></span>
  61. </button>
  62. <button type="button" ng-click="DeleteMovie(movie,$index)" class="btn btn-danger">
  63. <span class="glyphicon glyphicon-remove"></span>
  64. </button>
  65. </td>
  66. </tr>
  67. </tbody>
  68. </table>
  69. </div>
  70. </div>
  71. </body>
  72. </html>

 

code
Now, we are almost done with the UI part and have created the required ng-click events on all the buttons existing on the page.
Now,  it'stime to design the controller and write the code so that  every button should work very effectively. Thus, please copy and paste the code, given below and replace it within routing.js file.

  1. /// <reference path="../Scripts/angular.js" />
  2. var routingApp = angular.module('routingApp', []); 
  3. routingApp.controller("MovieController", ['$scope', function ($scope) { 
  4.     $scope.edit = false; 
  5.     $scope.message = "Welcome to DotnetPiper.com to learn Angular"; 
  6.     $scope.error = false; 
  7.     $scope.clear = false; 
  8.     $scope.success = false; 
  9. // alert("Servcie Called");
  10. var movies = [ 
  11.                 { title: "Revenent", year: "2015", rating: "5Star", plot: " A revenger Journey" }, 
  12.                  { title: "Counjouring2", year: "2016", rating: "4Star", plot: " A Complete Hourror" }, 
  13.                  { title: "DDLJ", year: "1995", rating: "SuperStar", plot: "Romantic love story" }, 
  14.                  { title: "Sultan", year: "2016", rating: "5Star", plot: "A Warrior" }, 
  15.                  { title: "BajiRao Mastani", year: "2015", rating: "4.5 Star", plot: "Film of the Year" } 
  16.     ]; 
  17.     $scope.movies = movies; 
  18.     $scope.AddMovie = function (movie) { 
  19. if ($scope.edit == true) { 
  20. var index = $scope.movies.indexOf(movie); 
  21. // alert("edit Called");
  22.             $scope.movies[index] = movie; 
  23. //alert(movie.rating);
  24.             $scope.updatedMovie = movie; 
  25.             $scope.success = true; 
  26.             $scope.movie = {}; 
  27.         } 
  28. else { 
  29. var newMovie = { 
  30.                 title: $scope.movie.title, 
  31.                 year: $scope.movie.year, 
  32.                 rating: $scope.movie.rating, 
  33.                 plot: $scope.movie.plot 
  34.             }; 
  35.             movies.push(newMovie); 
  36. // alert("Add Called");
  37.         } 
  38.     } 
  39.     $scope.DeleteMovie = function (movie,index) { 
  40.         movies.splice(index, 1); 
  41. // $scope.movie = movie;
  42.         $scope.updatedMovie = movie; 
  43.         $scope.success = false; 
  44.         $scope.clear = true; 
  45.         $scope.movie = {}; 
  46.         console.log(index); 
  47.     } 
  48.     $scope.EditMovie = function (movie, index) { 
  49.         $scope.selectedRow = null;  // initialize our variable to null
  50.         $scope.selectedRow = index; 
  51.         $scope.movie = movie;         
  52.         $scope.edit = true; 
  53.     } 
  54. }]); 

Run your Application now and it should perform the actions. Once you run an Application, give the path of your desired page. It should look, as given below, in the screenshot. For now, the button will be working as anticipated.
output
Kindly open Movie.html page and find the given below line and understand its actual fact. Please refer below the screen, given below:
code
There are three major cases here as shown below:
First – Add button with + symbol
Second – Edit button with pencil symbol
Third - Cross with * Symbol.

Kindly find GIF image, given below, which performs all the actions. It will help you to understand the facts.

Hope this will help you some day. Enjoy coding.

0 comments :

Post a Comment