0

I want to create a angularjs directive which can be used in many pages, The directive should handle the html table creation, It can have different columns depending upon the requirement of the page. What should be the starting point to write this directive where user we can have different columns in different pages but having one directive only.

Example can be for home page

 <table>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th> 
        <th>Age</th>
      </tr>
      <tr>
        <td>Jill</td>
        <td>Smith</td> 
        <td>50</td>
      </tr>
      <tr>
        <td>Eve</td>
        <td>Jackson</td> 
        <td>94</td>
      </tr>
    </table>

for about page

<table style="width:100%">
  <tr>
    <th>Company</th>
    <th>Address</th> 
    <th>City</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>
1

1 Answer 1

2
var tableDirective = function(){
    return {
       restrict: 'EA', //E = element, A = attribute, C = class, M = comment         
        scope: {
            data: '='         
            },
            templateUrl: 'table-directive.html',
        }
};

angular.module('myModule').directive('tableDirective', tableDirective);

table-directive.html:

<table style="width:100%">
  <tr>
    <th ng-repeat="item in data.headList">{{ item.name }}</th>
  </tr>
  <tr ng-repeat="item in data.rowList">
    <td>{{ item.name }}</td>
    <td>{{ item.surname }}</td> 
    <td>{{ item.propertyName }}</td>
  </tr>
</table>

Then, you can use directive and pass data :

<table-directive data="data"></table-directive>

Here data come from controller:

...
$scope.data = {
   headList: [{ name: 'Company' }, { name: 'Address' }, { name: 'City' }],
   rowList: // here will be your data
}
...
Sign up to request clarification or add additional context in comments.

3 Comments

There are multiple tables in different pages and couple of pages will have text and text box in the same cell of the table but other tables wont have, one table will have text and icon in the same cell of the row so the behavior of table cells change with respect to page plus headers of tables are different from what we get in $scope.data, so i'm wondering if we can achieved this kind of customization using one angular js custom directive.
@zumuhu, I understood
Would you suggest/advise me for the last question: "There are multiple tables in different pages and couple of pages will have text and text box in the same cell of the table but other tables wont have, one table will have text and icon in the same cell of the row so the behavior of table cells change with respect to page plus headers of tables are different from what we get in $scope.data, so i'm wondering if we can achieved this kind of customization using one angular js custom directive. "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.