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

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.

Related

Is there a way to align div elements in rows to be in a specific column using bootstrap?

I am using bootstrap for the first time. Im just finishing up this form, but I want to add a button at the bottom right of form. I've got the button where I want it to be vertically, but right now its at the bottom left of my form, not the right. The only solution I could find is to add 3 empty divs, and then my button as the fourth div. This causes it to be in the 4th column. I was wondering if there was a way to achieve this without using unnecesarry code.
Im fairly new to stackoverflow, I apologize if my question is missing something or isn't clear
Here's my code for that row so far:
Note: parent row is defined using col-md-12.
<div class="row" style="padding-top: 3px">
<div class="col-md-3">
</div>
<div class="col-md-3">
</div>
<div class="col-md-3">
</div>
<div class="col-md-3">
#Html.WETSubmitButton(btnAdd, new { #class = "btn btn-default", name = "action", value = "Add" })
</div>
</div>
you would just use the offset class like this:
<div class="row pt-1" >
<div class="col-md-3 offset-md-9">
#Html.WETSubmitButton(btnAdd, new { #class = "btn btn-default", name = "action", value = "Add" })
</div>
</div>
also I removed your padding and used the bootstrap built in padding class on the row.

Styling background-color for div When div height changes

I have the follow ASP.Net MVC razor code.
#{
foreach (var note in Model.Notes)
{
<div class="row data-area" style="margin-bottom:5px !important">
<div class="small-2 columns magmalabel" style="background-color:#01466d;">
#Html.DisplayFor(m => note.NoteTypeId)
</div>
<div class="small-10 columns">
#Html.DisplayFor(m => note.Notes)
</div>
</div>
}
}
This works, but occasionally, the second column word wraps due to the length, this is by design, but the first div that displays:
#Html.DisplayFor(m => note.NoteTypeId)
does not get the correct formatting as it does not color the second row.
I know that this is a height issue as so far the only way I have managed to do this is by setting the height property to twice the size in the Chrome console.
<div class="small-2 columns magmalabel" style="background-color:#01466d;height:50px;">
In Chrome, a single height equals 25px's
I have looked on SO and seen many solutions, but so far none has worked.
Had to resort to some jQuery to sort this out.
Added a placeholder class, rowHeading, to the markup as below:
<div class="row data-area" style="margin-bottom:5px !important">
<div class="small-2 columns magmalabel rowHeading" style="background-color:#01466d;">
#Html.DisplayFor(m => note.NoteTypeId)
</div>
<div class="small-10 columns">
#Html.DisplayFor(m => note.Notes)
</div>
</div>
Then simply added the following jQuery script:
$('.rowheading').each(function () {
var parentHeight = $(this).parent().height();
$(this).height(parentHeight);
});
This enabled the browser to set the row height for all divs on the page to correctly format the height.

Conditional layout of multiple divs in Thymeleaf

I am struggling to format this div structure using th:each in Thymleaf.
The desired html format is as follows
Desired HTML
<div class="row interest-featured"> <!--Parent Div -->
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
</div>
<div class="row interest-featured"> <!--Parent Div -->
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
<div class="col-md-4 interest">
</div>
</div>
Progress until now
<div th:each="interest,status : ${interest}" th:class="${(status.index + 1 % 3) == 0}? 'row interest-featured' :''">
<div class="col-md-4 interest">
</div>
</div>
Any help would be highly appreciated. Thanks
PS: HTML texts are removed for brevity
I think I understand the issue here. If you have a flat list and you want to display it in a Bootstrap grid, you'll have to create new rows for each n elements.
The solution to this problem isn't as clean as a normal th:each, but you'll have to use it twice and have proper th:if statements on when you want to display it.
For example, for the .row element, you want to only show it for elements with index 0, 3, 6, ... (if you want a 1/3th column grid). This means you should do:
<div th:each="interest,rowStatus : ${interests}" class="row interest-featured" th:if="${rowStatus.index % 3} == 0">
</div>
Now, for the children of the row you'll have to iterate again over your collection, but filter it so that for the first row you only show elements with index 0-2 for the second row the elements 3-5, ... .
To do that you use another th:if:
<div th:each="interest,rowStatus : ${interests}" class="row interest-featured" th:if="${rowStatus.index % 3} == 0">
<div th:each="interest,interestStatus : ${interests}" class="col-md-4 interest" th:text="${interest}"
th:if="${interestStatus.index lt rowStatus.index + 3 and interestStatus.index ge rowStatus.index}"></div>
</div>
If you don't want to use a cumbersome template like this, you can always just iterate over the column itself. If you're using Bootstrap, it will automatically create columns that do not fit within a row on a new line, for example:
<div class="row">
<div class="col-md-4">Column 1</div>
<div class="col-md-4">Column 2</div>
<div class="col-md-4">Column 3</div>
<div class="col-md-4">Column 4</div><!-- This item will be on a new row because the grid does only allow 12 spaces -->
</div>
However, there are some differences between both approaches when the content of the column is variable in height.
NOTE: You're also using the interest variable twice. The collection is named ${interest}, but the result variable is also called interest. You probably have to rename one of those.
You use object.
html
<div class="source_list" th:each="history : ${historys}">
<label>date : <span th:text="${history.date}">2016.06.27</span></label>
<br />
<label>description : </label><span th:text="${history.description}">2016.06.27</span>
<br /> <br />
</div>
contorller
#RequestMapping(value = "/settings/history/{companyIdx}")
public ModelAndView showHistory(ModelAndView mav, #PathVariable int companyIdx, HttpServletRequest request) {
String language = CookieUtil.getCookieValue(request, "lang");
Company company = companyService.findCompanyByIdx(companyIdx);
List<CompanyHistory> historys = companyService.findHistoryByLanguage(company, language);
mav.addObject("company", company);
mav.addObject("historys", historys);
mav.setViewName("/company/company_settings_history");
return mav;
}
dto
#Entity(name = "company_history")
public class CompanyHistory {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int idx;
#ManyToOne
#JoinColumn(name = "company_id")
private Company company;
private String date;
private String description;
...
}

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 grid layout issue when populating with items

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>
}