Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Directive table rows issue

I am a beginner Angular programmer, but I am really close to understanding the directives.

I create a fiddle here, but I have never used fiddle before, and it is not quite rendering ...

the tr-row is a directive. I am trying to loop through the data, and print a directive (row) per record. HTML:

<table ng-controller="fiddleCtrl">
   <thead>
      <th>id</th>
      <th>name</th>
      <th>description</th>
  </thead>
  <tbody>
    <tr><tr-row ng-repeat="d in data" scdata="d"></tr-row></tr>
  </tbody>
</table>

javascript:

var myapp = angular.module('myApp', [])
.controller('fiddleCtrl', ['$scope', function ($scope) {

$scope.data = [
     { id: 1, name: 'Fred',   description: 'not the best worker' }, 
     { id: 2, name: 'Wilma',  description: 'Freds Wife'}, 
     { id: 3, name: 'Barney', description: 'Freds best friend'}, 
     { id: 4, name: 'Louise', description: 'Never heard of Fred'}, 
     { id: 5, name: 'Tracy',  description: 'Some Chick'}, 
     { id: 6, name: 'Foo',    description: 'Inventer of bar'}
];
}]).directive('trRow', function ($compile) {
return {
    restrict: "E",
    replace: true,
    link: function (scope, element, attrs) {
        scope.id = scope.d.id;
        scope.name = scope.d.name;
        scope.desc = scope.d.description;

        var tmpl = '<tr  ><td>{{id}}</td><td><strong>{{name}}</strong></td><td>{{desc}}</td></tr>';
        element.html(tmpl).show();
        //var e =$compile(tmpl)(scope);
        //element.replaceWith(e);
        var e = $compile(element.contents())(scope);
    },
    scope: {
        d: "="
    }
};
});

should be easy. (le sigh)

any help would be appreciated, I REALLY need to understand this.

What is happening in my code is the tr-row directive has replace the table. I get a list of them, (with a tr INSIDE of a tr-row element but there is no table to display them in. I know this means I am close, but I cant think of any new combinations to try.

I just need a simple table with rows in it.

I appologise if this has been asked a million times, I seem to be unsure what to search for. I have tried so many things.

like image 612
Tracy Lauren Avatar asked Sep 01 '13 14:09

Tracy Lauren


2 Answers

Firstly, a tag name can't contain dash char. So you can't use tr-row as tag name, but you can use it as attribute.

Then, you can simply write a directive like that:

.directive('trRow', function () {

    return {
        template: '<tr><td ng-bind="row.id"></td><td><strong ng-bind="row.name"></strong></td><td ng-bind="row.description"></td></tr>'
    };
});

And usage is like that:

<tbody>
    <tr tr-row ng-repeat="row in data"></tr>
</tbody>

A working example in fiddle: http://jsfiddle.net/T7k83/85/

like image 52
Murat Çorlu Avatar answered Oct 31 '22 08:10

Murat Çorlu


Actually this problem is specific to <table> elements.

Browser parsing engines don't like invalid tags inside <table> so they will try to throw your directive out of the table (you can see that by inspecting the element), before your directive has a chance to replace itself with valid elements. This applies even if your directive doesn't contain dash in the name.

The way to solve this would be using directive type A instead of type E, which is suggested by @MuratCorlu.

For other elements such as <div>, you can pretty much replace it with custom tags with names containing dash. For example ng-repeat can be used as a tag.

like image 26
Icycool Avatar answered Oct 31 '22 07:10

Icycool