Bootstrap grid layout issue when populating with items - html

I'm new to MVC5 and especially to using Bootstrap. I'm trying to create a website in which the first page should feature a grid with student images. Right now it looks like this. I want to have 5 images per row, but I haven't manage to figure out how(it was either 4, or 6).
Also when there is no space between the rows and the images stuck together.
I am populating the grid with items from my Student Model.
<div class="col-md-10">
<div class="row">
#foreach (var item in Model.Students)
{
<div class="col-md-2">
<a href="#Url.Action("Edit", "Students", new {studentId = item.StudentId})">
<img src="../../#item.ProfileImagePath" alt="Profile Image" />
</a>
</div>
}
</div>
</div>
I didn't fully understand how does the bootstrap grid work, especially when I'm populating it with dynamic data. The website looks like this now
http://imgur.com/5fLRZeM

You have
<div class="row">
#foreach (var item in Model.Students)
{
<div class="col-md-2">
...
</div>
}
</div>
Which generates one long <row>. But there is no auto-wrapping, you'll have to break this up into rows yourself.
Assuming that Students is a List<>
// roughly, untested
#for(int r = 0; r < Model.Students.Count; r += 5)
{
<div class="row">
#for (int s = r; s < r+5; s += 1)
{
<div class="col-md-2">
// do stuff with Model.Students[s]
</div>
}
</div>
}

Related

html merge 2 columns in 1 with 100% width bootstrap 2

I have the following HTML markup in my .NET MVC project:
<div class="row">
<div class="span6">#Model.Data</div>
<div class="span6">#Model.OtherData</div>
</div>
I'm getting data from server. So I want to do the following:
If data is empty then show other data with width = 100%.
Just to clarify I want to do something like that:
<div class="row">
<div class="span12">#Model.OtherData</div>
</div>
Or vice versa.
Is there a way to do that? Maybe with using different HTML tags / CSS classes.
Essentially you just want conditionally display the #Model.Data only if it isn't null. You can also set the col class with a variable, and conditionally change that variable depending if #Model.Data exists or not. Try something like this:
# {
var colClass = 'span6';
if (#Model.Data == null) {
colClass = 'span12';
}
}
<div class="row">
#if (#Model.Data != null) {
<div class="#colClass">#Model.Data</div>
}
<div class="#colClass">#Model.OtherData</div>
</div>

How to create two rows with three columns using mustache.js as a logic-less template system?

I'm using mustache.js as my template system, I'm not very experienced with logic-less
templates so I decided to ask you this question.
I have the following html structure, two rows with 3 columns each, like so:
<div class="content-wrapper">
<div class="grid-row">
<div class="column-third">
<p>Text</p>
</div>
<div class="column-third">
<p>Text</p>
</div>
<div class="column-third">
<p>Text</p>
</div>
</div>
<div class="grid-row">
<div class="column-third">
<p>Text</p>
</div>
<div class="column-third">
<p>Text</p>
</div>
<div class="column-third">
<p>Text</p>
</div>
</div>
</div>
What I have at the moment on my template is the following
<div class="content-wrapper">
<div class="grid-row">
{{#contentListToRender}}
<div class="column-third">
<p>{{title}}</p>
</div>
{{/contentListToRender}}
</div>
</div>
But as you can already guess that will fill the row with as many elements (columns)
I have in contentListToRender.
I was wondering if there's a way of doing this and still keep the html structure,
or I should start thinking of chaning the html structure and use CSS to style the appearance?
Thank you for your help,
David
Edit, followed pawel comment.
I assume you mentioned to do something like this?
<div class="content-wrapper">
{{#contentListToRender}}
{{#rows}}
<div class="grid-row">
{{#columns}}
<div class="column-third">
<p>{{title}}</p>
</div>
{{/columns}}
</div>
{{/rows}}
{{/contentListToRender}}
</div>
Being my contentListToRender composed this way:
var contentListToRender = function() {
return {rows: [ { columns: [column, column, column] }, {columns: [column, column, column]} ]}
}
Would that be an acceptable way of accomplish this?
The following is the solution I build for this, in case someone ends up in the same place as me.
<div class="content-wrapper">
{{#contentListToRender}}
{{#rows}}
<div class="grid-row">
{{#columns}}
<div class="column-third">
<p>{{title}}</p>
</div>
{{/columns}}
</div>
{{/rows}}
{{/contentListToRender}}
</div>
And the function to split the elements into rows and columns
var contentListToRender = function(elements) {
var rows = { rows: [] };
var columns = [];
for (var i = 0; i < elements.length; i++) {
var base_path = elements[i].base_path;
var title = elements[i].title;
var column = {base_path: base_path, title: title};
if ((columns.length > 0) && (columns.length % 3 == 0)) {
// when the columns array has reached 3 elements
// then add those columns to the rows
rows.rows.push({columns: columns});
// Then clear the columns array
// and push the current column to the columns array
columns = [];
columns.push(column);
} else {
columns.push(column);
}
}
// push the last columns to the last row
rows.rows.push({columns: columns});
return rows;
}

Bootstrap - How can I move a row's div into another row?

I'm learing bootstrap and I'm working with the Grid System since I'm working to a responsive website with lots of tables.
<div class="row">
<div class="col-md-1">A</div>
<div class="col-md-1">B</div>
<div class="col-md-1">C</div>
</div>
<div class="row">
<div class="col-md-1">D</div>
<div class="col-md-1">E</div>
<div class="col-md-1">F</div>
</div>
I will start with this bootstrap "table". I want to move a div of the first row into the second row. Example:
<div class="row">
<div class="col-md-1">A</div>
<div class="col-md-1">B</div>
</div>
<div class="row">
<div class="col-md-1">C</div>
<div class="col-md-1">D</div>
<div class="col-md-1">E</div>
<div class="col-md-1">F</div>
</div>
I think you are misunderstanding how Bootstrap works. Its grid does not work by "moving a row's div into another row". What happens is that the grid is based off of 12 columns per row. If you want 3 elements in 1 row, you would set each one with a class of col-xx-4 (xx is either lg, md, sm or xs), since 12/4 = 3. Each element takes up 4 of the 12 columns.
For your issue at hand - you can put all 6 elements in the same row and adjust the number of columns each one spans based on the screen size using lg, md, sm and xs. Below is an example that does what I believe you are trying to do my manually moving a row's div, but this way utilizes Bootstrap's grid system correctly.
<div class="container">
<div class="row">
<div class="col-sm-4 col-xs-6">
A
</div>
<div class="col-sm-4 col-xs-6">
B
</div>
<div class="col-sm-4 col-xs-6">
C
</div>
<div class="col-sm-4 col-xs-6">
D
</div>
<div class="col-sm-4 col-xs-6">
E
</div>
<div class="col-sm-4 col-xs-6">
F
</div>
</div>
</div>
Here is a Demo JSFiddle
You can slide the screen to make it bigger/smaller and see the 2 rows of 3 collapse to 3 rows of 2, and vice-versa.
If I got what you wanted to ask, the simplest way to do it is by:
1) Checking the browser's width.
2) Then, get the inner element you want to move.
3) Add it to where you want to take it.
4) And remove it from it was before.
The code HTML:
<div class="row" id="row1">
<div class="col-md-1">
A
</div>
<div class="col-md-1">
B
</div>
<div class="col-md-1" id="moveto">
</div>
</div>
<div class="row" id="row2">
<h4>--</h4>
<div class="col-md-1" id="movefrom">
C
</div>
<div class="col-md-1">
D
</div>
<div class="col-md-1">
E
</div>
<div class="col-md-1">
F
</div>
</div>
The JS code:
$(document).ready(function() {
var x = document.getElementById("movefrom").innerHTML // Get the inner HTML (C in this case)
if (window.innerWidth <= 768) { // Check the browser's width
document.getElementById("moveto").innerHTML = x; // Move the element to where you want (moveto id)
document.getElementById("movefrom").innerHTML = "" // Replace the element by an empty string.
}
});
This was the most simple way I found to make it work.
Any feedback is great!
Good luck
It is my hobby to answer too late.
Do this if you want it to respond to resize and work both ways:
window.addEventListener("resize", function(event) {
//grab what to replace
var x = document.getElementById("social")
.innerHTML;
//for when going to smaller screen
if (window.innerWidth <= 560 && !x == "") {
document.getElementById("social-mobile")
.innerHTML = x;
document.getElementById("social")
.innerHTML = "";
}
var y = document.getElementById("social")
.innerHTML;
// if we are at bigger screen and node is "", then put it back
if (window.innerWidth >= 560 && y == "") {
document.getElementById("social")
.innerHTML = document.getElementById("social-mobile")
.innerHTML;
document.getElementById("social-mobile")
.innerHTML = "";
}
});
😎
Well, I have this simple jQuery function..
(function($){
$.fn.moveTo = function(selector){
return this.each(function(){
var cl = $(this).clone();
$(cl).prependTo(selector);
$(this).remove();
});
};
});
usage $('.element').moveTo('.element2');, you can also change it to appendTo, or send a parameter to change it.

insert dynamic 6 images into 2 lines via bootstrap

i try without luck to insert 6 images into 2 lines,
the problem is that the line number 2 break the last 2 images and push them down.
i'm using Umbraco and bootstrap 3.
here is my code
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = "InnerPage.cshtml";
}
#section slider
{
#{Html.RenderPartial("ImageSlider");}
}
#{
<!-- PROGRAM PAGE -->
<script src="js/script.js"></script>
<div class="caption-button caption-gray left-top" style="font-size:14px;">UPCOMING EVENTS</div>
<div class="padding-top-60">
<div class="row row-lg-height">
<div id="eventCarousel" class="carousel slide vertical" >
<div class="carousel-inner">
#{
var content = Umbraco.Content(1122);
int itemsCount = content.Children.Count();
int sliders = itemsCount / 6;
for (int i = 0; i <= sliders; i++)
{
var items = content.Children;
if (i == 0)
{
#Html.Raw("<div class=\"item active\">")
items = content.Children.Take(6);
}
else
{
items = content.Children.Skip(i * 6).Take(6);
#Html.Raw("<div class=\"item\">")
}
foreach (var childContent in items)
{
var contentService = ApplicationContext.Current.Services.ContentService.GetById(int.Parse(childContent.Id.ToString()));
var title = childContent.Name.ToString();
var image = Umbraco.Media(contentService.Properties["Thumbnail"].Value.ToString());
var description = contentService.Properties["shortDescription"].Value.ToString();
var img = image.Url.ToString();
<div class="col-lg-4">
<img src="#image.Url" class="media-object pull-left img-responsive" />
<h1 class="media-heading" style="color:#606060;">#title</h1>
<div style="padding:0px 5px; ">#MvcHtmlString.Create(#description)</div>
</div>
}
#Html.Raw("</div>")
}
}
</div>
</div>
</div>
</div>
<a class="caption-button caption-red right-bottom" href="#eventCarousel" data-slide="next">MORE</a>
}
i attached 2 images so you can see what firebug showing to me and what happened on the screen.
PLEASE HELP ME ! I am breaking my head 2 days already and it's wasting my time..
What do i do wrong ??
I think your code is a littlebit difficult. If you use another approach you will be able to debug much easier.
Try some approach like this:
#{
var allNodesToLoop = Umbraco.Content(1122).Children;
}
#foreach(var nodegroup in allNodesToLoop.InGroupsOf(2) {
<div class="row">
#foreach(var item in nodegroup) {
<div class="col-md-4">
#item.Name
<!-- and other stuff you want to render in this grid cell -->
</div>
}
</div>
}

MVC3 looping through the data & displaying in correct format

I am looping through the data and I want to show 3 items per line, each item in a div.
Without knowing how many items the model currently contains.
#foreach (var item in Model)
{
<div class="content">
#item.StudentName
</div>
<div>
#item.StudentId
</div>
}
Please provide example, thanks!
#{int i = 1;}
#foreach (var item in Model)
{
<div style="float:left;">
<div class="content">
#item.StudentName
</div>
<div>
#item.StudentId
</div>
</div>
i++;
if(i%3==0)
{
<div style="clear:both;"></div>
}
}
Use a for loop instead of a foreach, then when your index is divisible by 3 add a new row