Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JustJenFelice/97f48f2c5f9582fa8447b871eca1e92b to your computer and use it in GitHub Desktop.

Select an option

Save JustJenFelice/97f48f2c5f9582fa8447b871eca1e92b to your computer and use it in GitHub Desktop.

Revisions

  1. @umidjons umidjons revised this gist Sep 6, 2014. 1 changed file with 62 additions and 0 deletions.
    62 changes: 62 additions & 0 deletions show-clicked-row-details-right-way.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    Show clicked row details. Using ng-if, ng-repeat-start and ng-repeat-end directives
    ===

    ```html
    <!doctype html>
    <html lang="en-US" ng-app="App">
    <head>
    <meta charset="UTF-8">
    <script src="angular.js"></script>
    <title>Users</title>
    <style>
    table{ border-collapse:collapse; }
    td, th{ border:1px solid #999; }
    </style>
    </head>
    <body>

    <table ng-controller="UsersCtrl">
    <thead>
    <tr>
    <th>Name</th>
    <th>Age</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat-start="user in users" ng-click="selUser(user)">
    <td>{{user.name}}</td>
    <td>{{user.age}}</td>
    </tr>
    <tr ng-repeat-end ng-if="isSelected(user)">
    <td colspan="2">{{user.desc}}</td>
    </tr>
    </tbody>
    </table>

    <script>
    angular.module('App',[])
    .factory('Users',function(){
    return {
    query:function(){
    return [
    {name:'John',age:25,desc:'Software Developer at MacroSoft LLC'},
    {name:'Jonatan',age:26,desc:'Professor at University of Tashkent'},
    {name:'Nataly',age:27,desc:'Nurse at central hospital'},
    {name:'Lucy',age:28,desc:'Teacher at district school'}
    ];
    }
    }
    })
    .controller('UsersCtrl',function($scope,Users){
    $scope.users=Users.query();
    $scope.selUser=function(user){
    $scope.selected_user=user;
    }
    $scope.isSelected=function(user){
    return $scope.selected_user===user;
    }
    });
    </script>
    </body>
    </html>
    ```
  2. @umidjons umidjons revised this gist Sep 6, 2014. 1 changed file with 102 additions and 22 deletions.
    124 changes: 102 additions & 22 deletions show-clicked-row-details.md
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,105 @@ I also define `selIdx` to use in `ng-class` to switch class according selected u
    Here is our AngularJS code:

    ```javascript
    angular.module('App',[])
    .factory('Users',function(){
    return {
    query:function(){
    return [
    {name:'John',age:25,desc:'Software Developer at MacroSoft LLC'},
    {name:'Jonatan',age:26,desc:'Professor at University of Tashkent'},
    {name:'Nataly',age:27,desc:'Nurse at central hospital'},
    {name:'Lucy',age:28,desc:'Teacher at district school'}
    ];
    }
    }
    })
    .controller('UsersCtrl',function($scope,Users){
    $scope.users=Users.query();
    $scope.selIdx= -1;

    $scope.selUser=function(user,idx){
    $scope.selectedUser=user;
    $scope.selIdx=idx;
    }

    $scope.isSelUser=function(user){
    return $scope.selectedUser===user;
    }
    });
    ```

    Here is our markup:

    ```html
    <table ng-controller="UsersCtrl">
    <thead>
    <tr>
    <th class="col-1">Name</th>
    <th class="col-2">Age</th>
    </tr>
    </thead>
    <tbody ng-repeat="user in users" ng-switch on="isSelUser(user)" ng-click="selUser(user,$index)">
    <tr ng-class="{sel:selIdx==$index}">
    <td>{{user.name}}</td>
    <td>{{user.age}}</td>
    </tr>
    <tr ng-switch-when="true">
    <td colspan="2" class="desc">{{user.desc}}</td>
    </tr>
    </tbody>
    </table>
    ```

    `ng-switch-when` used to show `tr` only when current row is selected row.

    Here is some styling:
    ```css
    table { border-collapse: collapse; }
    td, th { border: 1px solid #999; }
    .col-1 { width: 200px; }
    .col-2 { width: 300px; }
    .sel { background-color: #bce65e; }
    .desc { background-color: #d1e6ac; }
    ```

    Full code is below:
    ```html
    <!doctype html>
    <html lang="en-US" ng-app="App">
    <head>
    <meta charset="UTF-8">
    <script src="angular.js"></script>
    <title>Users</title>
    <style>
    table { border-collapse: collapse; }
    td, th { border: 1px solid #999; }
    .col-1 { width: 200px; }
    .col-2 { width: 300px; }
    .sel { background-color: #bce65e; }
    .desc { background-color: #d1e6ac; }
    </style>
    </head>
    <body>

    <table ng-controller="UsersCtrl">
    <thead>
    <tr>
    <th class="col-1">Name</th>
    <th class="col-2">Age</th>
    </tr>
    </thead>
    <tbody ng-repeat="user in users" ng-switch on="isSelUser(user)" ng-click="selUser(user,$index)">
    <tr ng-class="{sel:selIdx==$index}">
    <td>{{user.name}}</td>
    <td>{{user.age}}</td>
    </tr>
    <tr ng-switch-when="true">
    <td colspan="2" class="desc">{{user.desc}}</td>
    </tr>
    </tbody>
    </table>
    <script>
    angular.module('App',[])
    .factory('Users',function(){
    return {
    @@ -37,26 +136,7 @@ Here is our AngularJS code:
    return $scope.selectedUser===user;
    }
    });
    ```

    Here is our markup:

    ```html
    <table ng-controller="UsersCtrl">
    <thead>
    <tr>
    <th class="col-1">Name</th>
    <th class="col-2">Age</th>
    </tr>
    </thead>
    <tbody ng-repeat="user in users" ng-switch on="isSelUser(user)" ng-click="selUser(user,$index)">
    <tr ng-class="{sel:selIdx==$index}">
    <td>{{user.name}}</td>
    <td>{{user.age}}</td>
    </tr>
    <tr ng-switch-when="true">
    <td colspan="2" class="desc">{{user.desc}}</td>
    </tr>
    </tbody>
    </table>
    </script>
    </body>
    </html>
    ```
  3. @umidjons umidjons revised this gist Sep 6, 2014. 1 changed file with 25 additions and 1 deletion.
    26 changes: 25 additions & 1 deletion show-clicked-row-details.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,9 @@ Show clicked row details. Using ng-switch, ng-click, ng-class, ng-repeat, $index
    I want to show additional row (user details) below clicked row (selected user) with details.
    First I need some data, therefore I create `Users` factory with `query` method.
    I'll show user `name` and `age` in row by default, but show `desc` only for selected user.

    I create 2 methods, `selUser(user,idx)` - called when clicked onto the row to select user and its index in table,
    `isSelUser(user)` - called to determine, is current row contains selected user.
    I also define `selIdx` to use in `ng-class` to switch class according selected user index.

    Here is our AngularJS code:

    @@ -35,4 +37,26 @@ Here is our AngularJS code:
    return $scope.selectedUser===user;
    }
    });
    ```

    Here is our markup:

    ```html
    <table ng-controller="UsersCtrl">
    <thead>
    <tr>
    <th class="col-1">Name</th>
    <th class="col-2">Age</th>
    </tr>
    </thead>
    <tbody ng-repeat="user in users" ng-switch on="isSelUser(user)" ng-click="selUser(user,$index)">
    <tr ng-class="{sel:selIdx==$index}">
    <td>{{user.name}}</td>
    <td>{{user.age}}</td>
    </tr>
    <tr ng-switch-when="true">
    <td colspan="2" class="desc">{{user.desc}}</td>
    </tr>
    </tbody>
    </table>
    ```
  4. @umidjons umidjons created this gist Sep 6, 2014.
    38 changes: 38 additions & 0 deletions show-clicked-row-details.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    Show clicked row details. Using ng-switch, ng-click, ng-class, ng-repeat, $index.
    ===

    I want to show additional row (user details) below clicked row (selected user) with details.
    First I need some data, therefore I create `Users` factory with `query` method.
    I'll show user `name` and `age` in row by default, but show `desc` only for selected user.


    Here is our AngularJS code:

    ```javascript
    angular.module('App',[])
    .factory('Users',function(){
    return {
    query:function(){
    return [
    {name:'John',age:25,desc:'Software Developer at MacroSoft LLC'},
    {name:'Jonatan',age:26,desc:'Professor at University of Tashkent'},
    {name:'Nataly',age:27,desc:'Nurse at central hospital'},
    {name:'Lucy',age:28,desc:'Teacher at district school'}
    ];
    }
    }
    })
    .controller('UsersCtrl',function($scope,Users){
    $scope.users=Users.query();
    $scope.selIdx= -1;

    $scope.selUser=function(user,idx){
    $scope.selectedUser=user;
    $scope.selIdx=idx;
    }

    $scope.isSelUser=function(user){
    return $scope.selectedUser===user;
    }
    });
    ```