Unable to add row dynamically in loop inside table in Razor - razor

I want to display 3 Cells in a row and then create row <tr> dynamically for remaining cells. When I searched for this logic, I am getting same answer from multiple articles about using modulo and it seems working for evryone but I'm not getting desired output. What am I missing here?
Code:
<table>
#for (var i = 0; i < deliveryProvider.DeliveryTagInfo.DeliveryProviderTagList.Count; i++)
{
var deliveryTag = deliveryProvider.DeliveryTagInfo.DeliveryProviderTagList[i];
if ((i%3) == 0) //Create new row for 4th item
{
#:<tr>
}
<td>
<div class="input-element checkbox">
#Html.CheckBoxFor(x => x.DeliveryProviderList[j].DeliveryTagInfo.IsDeliveryTagSelectedList[i], new
{
name = deliveryTag,
id = deliveryTag
})
<label for="#deliveryTag">
<span></span>#deliveryTag</label>
</div>
</td>
if ((i%3) == 0)
{
#:</tr>
}
}
</table>
Output:
Expected Output:
Test1
ALCOHOL SPILLABLE COLD_BAG
HOT_BAG HEAVY SIZE
Test2
abc pqr xyz

Some of the problem code is in your closing tag.
if ((i%3) == 0)
{
#:</tr>
}
On your first iteration, i%3 is == 0, so you put a closing tag on your first statement. Essentially you are only creating <tr> tags on every third one.
Here is an example of how you could remedy this:
var listCount = 11;
for (var i = 0; i < listCount; i++)
{
if (i%3 == 0)
{
#:<tr>
}
//Add Checkboxes
//Closing Tag
if (((i+1) % 3 == 0 )|| i == listCount - 1)
{
#:</tr>
}
}
Closing tag Explained...
For the first condition of the closing tag:
(i+1) % 3 == 0 )
Since the item has been added, we check to see if it is the third one- by using i+1, we will know how many items
are in the row and we can close it if it is.
For the second condition:
i == listCount - 1
We also need to account for if it is the last item. If the list only has 2 items for example, we still need to close the row.

Related

Angular : how to create the row dynamically in angular

I am using
<div *ngFor="let item of object | keyvalue">
{{item.key}}:{{item.value}}
</div>
to print the value in html. but I need to print it in form of table
say total we have 8 key value pair, now i want to create the new row after 4th key value pair, such that it should come in this way
a a a a
a a a a
can we do it dynamically, the key value pair can be 12 or 16, multiple of 4
you could convert your structure into a more suitable one and use a nested loop.
This would also lead to a data structure more similar to a table.
something like that
<table>
<tr *ngFor="let item of arr">
<td *ngFor="let td of item">
{{td}}
</td>
</tr>
</table>
and in your components ts file.
let i = 0;
const itemsPerRow = 4;
Object.keys(this.object).forEach((key,index) => {
if(index > 0 && index % itemsPerRow == 0) {
i++;
}
if(!this.arr[i]) {
this.arr[i] = [];
}
this.arr[i].push(this.object[key]);
});
this will convert your structure into an array of objects where every array item represents one row.
Here's a working stackblitz
Just as further info.
You can get the index of your current element in your *ngFor like
*ngFor="let item of items;let i = index"

Can't set the .innerHTML of an <span> created dynamically (Angular)

I'm trying to change the text inside a <'span> generated dynamically.
The <'span> is generated inside a list (<'ul><'li>) that change its number of item (li) if a selection was changed.
this is the dynamic list (it works)
<ul *ngIf="placeholder[0]">
<li *ngFor="let time of placeholder[0], let i = index" >
<div><span id="timer0{{i}}"> lol </span><span *ngIf="timer[0].timeLeft[i] > 0"> : {{timer[0].timeLeft[i]}} secondi <button (click)="restart(0, i)"> reset qua</button><button (click)="check()"> check div</button></span></div>
</li>
</ul>
This is the Typescript code in which i'm trying to change the text inside the <span id = "timer0{{i}}">
setTime(i: number){
//setting del timer (azzero tutti i campi)
this.placeholder[i]=[]
this.timer[i].lunghezza = Object.keys(this.datiX.fileJson.ricette[this.datiX.postazioniNome[this.datiX.defPostazione]][this.datiX.ricetta[i]]).length;
for (let j = 0; j < this.timer[i].lunghezza; j++) {
this.placeholder[i][j] = [""]
}
for (let j = 0; j < this.timer[i].lunghezza; j++) {
this.pauseTimer(i, j);
this.timer[i].inizio[j] = 0;
this.timer[i].fine[j] = Object.keys(this.datiX.fileJson.ricette[this.datiX.postazioniNome[this.datiX.defPostazione]][this.datiX.ricetta[i]][j]).length;
this.timer[i].timeLeft[j] = <number> <unknown>Object.keys(this.datiX.fileJson.ricette[this.datiX.postazioniNome[this.datiX.defPostazione]][this.datiX.ricetta[i]][j][0])[this.timer[i].inizio[j]];
}
console.log("check: ",document.getElementById('timer01'))
}
console.log("check: ",document.getElementById('timer01')) gives me = null inside the function, but if i call a function (after pressing the check button) that print it, it gives me the correct value ("lol")...
what i'm doing wrong?

How to change the appearance of an anchor (text) by comparing 2 id?

I'm building a book-like pagination style for my website. It is composed of a 4 parts code: the text supposed to be displayed, the pages numbered, the "next" and "previous" buttons and the script to determine which part/page of the text is displayed.
var current = 1;
var totalPages = document.getElementById("pageContainer").childElementCount;
function showPages(id = 1) {
if (id < 1 || id > totalPages)
return;
curr_page = document.getElementById("page" + current);
curr_page.classList.add("pageHidden");
curr_page.classList.remove("pageDisplayed");
target_page = document.getElementById("page" + id);
target_page.classList.add("pageDisplayed");
target_page.classList.remove("pageHidden");
current = id;
}
.pageHidden
{
display: none;
}
.pageDisplayed
{
display: block;
}
<div id="pageContainer">
<div class="pageDisplayed" id="page1"><p>page 1 displayed</p></div>
<div class="pageHidden" id="page2"><p>That is the second page.</p></div>
<div class="pageHidden" id="page3"><p>And finally a third one.</p></div>
</div>
<h2>pages :
1
2
3
</h2>
<h2>
<span style="float: left;">
Previous
</span>
<span style="float: right;">
Next
</span>
</h2>
Now, I'd like to mark the actual page that is being displayed by changing the appearance of the link. For now it shows
"Pages: 1 2 3"
And I'd like it to become, for example:
"Pages: 1 [2] 3" (with a different color for the marked "[2]".
I've found ways to do that, but I couldn't make it works with the "next" and "previous" button, which, when triggered, didn't highlight the anchor number.
Basically, if the function showPages returns "page2", the anchor "2" with the id "2" should appear like that: [2].
I think it means that I have to compare the id from the page container, to the id from the pagination part... Anyone know how I can do that?
Same As you are hiding previous block and showing new block. create a css class with following css:
.page:before{
content: '['
}
.page:after{
content: ']'
}
this css will wrap any element with page class in brackets [].
Now add page class to selected page and remove from older one.
pageNum = document.getElementById("" + id);
pageNum.classList.add("page")
pageNum = document.getElementById("" + current);
pageNum.classList.remove("page")
Here is your working code.
var current = 1;
var totalPages = document.getElementById("pageContainer").childElementCount;
function showPages(id = 1) {
if (id < 1 || id > totalPages)
return;
curr_page = document.getElementById("page" + current);
curr_page.classList.add("pageHidden");
curr_page.classList.remove("pageDisplayed");
target_page = document.getElementById("page" + id);
target_page.classList.add("pageDisplayed");
target_page.classList.remove("pageHidden");
pageNum = document.getElementById("" + id);
pageNum.classList.add("page")
pageNum = document.getElementById("" + current);
pageNum.classList.remove("page")
current = id;
}
.pageHidden
{
display: none;
}
.pageDisplayed
{
display: block;
}
.page:before{
content: '['
}
.page:after{
content: ']'
}
<div id="pageContainer">
<div class="pageDisplayed" id="page1"><p>page 1 displayed</p></div>
<div class="pageHidden" id="page2"><p>That is the second page.</p></div>
<div class="pageHidden" id="page3"><p>And finally a third one.</p></div>
</div>
<h2>pages :
1
2
3
</h2>
<h2>
<span style="float: left;">
Previous
</span>
<span style="float: right;">
Next
</span>
</h2>
Here you go..

Can't increase variable

I'm working on a navigation and for some reason, I can't increase a variable.
<ul id="menu">
<li>
Home
<div class="dropdown_2columns">
<!-- Begin 2 columns container -->
<div class="col_2">
<h2>Welcome !</h2>
</div>
</div><!-- End 2 columns container -->
</li><!-- End Home Item -->
#foreach (var mainNode in rootNode.Children())
{
int childCount = 1;
int numChildren = mainNode.Children().Count();
int num = 0; #*<-------------- Num variable*#
<li>
#mainNode.Name
<div id=main-#num class="dropdown_5columns"> #*<----------Using the variable here*#
<!-- Begin 2 columns container -->
<div class="col_5">
<h2>#mainNode.Name</h2>
</div>
#* note if you want ALL descendants change .Children to .Descendats*#
#foreach (var childNode in mainNode.Children())
{
// if first node or new set of three open the div and ul #: is used to stop razor trying to
// "balance" the tags
if (childCount == 1 || (double)childCount % 3 == 1)
{
#:<div class="col_1">
#:<ul>
}
#childNode.Name
// close the div and list if this is either a multiple of 3 or the last one
if ((double)childCount % 3 == 0 || numChildren == childCount)
{
#:</ul>
#:</div>
}
childCount++;
num++; #*< -----------Increasing the varable here*#
}
</div>
</li>
}
</ul>
}
The problem is that the varable doesnt increase after looping. The div id is always main-0 even after looping multiple times. Does anyone know the cause of this?
"Num" is placed inside the mainNode loop, so it's always set to 0.
You have to move the num variable outside the mainNode loop and it should work :-)

Add IF statement to JSP

I am trying to add an if statement to following code, but it keeps throwing errors. What am I doing wrong?
<%
int countOnRows = 0;
int i;
countOnRows= Integer.parseInt(formFields.getValue("broughtBack"));
if (countOnRows = 0) {
<h2>No data available - check back later </h2>
}
for( i=1; i<=countOnRows; i++ )
{ %>
<div class="a_report">
<h2>Dealer Sales Report</h2>
<h3>Sales Results as of July 2014 - <%=formFields.getValue("dealerName"+i)%>, Dealer <span class="dlr_info"># <%=formFields.getValue("dealerNo"+i)%></span></h3>
<div class="body_copy"><span class="print">[ print this report ]</span>
:
: table with table data in report here
:
<% } %>
</div>
It is not Java code:
if (countOnRows = 0) {
<h2>No data available - check back later </h2>
}
change it into:
if (countOnRows == 0) {
%> <h2>No data available - check back later </h2> <%
}
I also changed if (countOnRows = 0) into if (countOnRows == 0)
You should read about using JSTL and EL, it's better than using Java code inside JSP pages.