Why is my ng-if in td having unexpected results? - angularjs

Maybe I am completely confused how td works as I haven't used tables in my code for yeeaars (strong div believer), or maybe I am confused on how ng-if is attempting to separate my data.
Either way, I am not getting my expected results, but there is otherwise nothing wrong with the code, I think my logic is just off.
Here is a working Plunker http://plnkr.co/edit/LoMEMBYc6kUodb9tcbfM?p=preview
<table>
<tr ng-repeat="z in waiverModalLinks">
<td ng-if="z.id=='color'" class="{{z.id}}">{{z.title}}</td>
<td ng-if="z.id==''" class="{{z.id}}">{{z.title}}</td>
</tr>
</table>
The ultimate end result is to have every other td a different color, however I realize that the current logic is going to put the colored td on the left and the non-colored td on the right. But that was the object I was working with before I realized I needed two separate columns, finding the need for a table in the first place.
But anyway, what is not working then is that.. well they are still stacked. I would expect to see the colored td on the left-hand side of the table and the non-colored td on the right-hand side of the table.
Where's the err in my logic?

Your data model seems to be the source of the confusion. You have a simple array, so each td will be in the same row. If you create a 2d array, you can make a new row for each inner array:
$scope.waiverModalLinks =[
[
{title:"1", link:"", id:"color"},
{title:"2", link:"", id:""}
],
[
{title:"3", link:"", id:"color"},
{title:"4", link:"", id:""}
]
];
And a simpler repeater:
<tr ng-repeat="row in waiverModalLinks">
<td ng-repeat="z in row" class="{{z.id}}">
{{z.title}}
</td>
</tr>
Plunkr: http://plnkr.co/edit/VeXBJcf3naI5bRttQ1Pt?p=preview

z.id is either equal to 'color', or an empty string, but can't be both at the same time. So every row will have at most one cell:
if z.id is equal to color, the generated dom will be
<tr ng-repeat="z in waiverModalLinks">
<td class="{{z.id}}">{{z.title}}</td>
<!-- removed second td -->
</tr>
if z.id is equal to ''', the generated dom will be
<tr ng-repeat="z in waiverModalLinks">
<!-- removed first td -->
<td class="{{z.id}}">{{z.title}}</td>
</tr>
You need two cells in every row: one blank and one filled, or vice-versa:
<table>
<tr ng-repeat="z in waiverModalLinks">
<td ng-if="z.id==''"></td>
<td class="{{z.id}}">{{z.title}}</td>
<td ng-if="z.id=='color'"></td>
</tr>
</table>

Related

AngularJS: Display two arrays in separate columns of table

I have two arrays of variable lenghts containing html which need to be displayed side by side in a table by index. So arrayA[0] should be in the same row as arrayB[0], and so on.
I figured I could do this with two ng-repeat-start directives, e.g.:
<table>
<tbody ng-repeat-start="arrayA">
<tr ng-repeat-start="arrayB">
<td bind-html="arrayA.Details"></td>
<td bind-html="arrayB.Details"></td>
<tr ng-repeat-end></tr>
<tbody ng-repeat-end></tbody>
</table>
However this does not seem to work. Any ideas?
Thanks

Automatic scrolling loop for table when content is too big (sub teacher screen)

I've got a screen that displays substitute teacher information for students in two tables for the current day and the next schoolday. On days with lots of absent teachers, there are too many table rows to fit the screen and I would like the table to scroll/loop through the content automatically (to the bottom, and then start with the top again).
I've found code that scrolls up and down, but not for a continuous loop with a table. I've also learned that to loop I need to create a "clone" of the table's content/body, but I haven't gotten much further than that.
Here's the HTML:
<table id="main">
<tr>
<td width="50%">
<table class="half-width-table">
<tr>
<td colspan="5" class="firstrow">TODAY</td>
</tr>
<tr>
<td width="100%" colspan="5" style="margin: 0; padding: 0;"></td>
</tr>
<tr>
<td width="60" class="table_header text_bold">Class</td>
<td width="20" class="table_header text_bold">Period</td>
<td width="40" class="table_header text_bold">Teacher</td>
<td width="40" class="table_header text_bold">Substitute</td>
<td width="40" class="table_header text_bold">Info</td>
</tr>
<tr class="white">
<td>5RS</td><td>2</td><td>RSWI</td><td>LLOR</td><td>Room A41</td>
</tr>
<tr class="grey">
<td>7MC</td><td>5</td><td>TPOR</td><td>NFIE</td><td>Chem, Room S11</td>
</tr>
<!-- This list will often go on and on, usually 40+ items -->
</table>
</td>
<td><!-- The same again for TOMORROW -->
</td>
</tr>
</table>
Auto-scrolling/looping should only be done
if (table_height > window.innerHeight)
(I could get rid of "TODAY" and "TOMORROW" as separate rows in the table by just writing that text aove the table if table-head and tbody are necessary for a solution.)
Any help is much appreciated!
I've found a solution and I'm posting it here, in case someone has the same problem:
I modified the script provided by a user and trincot in this question to suit my needs. I splitted the tables in half, and put the inner and outer div around the content of each table, calling them inner1/outer1 and inner2/outer2.
I adjusted the script so that it would scroll both tables independently. The speed is dependend on the table height, and set in outer1_scroll.
If you want the exact same speed for both tables, remove the "+5000" (which I added to make the table scroll a little faster when there is a lot of content).
The last line ensures that scrolling is only activated if necessary, that is if the table doesn't fit into the window.
var outer1_height = parseInt($(".outer1").height());
var outer1_scroll = outer1_height*20+5000;
function autoScrollDown(){
$(".inner1").css({top:-$(".outer1").outerHeight()})
.animate({top:0},30000,"linear", autoScrollDown);
}
function autoScrollUp(){
$(".inner1").css({top:0}) // jump back
.animate({top:-$(".outer1").outerHeight()},outer1_scroll,"linear", autoScrollUp);
}
$('.outer1').css({maxHeight: $('.inner1').height()});
$('.inner1').html($('.inner1').html() + $('.inner1').html());
if (outer1_height > window.innerHeight) {
autoScrollUp();
}

Bootstrap - How to line up two tbody's properly?

I'm having an issue where lining up a table isn't working properly and I was wondering if anyones dealt with something like this before. I looked it up, but couldn't find anything addressing it. I'm using AngularJS and nested ng-repeats which is why I'm having some trouble with this (and need to nest them inside a table). My code is below:
<table class="table table-condensed table-hover table-responsive">
<thead><tr>
<th class="col-sm-4">1</th>
<th class="col-sm-3">2</th>
<th class="col-sm-3">3</th>
</tr></thead>
<tbody ng-repeat="blah in blah">
<tr ng-repeat="blah2 in blah">
<td>......</td>
</tr>
</tbody>
<!--This is the end of "Table 1" in the diagram below-->
<tbody ng-repeat="blah3 in blah4">
<tr ng-repeat="blah5 in blah6">
<td>.........</td>
</tr>
</tbody>
<!--This is the end of "Table 2" in the diagram below-->
</table>
I'm ending up with a result like this (note, I had to draw it due to the fact that the table data I'm using is sensitive information):
How can I pull the second tbody (the smaller one) next to the first?
Thanks
If I understood you correctly, you can simply use Bootstrap's columns as containers for your tables. For instance:
div.col-md-6
Will render two columns together until the screen collapse.
https://jsfiddle.net/DTcHh/11692/
Why not use the grid system of bootstrap ?
<div class="row">
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4">.col-md-4</div>
</div>
I don't know what you want to put in the table but you can probably put it into the grid system. link to doc

Creating and Assign variable value on template

In my view I have a table that will be filled dinamycally with data from server. This table should have at least 5 rows, which means that if my server doesn't return 5 sets of data, the table should be filled with 5 - (n_sets_returned). To make that happen I guess the easy way would be to define a variable and set it to the number of sets returned, but I can't make it. I tried as follow:
<tbody class="center-alignment xcompact" ng-init="cont = 0">
<tr ng-repeat="row in data">
<td>{{row.val1}}</td>
<td>{{row.val2}}</td>
<td>{{row.val3}}</td>
<td>{{row.val5}}</td>
{{cont = $index}}
</tr>
<tr ng-repeat="n in [] | range:(5-cont)">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
The problem is I don't even know if this would work (probably not) because the empty rows appears before the filled ones, what means Angular obviously doesn't wait for server response to fill the empty rows.
I'm pretty new at Angular. Any help will be appreciated. Thanks.

How to generate table tr from multiple overlapping ng-repeat

While replacing legacy javascript with angularjs code, I'm stuck with the following use case (simplified code as follows):
<tr ng-repeat="section in page.sections>
<td>{{section.name}}:</td>
</tr>
<tr ng-repeat="task in section.tasks>
<td>{{task.name}}:</td>
</tr>
The expected result should be a single table, with task rows should follow parent section row.
I understand that the inner loop won't work because of the scope of the section element is not the parent of the inner loop.
Is there a work around (or a best suitable way) to perform this?
The following should work:
<tr ng-repeat-start="section in page.sections">
<td>{{section.name}}:</td>
</tr>
<tr ng-repeat="task in section.tasks">
<td>{{task.name}}:</td>
</tr>
<tr ng-if="false" ng-repeat-end><td>end</td></tr>
See a demo plunkr

Resources