Skip to main content

Combine Two Column Using AngularJS

This code will show to do Combine Two Column using AngularJS. The code will dynamically combine the two table column into one and the content also merge. Feel free to use this code and modify as you learn from it. To learn more about this, just follow the steps below.

Getting started:

First you will need to download & install the AngularJS here's the link And this is the link for the bootstrap that i used for the layout design

Creating the Main Interface

This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body ng-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">Combine Two Column Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-8">
  18. <button type="button" ng-click="contentToggle('name')" class="btn btn-primary">Combine</button>
  19. <br /><br />
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th ng-hide="isContentToggled('name')">Firstname</th>
  24. <th ng-hide="isContentToggled('name')">Lastname</th>
  25. <th ng-show="isContentToggled('name')">Full Name</th>
  26. <th>Age</th>
  27. <th>Address</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;">
  31. <tr ng-repeat="member in members">
  32. <td ng-hide="isContentToggled('name')">{{member.firstname}}</td>
  33. <td ng-hide="isContentToggled('name')">{{member.lastname}}</td>
  34. <td ng-show="isContentToggled('name')">{{member.firstname}} {{member.lastname}}</td>
  35. <td>{{member.gender}}</td>
  36. <td>{{member.address}}</td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. </div>
  41. </div>
  42. <script src="js/angular.js"></script>
  43. <script src="js/script.js"></script>
  44. </body>
  45. </html>

Creating the Main Function

This code contains the main function of the application. This code will combine the two column when user click the table header. To that just kindly copy and write these block of codes inside the text editor, then save it inside the js folder as script.js
  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3. var members = [
  4. {firstname: "John", lastname: "Smith", gender: "Male", address: "Hong Kong"},
  5. {firstname: "Steve", lastname: "Roger", gender: "Male", address: "New York"},
  6. {firstname: "Kate", lastname: "Sebas", gender: "Female", address: "Paris"}
  7. ];
  9. $scope.members = members;
  11. $scope.content = undefined;
  13. $scope.contentToggle = function(name){
  14. if ($scope.isContentToggled(name)){
  15. $scope.content = undefined;
  16. } else {
  17. $scope.content = name;
  18. }
  19. }
  21. $scope.isContentToggled = function (name){
  22. return $scope.content == name;
  23. }
  24. });
There you have it we successfully created a Combine Two Column using AngularJS. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment