Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  172] [ 1]  / answers: 1 / hits: 58659  / 10 Years ago, thu, march 13, 2014, 12:00:00

I'm trying to implement custom sortBy directive in order to make columns in html table sortable.



HTML:



<thead>
<tr>
<sort-by-directive
ng-repeat=header in headers
onsort=onSort
sortdir=filterCriteria.sortDir
sortedby=filterCriteria.sortedBy
sortvalue={{ header.value }}>{{ header.title }}
</sort-by-directive>
</tr>
</thead>


JS:



angular.module('mainApp.directives').directive('sortByDirective', function () {

return {
templateUrl: 'SortHeaderTemplate',
restrict: 'E',
transclude: true,
replace: true,
scope: {
sortdir: '=',
sortedby: '=',
sortvalue: '@',
onsort: '='
},
link: function (scope, element, attrs) {
scope.sort = function () {
if (scope.sortedby == scope.sortvalue)
scope.sortdir = scope.sortdir == 'asc' ? 'desc' : 'asc';
else {
scope.sortedby = scope.sortvalue;
scope.sortdir = 'asc';
}
scope.onsort(scope.sortedby, scope.sortdir);
}
}
};
});


Directive Template:



<script id=SortHeaderTemplate type=text/ng-template>
<th ng-click=sort(sortvalue)>
<span ng-transclude=></span>
<span ng-show=sortedby == sortvalue>
<i ng-class={true: 'sorting_asc', false: 'sorting_desc'}[sortdir == 'asc']></i>
</span>
<span ng-show=sortedby != sortvalue>
<i ng-class={true: 'sorting', false: 'sorting'}[sortdir == 'asc']></i>
</span>
</th>
</script>


So when I use th as root tag of directive template I retrieve an error:



Error: [$compile:tplrt] Template for directive 'sortByDirective' must have exactly one root element. SortHeaderTemplate


but when I change th to a or span tags everything works fine.



What am I doing wrong?


More From » angularjs

 Answers
2

I expect that the <th> is getting melted away at some intermediate point when it is evaluated outside the context of a <tr> (put that template into some random part of your webpage to see the <th> disappear).



In your position, I would use a <div> in the template, change sort-by-directive to an 'A' type directive, and use a <th sort-by-directive>...</th> as before, without replace: true.


[#72015] Tuesday, March 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaylynkarinam

Total Points: 740
Total Questions: 103
Total Answers: 103

Location: Liechtenstein
Member since Wed, Dec 8, 2021
2 Years ago
;