Create a div for each parent? - foreach

I need to wrap all the childrens of the parent with a div. In the example below I managed to put a div tag after each parent but how do I put an end /div tag at the end of the chidrens.
I tried with document.createElement, and now with insertAdjacentHTML ..., what is the best way to fix this, or the best way to encode it?
var divs = document.getElementById("container").querySelectorAll('.activ-window *');
var elements = "";
divs.forEach(function(elem){
if (elem.firstChild) {
elements += elem.insertAdjacentHTML("afterbegin", '<div id="afterParent" style="display:block;">');
elem.insertAdjacentHTML("afterend", '</div>');
}
});
document.getElementById("demo").innerHTML = elements;
<div id="container" class="activ-window">
<div id="parent1">
<div id="child" ></div>
<div id="child" ></div>
</div>
<div id="parent2">
<div id="child" ></div>
<div id="paren3" >
<div id="child" ></div>
<div id="child" ></div>
<div id="child" ></div>
</div>
</div>
</div>
It should look like:
<div id="container" class="activ-window">
<div id="parent1">
<div id="afterParent" style="display:block;">
<div id="child" ></div>
<div id="child" ></div>
</div>
</div>
<div id="parent2">
<div id="afterParent" style="display:block;">
<div id="child" ></div>
</div>
<div id="paren3" >
<div id="afterParent" style="display:block;">
<div id="child" ></div>
<div id="child" ></div>
<div id="child" ></div>
</div>
</div>
</div>
</div>
Edit for Nick:
This is with text(see Parent1):
<div id="parent1">Parent1
<div id="child" >child1</div>
<div id="child" >child1</div>
</div>
I get it with your code
<div id="parent1">
<div id="afterParent" style="display:block;">
"Parent1"
<div id="child" >Child1</div>
<div id="child" >Child1</div>
</div>
</div>
It should look:
<div id="parent1">Parent1
<div id="afterParent" style="display:block;">
<div id="child" >Child1</div>
<div id="child" >Child1</div>
</div>
</div>

If you want to enclose all the divs beneath each parent in a new div, you can create a new div element, move all the non-text children to it, and then add that div as a child to the original parent:
const container = document.getElementById("container");
const parentDivs = container.querySelectorAll('.activ-window div[id^="parent"]');
for (let parent of parentDivs) {
// create a new div
let after = document.createElement('div');
after.id = 'after' + parent.id;
after.style.display = 'block';
// move the parent's non-text children to it
let children = parent.childNodes;
for (let i = 0; i < children.length; i++) {
if (children[i].nodeType != Node.TEXT_NODE) {
after.append(children[i]);
}
}
// and append it to the parent
parent.appendChild(after);
}
console.log(container.innerHTML);
<div id="container" class="activ-window">
<div id="parent1">
Parent1
<div id="child1"></div>
<div id="child2"></div>
</div>
<div id="parent2">
Parent2
<div id="child3"></div>
<div id="parent3">
<div id="child4"></div>
<div id="child5"></div>
<div id="child6"></div>
</div>
</div>
</div>

Related

Grouping/GROUP BY With LINQ in MVC

I am new in MVC. In this page; I list the projects belonging to the "unit name" by loop. And what I want to do: I want to group projects belonging to the same "unit name". Each project has a "unit name" to which it belongs. There can be more than one project belonging to one unit. So I want to group it.
My cshtml code is as follows:
#if (Model.Projects.Any())
{
foreach (var item in Model.Projects.ToList())
{
<div class="ProjectPartialBody" data-id="#item.ID">
<div class="portlet box blue">
<div class="portlet-title">
<div class="caption">
<span><i class="glyphicon glyphicon-asterisk"></i> #item.tbl_Unit.Name</span>
</div>
<div class="tools">
</div>
<div class="actions">
Details
</div>
</div>
<div class="portlet-body">
<span><i class="fa fa-file"></i> #item.Name</span>
<span><i class="fa fa-calendar"> Contract Start and End Dates: #(item.ContractStartDate != null ? item.ContractStartDate.Value.ToString("dd.MM.yyyy") : "-") / #(item.ContractEndDate != null ? item.ContractEndDate.Value.ToString("dd.MM.yyyy") : "-")</i></span>
<div class="well">
<div class="form-group">
<label>Cash Completion Rate: %#item.CashCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: #(item.CashCompletionRate)%;"></div>
</div>
</div>
</div>
<div class="well">
<div class="form-group">
<label>Physical Completion Rate: %#item.PhysicalCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: #(item.PhysicalCompletionRate)%;"></div>
</div>
</div>
</div>
</div>
</div>
</div>
}
}
And the view of the page is as follows (my view is in Turkish language but you'll understand what I am talking about):
My Page View
Can you please help me? And if there is any other code block you want to insert, please tell me.
You first need to group by in your for statement:
foreach(var unitGroup in Model.Projects.GroupBy(g => g.tbl_Unit.Name))
{
//...your MVC markup here
}
Now you actualy need to change the markup as you would need to put in another foreach loop that would generate markup for projects.
So something simillar to this.
<div class="ProjectPartialBody" data-id="#item.ID">
<div class="portlet box blue">
<div class="portlet-title">
<div class="caption">
<span><i class="glyphicon glyphicon-asterisk"></i> #item.tbl_Unit.Name</span>
</div>
<div class="tools">
</div>
<div class="actions">
Details
</div>
</div>
#foreach(var project in unitGroup.ToList())
{
<div class="portlet-body">
<span><i class="fa fa-file"></i> #project.Name</span>
<span><i class="fa fa-calendar"> Contract Start and End Dates: #(project.ContractStartDate != null ? project.ContractStartDate.Value.ToString("dd.MM.yyyy") : "-") / #(project.ContractEndDate != null ? project.ContractEndDate.Value.ToString("dd.MM.yyyy") : "-")</i></span>
<div class="well">
<div class="form-group">
<label>Cash Completion Rate: %#project.CashCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: #(project.CashCompletionRate)%;"></div>
</div>
</div>
</div>
<div class="well">
<div class="form-group">
<label>Physical Completion Rate: %#project.PhysicalCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: #(project.PhysicalCompletionRate)%;"></div>
</div>
</div>
</div>
</div>
}
</div>
</div>
There should be some errors here as I am not very familliar with your model but this should be the general idea behind it.

inputs not filling entire width of bootstrap column in mvc view

I am having the hardest time of getting my inputs to fill the max width of a bootstrap column in a MVC 5 project. I added a view and added the layout page to the view (nothing fancy, just the normal layout page that comes with a typical MVC project). If you run the snippet I provided, my inputs are filling the given width of the bootstrap column, but when I run this code in my app, its not filling the width of the column.
Is there anything in particular that would cause my screenshot to look like it does and not how it is in my snippet?
here is the screenshot ( the multicolored columns are for a point of reference for me )
.max-size {
width: 100% !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="form-horizontal">
<div class="row">
<div class="col-md-12">
<div class="col-md-1" style="background:blue">col-md-1</div>
<div class="col-md-1" style="background:red">col-md-1</div>
<div class="col-md-1" style="background:purple">col-md-1</div>
<div class="col-md-1" style="background:yellow">col-md-1</div>
<div class="col-md-1" style="background:green">col-md-1</div>
<div class="col-md-1" style="background:orange">col-md-1</div>
<div class="col-md-1" style="background:blue">col-md-1</div>
<div class="col-md-1" style="background:red">col-md-1</div>
<div class="col-md-1" style="background:purple">col-md-1</div>
<div class="col-md-1" style="background:yellow">col-md-1</div>
<div class="col-md-1" style="background:green">col-md-1</div>
<div class="col-md-1" style="background:orange">col-md-1</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
ABC
</div>
<div class="col-md-6">
<div class="row max-size">
<div class="col-md-12">
<div class="form-group">
<div class="col-md-12">
<input class="form-control max-size" style="width:100%;" placeholder="Department" />
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input class="form-control max-size" placeholder="Department" />
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input class="form-control max-size" placeholder="Department" />
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input class="form-control max-size" placeholder="Department" />
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input class="form-control max-size" placeholder="Department" />
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input class="form-control max-size" placeholder="Department" />
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<div class="col-md-5">
<div class="btn btn-block btn-danger">
<center>Cancel</center>
</div>
</div>
<div class="col-md-5">
<div class="btn btn-block btn-success">
<center>Submit</center>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-3">
CBA
</div>
</div>
</div>
</div>
they can't go full width cause their parent have a col-md-6 put 12 and it will work
i mean here
<div class="col-md-6">
<div class="row max-size">
<div class="col-md-12">
also try to read more about nesting here is a link http://getbootstrap.com/2.3.2/scaffolding.html

Angular 2 components tree

I'm developing my first application with Angular 2 and I have installed Augury plugin for Google Chrome, in order to help me debug the code.
Here's the components tree and graph:
This is the HTML template of my custom component (DocumentsList):
<div class="container-fluid" style="margin-top: 10px">
<div class="table-row header">
<div class="column index">#</div>
<div class="wrapper attributes">
<div class="wrapper title-comment-module-reporter">
<div class="wrapper title-comment">
<div class="column title">Title</div>
<div class="column comment">Comment</div>
</div>
<div class="wrapper module-reporter">
<div class="column module">Module</div>
<div class="column reporter">Reporter</div>
</div>
</div>
<div class="wrapper status-owner-severity">
<div class="wrapper status-owner">
<div class="column status">Status</div>
<div class="column owner">Owner</div>
</div>
<div class="column severity">Severity</div>
</div>
</div>
<div class="wrapper icons">
<div title="Watch" class="column watch">
<span class="glyphicon glyphicon-eye-open"></span>
</div>
<div title="Add comment" class="column add-comment">
<span class="glyphicon glyphicon-comment"></span>
</div>
</div>
<div class="wrapper dates">
<div class="column date">Created</div>
<div class="column date">Updated</div>
<div class="column date">Due</div>
</div>
</div>
<div *ngFor="let document of docs" class="table-row">
<div class="column index">{{document.id}}</div>
<div class="wrapper attributes">
<div class="wrapper title-comment-module-reporter">
<div class="wrapper title-comment">
<div class="column title">{{document.title}}</div>
<div class="column comment">{{document.comment}}</div>
</div>
<div class="wrapper module-reporter">
<div class="column module">{{document.module}}</div>
<div class="column reporter">{{document.reporter}}</div>
</div>
</div>
<div class="wrapper status-owner-severity">
<div class="wrapper status-owner">
<div class="column status"><span class="label" [ngClass]="{'Open': 'label-primary', 'In Progress': 'label-success'}[document.status]">{{document.status}}</span></div>
<div class="column owner">{{document.owner}}</div>
</div>
<div class="column severity high">{document.severity}}</div>
</div>
</div>
<div class="wrapper icons">
<div class="column watch"><span class="glyphicon glyphicon-eye-open" [class.active]="document.watched"></span></div>
<div class="column add-comment"><span class="glyphicon glyphicon-comment" [class.active]="document.commented"></span></div>
</div>
<div class="wrapper dates">
<div class="column date">{{document.created}}</div>
<div class="column date">{{document.updated}}</div>
<div class="column date">{{document.due}}</div>
</div>
</div>
</div>
Since the span in the graph is child of a div in which there is a *ngFor directive, shouldn't it be displayed in the graph as the parent of the span?
I think the graph should've looked like this (images created with Paint)
Short answer, yes. It makes sense.

Render Partial View in Layout MVC 4 with Caching and Data

I'm new to MVC.NET and I'm working with MVC4 .
In a project I should return System Information like Post count, User counts and etc. In My _Layout I have defined a Sidebar which will show these data.
here is my sidebar div
<div class="sideElement">
<div class="header">
مشخصات سیستم
</div>
<div class="body">
#{Html.Action("GatherSystemInfo","Home").ToString();}
</div>
</div>
My GatherSystemInfo Action is Like This :
Of Course I have used static data For testing
[OutputCache(Duration=900, VaryByParam = "none")]
[ChildActionOnly]
public ActionResult GatherSystemInfo(SystemInformation model = null)
{
model.QuestionCount = 10930;
model.ToturialCount = 10353;
model.UserCount = 120123;
model.ProjectsNumber = 10231;
model.DateTime = DateTime.Now.ToLongTimeString();
model.OnlineUsers = 28;
return PartialView("_SystemInfo", model);
}
And My Partial View Is like this:
#model Persiangeeks.Models.SystemInformation
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد سوالات ثبت شده : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.QuestionCount)</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد آموزشهای موجود : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.ToturialCount)</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد پروژه های موجود : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.ProjectsNumber)</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد کاربران ثبت شده : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.UserCount) نفر</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد کاربران آنلاین : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.OnlineUsers) نفر</div>
<div class="clear" ></div>
</div>
But When I run my project I have Nothing to show in my sidebar div. here is the Image
Thank you for your help :)
Html.Action() – Outputs string
Html.RenderAction() – Renders directly to response\
Solution : use RenderAction() in place of Action()

Unobtrusive ajax - View being fully replaced by partial view

I have searched multiple examples and have tried several things to get this to work properly. Currently I am trying to get a div to be replaced by using an Ajax.HtmlLink pointing to a PartialViewResult method.
I have tried
Checking network and javascript consoles to make sure my .js are all being loaded
Unbundling the stock jqueryval bundle and including the scripts manually
Double checked I didn't have another DOM element with the same update target Id
Checked my ajaxoptions to ensure everything was correct
I know unobtrusive is working because my Email duplicate remote validation check is working.
I will now show some screen shots of my debugging process
Here is what the page looks like prior to ajax load
My goal is to replace the container on the right that is currently holding email, name, and zipcode values
Here is the result##
This is what my view code looks like
<div class="container">
<br />
<br />
<div class="row">
<div class="col-sm-3">
<div class="sidebar-account">
<div class="row ">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">My Account</div>
<div class="panel-body">
<ul class="nav">
<li>
<a class="active" href="account_dashboard.html">Me</a>
</li>
<li>
#Ajax.ActionLink("Alerts", "ManageAlerts", null, new AjaxOptions
{
HttpMethod = "GET",
UpdateTargetId = "ajax-update",
InsertionMode = InsertionMode.Replace,
OnBegin = "ajaxBegin",
OnFailure = "ajaxFail",
OnComplete = "ajaxComplete",
OnSuccess = "ajaxSuccess"
}, new { #class = "active" })
</li>
<li>
<a class="active" href="account_account.html">Linked Accounts</a>
</li>
<li>
<a class="active" href="account_ads.html">Manage ads</a>
</li>
<li>
<a class="active" href="account_ad_create.html">Create new ad</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="row hidden-xs">
<div class="col-lg-12">
<div class="well">
<div class="row ">
<div class="col-lg-3">
<img src="css/images/icons/Crest.png" width="45" />
</div>
<div class="col-lg-9">
<h4 style="margin-top: 0">Increase visibility</h4>
<p>Don't forget to 'bump' your listing to gain more visibility</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-9">
<div id="ajax-update">
#Html.Partial("_ManageUserAccount", Model)
</div>
</div>
<br />
</div>
Partial View Code##
<div id="fadein-div">
<div class="panel panel-default">
<div class="panel-heading">#Model.Username : Alerts</div>
<div class="panel-body">
<br />
<div class="row">
<div class="col-sm-12">
<p><span style="font-weight:bold">Change your alerts!</span></p>
</div>
</div>
<br />
#using (Html.BeginForm("ManageAlerts", "Account", FormMethod.Post, new { #class = "form-vertical", role = "form" }))
{
#Html.AntiForgeryToken()
<fieldset>
<div class="row">
<div class="col-sm-12 ">
<div class="form-group">
<div class="row">
<div class="col-sm-2">
<div class="checkbox">
#Html.CheckBoxFor(P => P.EmailAlertsOn) #Html.LabelFor(P => P.EmailAlertsOn)
</div>
</div>
</div>
</div>
<div id="control-div">
<div class="form-group">
<h3>Email alert options</h3>
</div>
<div class="form-group">
<div class="checkbox">
#Html.CheckBoxFor(P => P.EmailOnNewMessageOn) #Html.LabelFor(P => P.EmailOnNewMessageOn)
</div>
<div class="text-danger">
#Html.ValidationMessageFor(P => P.EmailOnNewMessageOn)
</div>
</div>
<div class="form-group">
<div class="checkbox">
#Html.CheckBoxFor(P => P.EmailOnReplyOn) #Html.LabelFor(P => P.EmailOnReplyOn)
</div>
<div class="text-danger">
#Html.ValidationMessageFor(P => P.EmailOnReplyOn)
</div>
</div>
<div class="form-group">
<div class="checkbox">
#Html.CheckBoxFor(P => P.EmailOnAccountUpdateOn) #Html.LabelFor(P => P.EmailOnAccountUpdateOn)
</div>
<div class="text-danger">
#Html.ValidationMessageFor(P => P.EmailOnAccountUpdateOn)
</div>
</div>
</div>
<br />
<button type="submit" class="btn btn-primary">Save details</button>
</div>
</div>
</fieldset>
}
</div>
</div>
And finally, controller code##
[HttpGet]
public PartialViewResult ManageAlerts()
{
var user = UserManager.FindById(Guid.Parse(User.Identity.GetUserId()));
Models.Account.ManageAlertsViewModel vm = new ManageAlertsViewModel();
vm.Username = user.UserName;
vm.EmailAlertsOn = user.EmailAlertsOn;
vm.EmailOnAccountUpdateOn = user.EmailOnAccountUpdateOn;
vm.EmailOnNewMessageOn = user.EmailOnNewMessageOn;
vm.EmailOnReplyOn = user.EmailOnReplyOn;
return PartialView("_ManageAlerts", vm);
}
<!-- js library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/bootstrap.min.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/jquery.icheck.min.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/waypoints.min.js"></script>
<!-- authenty js -->
<script src="~/Themes/Authenticity%20Forms/Authenty/js/authenty.js"></script>
<!-- preview scripts -->
<script src="~/Themes/Authenticity%20Forms/Authenty/js/preview/jquery.malihu.PageScroll2id.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/preview/jquery.address-1.6.min.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/preview/scrollTo.min.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/preview/init.js"></script>
#Scripts.Render("~/bundles/jqueryval")

Resources