I want to achieve something like this:
<li>Item1
<ul>
<li>Item2</li>
<li>
Item3
<ul>
<li>
Item4
<ul>
<li>
Item5
<ul>
<li>
Item6
<ul>
<li> Item7 </li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li> Item8 </li>
</ul>
</li>
Item 1 etc are pulled from a dictionary which can change based on user input. Below is what I tried with my MVC app in the view:
#foreach (var stp in Model.stepData)
{
<li>#stp.Key</li>
<ul>
#for (int i = 0; i < stp.Value.Count; i++)
{
<li>#stp.Value[i]</li>
}
</ul>
}
I know I want to add < ul > before the < li > in the #stp.Value loop.. but that needs to be open.. the closing tags need to be after the last one in that iteration is written. So I thought that maybe another for loop to enter the tag.. but that doesn't work at all. Is it possible for me to achieve this in html?
code of what I tried:
#for (int i = 0; i < stp.Value.Count; i++)
{
<ul><li>#stp.Value[i]</li>
}
#for(int i = 0; i<stp.Value.Count;i++){</ul>}
TIA for your help.
Tried the answer below but got it was unable to find the listItems in the current context within the loop and it also complained of too many characters in the character literal when #for was used.
The target framework is .Net Core 2.0; MVC 5
This should loop through stp.Value and returned each item wrapped in li tags rendered within your parent ul tag:
<ul id="myList">
// stp.Value items here
</ul>
<script>
var list = docment.getElementById('myList');
var listItem = '';
#for (int i = 0; i < stp.Value.Count; i++){
listItem += '<li>' + #stp.Value[i] + '</li>'
}
list.innerHTML = listItem;
</script>
Most MVC frameworks have their own approach to this but since you didn't specify which framework you are using, the above is a general approach to it using vanilla JS.
Made slight modification to the answer above so that it works with razor loop
:
`
<script>
var list = document.getElementById('valueList');
var listItem = '';
var count = 0;
#for (int i = 0; i<stp.Value.Count; i++) {
<text>
listItem += '<ul><li>' + '#stp.Value[i]' + ''
count = i;
</text>
}
for (var i = 0; i <=count; i++) {
listItem += '</li></ul>'
}
list.innerHTML = listItem;
</script>
`
Related
I have a code of TOC on my website it seems to be very great for users.
But when I integrate it into my indiantechhunter's it appears in open condition i.e in show condition, I want it to be in Hided condition.
How can I do It?
The code is here ↓
<html>
<head>
...HEAD Stuff...
<script>
var hideTOC = function(){
var hideThese = document.getElementsByClassName("toc_list");
for (var i = 0; i < hideThese.length; i++){
hideThese[i].style.display = "none";
}
document.getElementById("showTOC").style.display = "";
}
var showTOC = function(){
var showThese = document.getElementsByClassName("toc_list");
for (var i = 0; i < showThese.length; i++){
showThese[i].style.display = "";
}
document.getElementById("showTOC").style.display = "none";
}
</script>
</head>
<body>
...BODY Stuff...
<div id="toc_container">
<p class="toc_title">Contents [<a href='javascript:void(null)' class='toc_list' onclick='hideTOC()'>hide</a><a href='javascript:void(null)' id='showTOC' style="display:none;" onclick='showTOC()'>show</a>]</p>
<ul class="toc_list">
<li>1 First Point Header
<ul>
<li>1.1 First Sub Point 1</li>
<li>1.2 First Sub Point 2</li>
</ul>
</li>
<li>2 Second Point Header</li>
<li>3 Third Point Header</li>
</ul>
</div>
...BODY Stuff...
</body>
</html>
I want to show it as shown in the following photo when a user opens page containing TOC.
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?
this how it look when we debug and
this how we wanted to look like
i have a foreach loop and and inside the foreach but i want to close the tag outside the foreach . so the opening of inside and the closing outside.
#foreach (var item1 in Model){
if(num != item1.level){
num = item1.level;
<ul class="#Html.DisplayFor(modelItem => item1.circleLevel)">
#foreach (var item in Model){
if(item1.level == item.level){
<li>
<a class="text" style="background-color:
#Html.DisplayFor(modelItem => item.IsExest)"
href="#Url.Action("Details",
"Tree", new { id = item.User_ID
})">#Html.DisplayFor(modelItem => item.Name)</a>
</li>
}
}
<li>
<div class="text" data-toggle="tooltip" data-placement="left"
title="add family member">#Html.ActionLink("+", "Create")</div>
</li>
<ul class="circle5"></ul>
}
}
</ul>
You can create the ul element outside the forloop and then append the li to the ul element.
Something like this:-
var createUlElem = $('<ul class="ulClass"></ul>');
var liElem='';
$.each (items,function(indx,item1) {
liElem +='<li><a class="text">item1.name</a></li>';
})
$(createUlElem).append($(liElem));
How do I align add this text properly?
my code :
data[0]=hello 123
data[1]=hellowq 345
data[2]=heloowaaa 678
for (var i = 0; i < data.length; i++) {
obj += "<pre><li>" + data[i] + "</li></pre>";
}
my actual input :
hello 123
hellowq 345
heloowaaa 678
output :
hello 123
hellowq 345
heloowaaa 678
I would like to get output like this:
hello 123
hellowq 345
heloowaaa 678
You have invalid HTML, the only valid child of either a <ul> or <ol> is an <li> element; the <pre> elements should be within the <li> elements:
for (var j = 0; j < data.length; j++) {
obj += "<li><pre>" + data[i] + "</pre></li>";
}
And the CSS for the <pre> elements set to white-space: pre-wrap (or similar), this is the usual default styling of that element, but it's possible that the defaults have been overwritten or set differently in your browser.
As I claimed in the comments, you probably add the same amount of spaces to each label/value-pair. The solution is probably in the code we don't see where you would have to subtract the length of your label+value from the number of whitespaces generated.
A better way to do this would be to layout in html/css instead. A quick and dirty example.
HTML
<ul>
<li>Domdido <span class="value">$8</span></li>
<li>Domdido123 <span class="value">$8</span></li>
</ul>
CSS:
ul{list-style:none;padding-left:0;
width:50%;
}
.value{float:right;}
http://jsfiddle.net/tqs2rvrd/
Hi I want to add a class of "active" to my li, if it is active or if I am on a page under that. I have the following code
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = null;
}
<nav class="topNav">
<ul>
#foreach (var item in Model.Content.AncestorOrSelf(1).Children.Where(x => x.IsVisible() && x.IsDocumentType("Subfrontpage") || x.IsDocumentType("Procesguide")))
{
<li>
#item.Name
</li>
}
</ul>
</nav>
I think I can do something with this, but it gives an error
var isSelected = Model.Path.Contains(item.Id.ToString()) ? "active" : "";
<li class="#Html.Raw(isSelected)">
#item.Name
</li>
This is the error I get. Line 10.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'Umbraco.Web.Models.RenderModel' does not contain a definition for 'Path' and no extension method 'Path' accepting a first argument of type 'Umbraco.Web.Models.RenderModel' could be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 8: #foreach (var item in Model.Content.AncestorOrSelf(1).Children.Where(x => x.IsVisible() && x.IsDocumentType("Subfrontpage") || x.IsDocumentType("Procesguide")))
Line 9: {
Line 10: var isSelected = Model.Path.Contains(item.Id.ToString()) ? "active" : "";
Line 11:
Line 12: <li class="#Html.Raw(isSelected)">
I have now tried with this, but no lunk
<ul>
<li>
#home.Name
</li>
#foreach (var item in Model.Content.AncestorOrSelf(1).Children.Where(x => x.IsVisible() && x.IsDocumentType("Subfrontpage") || x.IsDocumentType("Procesguide")))
{
var isSelected = item.IsDescendant(Model,"active", "");
<li class="#Html.Raw(isSelected)">
#item.Name
</li>
}
</ul>
ok here is the solution. This is for typed, not dynamic cshtml.
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = null;
var home = Model.Content.AncestorOrSelf(1);
}
<ul>
#*Render Home item*#
#{ var homeActive = ""; }
#if( home.Id == Model.Content.Id){
homeActive = "active";
}
<li class="#homeActive">
<a href="#home.Url">
#home.Name
</a>
</li>
#*Render Home children*#
#foreach (var item in home.Children.Where(x => x.IsVisible()))
{
var active = "";
if(home.Id != Model.Content.Id){ #* if NOT home *#
if (item.Id == Model.Content.AncestorOrSelf(2).Id){
#* if foreach id and currentpage ancestor id is equal *#
active = "active";
}
}
<li class="#active">
<a href="#item.Url">
#item.Name
</a>
</li>
}
</ul>
You can try something like this:
var isSelected = item.IsDescendant(Model,"active", "");
Here is a list of the functions you can use:
#Model.AncestorOrSelf(string nodeTypeAlias)
#Model.AncestorOrSelf(int level)
#Model.AncestorOrSelf(Func<DynamicNode, bool> func)
and those Helper functions:
#Model.IsDescendant(DynamicNode[,valueIfTrue][,valueIfFalse])
#Model.IsDescendantOrSelf(DynamicNode[,valueIfTrue][,valueIfFalse])
Reference: Is the current page a descendant of a specific node id?
The following is the default script for navigation produced by umbraco:
#inherits umbraco.MacroEngines.DynamicNodeContext
#*
Macro to display child pages below the root page of a standard website.
Also highlights the current active page/section in the navigation with
the css class "current".
*#
#{
#*Get the root of the website *#
var root = Model.AncestorOrSelf(1);
}
<ul>
#foreach (var page in root.Children.Where("Visible"))
{
<li class="#page.IsAncestorOrSelf(Model, "current", "")">
#page.Name
</li>
}
</ul>
I just created a menu myself and needed the Home page to be active also. Working with just the method IsAncestorOrSelf doesn't work for the homepage.
The piece of code I've created is this:
#{
var root = Model.AncestorOrSelf(1);
var homeClass = root.Id == Model.Id ? "active" : "";
}
<ul id="nav" class="nav navbar-nav pull-right">
<li class="#homeClass"><a href="/" >Home</a></li>
#foreach (var page in root.Children.Where("Visible"))
{
<li class="#page.IsAncestorOrSelf(Model, "active", "")">
#page.Name
</li>
}
</ul>
This iterates through all children of the root node (1). To be able to discover if you are on the Home page, I'm checking the Id's of the page and set the active class accordingly.