How to create rows dynamically with razor - html

I'm trying to create rows dynamically with razor but I can't figure out how to wrap this around. What I want is for every 5:e object in my model I want to create a new row/div <div class="row pics_in_a_row"> so that each row contains five or less images.
<section class="slice">
<div class="wp-section">
<div class="container">
<div class="row pics_in_a_row">
#{
int i = 0;
foreach (dbphoto.Models.Image s in Model.images.OrderByDescending(x => x.idimage))
{
if (i % 5 == 0 && i != 0)
{
<br />
}
i++;
if (1 == 1)
{
<div class="flxbox" style="flex: calc(1024/713)">
<a href="#s.HighResolution" data-fancybox="gallery" data-caption="xyz, Stockholm">
<img src="#s.LowResolution" class="img-fluid rounded flximg" />
</a>
</div>
}
}
}
</div>
</div>
</div>
</section>
Bonus question: how can I get the height and width of the image and add it to the calc() css?

You should add the div with class row pics_in_a_row inside your loop
This should create a a row for every 5 items (or less for the last row when the remaining items are less than 5).
<div class="container">
#{
var numberOfColsNeeded = 5;
var totalCounter = Model.images.Count();
var itemCounter = 1;
}
#foreach (var item in Model.images)
{
if (itemCounter % numberOfColsNeeded == 1)
{
#:<div class="row pics_in_a_row">
}
<div class="flxbox" style="flex: calc(1024 / 713)">
<img src="#item.LowResolution" class="img-fluid rounded flximg" />
</div>
if ((itemCounter % numberOfColsNeeded == 0) || ((itemCounter) == totalCounter))
{
#:</div>
}
itemCounter++;
}
</div>

Related

Delete an item(Object) from an array using ng-if

I am trying to remove item using ng-if and ng-else-if.
"ng-if="navItem.gbn.indexOf('12345') !== -1" is true then array item only show contains('12345') items.
but the item still remain in array so I think ng-else-if is not working.
I don't know how to delete it.
I have the following Angular code:
<div ng-controller="PIDAEController">
<div class="container-fluid" style="padding-top:30px;">
<div class="row">
<div class="col-lg-2 col-md-3 col-xs-6 tubmbimg" ng-repeat="navItem in navItems | Fpage: curPage * pageSize | limitTo:pageSize" ng-if="navItem.gbn.indexOf('12345') !== -1">
<a class="thumbnail border" href="#/item/{{navItem.site}}">
<div class="b-marjin">
<span align="center" class="label label-primary full-width" >{{ navItem.gbn }}</span>
</div>
<div>
<img style="border: 1px solid black;" class="img-responsive full-width" src="{{navItem.pimg}}" alt="">
</div>
<div align="center" class="under-block">
<span title="{{ navItem.pname }}" align="center">{{ navItem.pname2}}</span>
</div>
</a>
</div>
<div ng-else-if="navItems.splice(navItem, 1)"></div>
</div>
</div>
</div>
angular.forEach(item, function (value, key) {
var keepGoing = true;
if(keepGoing) {
if(value.gbn.indexOf("11600") != -1) {
$scope.initialized = true;
$scope.navItems = item;
$scope.curPage=0;
$scope.pageSize=20;
$scope.numberOfPage = Math.ceil(item.length / $scope.pageSize);
keepGoing = false;
}
else {
$scope.navItems.splice(key, 1);
}
}
});
ng-else-if does NOT EXIST. its neither angular or angularJS.
In the line
<div ng-else-if="navItems.splice(navItem, 1)"></div>
splice requires navItem to be an index/int, not an object.
As an alternative to ng-else-if you can simply do:
ng-if="navItem.gbn.indexOf('12345') == -1
Also, splice would return the removed array item when found, not sure if "ng-if" handles that as being TRUE.

Asp.net Core Dynamically inserting HTML elements in a specific position based on model values

I have figured out a way to dynamically insert child elements into a specific parent div element based on the status property from the model. The problem with my version is that I am using 4 loops to achieve this. Is there a better way to achieve this? like 1 loop.
Here is my code:
<div class="col-lg-3">
#foreach (var item in Model)
{
if (item.Status == OrderStatus.New)
{
<div class="order"></div>
}
}
</div>
<div class="col-lg-3">
#foreach (var item in Model)
{
if (item.Status == OrderStatus.Preparing)
{
<div class="order"></div>
}
}
</div>
<div class="col-lg-3">
#foreach (var item in Model)
{
if (item.Status == OrderStatus.Delivering)
{
<div class="order"></div>
}
}
</div>
<div class="col-lg-3">
#foreach (var item in Model)
{
if (item.Status == OrderStatus.Delivered)
{
<div class="order"></div>
}
}
</div>
</div>
As you can see that I am adding a bunch of div tags into one of the 4 columns based on the status. I am using a loop for each one to achieve this.
Just take this loop outside of this elements and check if item has expected status. Is this what are you looking for?
#foreach(var item in Model)
{
<div class="col-lg-3">
if (item.Status == OrderStatus.New)
{
<div class="order"></div>
}
</div>
<div class="col-lg-3">
if (item.Status == OrderStatus.Preparing)
{
<div class="order"></div>
}
</div>
<div class="col-lg-3">
if (item.Status == OrderStatus.Delivering)
{
<div class="order"></div>
}
</div>
<div class="col-lg-3">
if (item.Status == OrderStatus.Delivered)
{
<div class="order"></div>
}
</div>
}
</div>
Edited version:
#foreach(var item in Model)
{
if (item.Status == OrderStatus.New)
{
<div class="col-lg-3">
<div class="order"></div>
</div>
}
if (item.Status == OrderStatus.Preparing)
{
<div class="col-lg-3">
<div class="order"></div>
</div>
}
if (item.Status == OrderStatus.Delivering)
{
<div class="col-lg-3">
<div class="order"></div>
</div>
}
if (item.Status == OrderStatus.Delivered)
{
<div class="col-lg-3">
<div class="order"></div>
</div>
}
}
</div>
Add break to your consition like below:
<div class="col-lg-3">
#foreach (var item in Model)
{
if (item.Status == OrderStatus.New)
{
<div class="order"></div>
break;
}
}
</div>
<div class="col-lg-3">
#foreach (var item in Model)
{
if (item.Status == OrderStatus.Preparing)
{
<div class="order"></div>
break;
}
}
</div>
<div class="col-lg-3">
#foreach (var item in Model)
{
if (item.Status == OrderStatus.Delivering)
{
<div class="order"></div>
break;
}
}
</div>
<div class="col-lg-3">
#foreach (var item in Model)
{
if (item.Status == OrderStatus.Delivered)
{
<div class="order"></div>
break;
}
}
</div>

How to use two if statement into foreach in razor?

I tried to use two if statements into foreach such as this but I get this error:
Encountered end tag "div" with no matching start tag. Are your
start/end tags properly balanced?
I want this to be the condition: If the variable i is smaller than the one I want to be the part of this code inside a div element with row class.
<text>
<div class="col-xs-2 col-wrapper">
<div class="image-wrapper">
<img src="#Url.Content(path + item.PhotoName)" alt="" />
<img class="delimg" src="~/Content/Adminex/images/delete-icons.png" id="#item.Id" />
</div>
</div>
</text>
.Please advice,
#{
string path = System.Configuration.ConfigurationManager.AppSettings["ImageEdit"];
int i = 0;
}
#foreach (var item in Model.PhotoTables)
{
if (i == 7)
{
i = 0;
}
if (i < 1)
{
#:<div class="row">
}
<text>
<div class="col-xs-2 col-wrapper">
<div class="image-wrapper">
<img src="#Url.Content(path + item.PhotoName)" alt="" />
<img class="delimg" src="~/Content/Adminex/images/delete-icons.png" id="#item.Id" />
</div>
</div>
</text>
if (i < 1)
{
#:</div>
}
i++;
}
That </div> above i++ looks suspiciously lonesome.
EDIT:
Now I think I understand, you want 7 text items each in each div row (with any remainder in the last row). Here's one of many ways..
#foreach (var item in Model.PhotoTables) {
if (i < 1) {
#:<div class="row">
}
<text>
...item...
</text>
i++;
if (i == 7) {
i = 0;
#:</div>
}
}
if (i > 0) {
#:</div>
}
That produces this, if that's what you want..
<div class="row">
<text>..1..</text>
<text>..2..</text>
<text>..3..</text>
<text>..4..</text>
<text>..5..</text>
<text>..6..</text>
<text>..7..</text>
</div>
<div class="row">
<text>..8..</text>
</div>
If I am not wrong, you want to wrap text tag in <div class="row"> when i < 1. So you can try this. It works for me.
#{
string path = System.Configuration.ConfigurationManager.AppSettings["ImageEdit"];
int i = 0;
}
#foreach (var item in Model.PhotoTables)
{
if (i == 7)
{
i = 0;
}
if (i < 1)
{
<div class="row">
<text>
<div class="col-xs-2 col-wrapper">
<div class="image-wrapper">
<img src="#Url.Content(path + item.PhotoName)" alt="" />
<img class="delimg" src="~/Content/Adminex/images/delete-icons.png" id="#item.Id" />
</div>
</div>
</text>
</div>
}
else
{
<text>
<div class="col-xs-2 col-wrapper">
<div class="image-wrapper">
<img src="#Url.Content(path + item.PhotoName)" alt="" />
<img class="delimg" src="~/Content/Adminex/images/delete-icons.png" id="#item.Id" />
</div>
</div>
</text>
}
i++;
}
I think this will do the trick. use the modulus operator to find out if you are at the beginning or at the end of counting to 7 and if the items are not up to 7 the closing div will still be in place
#{
string path = System.Configuration.ConfigurationManager.AppSettings["ImageEdit"];
int i = 0;
int modelCount = Model.PhotoTables.Count();
}
#foreach (var item in Model.PhotoTables)
{
//if (i == 7)
// {
// i = 0;
// }
if (i % 6 == 0)
{
#:<div class="row">
}
<text>
<div class="col-xs-2 col-wrapper">
<div class="image-wrapper">
<img src="#Url.Content(path + item.PhotoName)" alt="" />
<img class="delimg" src="~/Content/Adminex/images/delete-icons.png" id="#item.Id" />
</div>
</div>
</text>
if (i % 6 == 5 || i == modelCount - 1)
{
#:</div>
}
i++;
}

Have images in 3 wrapper divs but need to view only one div at a time. too slow loading

I have 3 sets of images in 3 separate divs that are hard coded into the webpage. Only one set is viewed at a time by user choice button. This was ok when there were only 5 images in each set but now that I added more, the page is loading so slowly. The pictures are 1200 by 900 px because I am using nivo-slider which allows sizing.
Is there a way to load only the picture set that is clicked?
<div class="slider-wrapper theme-default" id="wrapper1" >
<div class="nivoSlider" id="c1" >
<img src="images/Germany2008/GermanyTrip01.jpg" alt="" />
<img src="images/Germany2008/GermanyTrip02.jpg" alt="" />
<!--etc. -->
</div>
</div>
<div class="slider-wrapper theme-default" id="wrapper2" >
<div class="nivoSlider" id="c2">
<img src="images/Germany2008/GermanyTrip01.jpg" alt="" />
<img src="images/Germany2008/GermanyTrip02.jpg" alt="" />
<!--etc. -->
</div>
</div>
Thank you.
I'm not perfectly clear on what you want but I'll try.
Add the following javascript at the bottom of the body:
var wrapperOneImages = ["path/to/image","path/to/image","path/to/image"];
var wrapperTwoImages = ["path/to/image","path/to/image","path/to/image"];
var amountOfWrappers = 2;
for (i=0; i < amountOfWrappers; i++) {
var nivoSlider = document.createElement("div");
nivoSlider.setAttribute("class", "nivoSlider");
nivoSlider.setAttribute("id", "c" + toString(i+1));
var wrapper = document.getElementById("wrapper" + toString(i+1));
if (i == 0) {
var whatWrapper = wrapperOneImages;
} else if (i == 1) {
var whatWrapper = wrapperTwoImages;
}
for (j=0; j < whatWrapper.length; j++) {
var img = document.createElement("img");
var img.setAttribute("src", whatWrapper[j]);
nivoSlider.appendChild(img);
}
wrapper.appendChild(nivoSlider);
}
And replace all of the html you provided with the following:
<div class="slider-wrapper theme-default" id="wrapper1" ></div>
<div class="slider-wrapper theme-default" id="wrapper2" ></div>
I haven't tested it but it should work.

Boostrap responsive grid issue with different images size

this is my Boostrap grid:
http://i59.tinypic.com/nozn12.png
Each image has different sizes, unfortunately the heightest one wraps the row leaving an empty space.
I should give the same size in each image, but I can't because this isn't a fixed layout.
Is there any fix / solution to this issue? Here's the code
<div class="row">
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="temp/1.jpg" class="img-responsive">
<h4>4280 / 5PL</h4>
</div>
</div>
<div class="col-sm-6 col-md-3">
ecc.
</div>
<div class="col-sm-6 col-md-3">
ecc.
</div>
</div>
use equal height for <div class="col-sm-6 col-md-3">
$.fn.eqHeights = function(options) {
var defaults = {
child: false
};
var options = $.extend(defaults, options);
var el = $(this);
if (el.length > 0 && !el.data('eqHeights')) {
$(window).bind('resize.eqHeights', function() {
el.eqHeights();
});
el.data('eqHeights', true);
}
if( options.child && options.child.length > 0 ){
var elmtns = $(options.child, this);
} else {
var elmtns = $(this).children();
}
var prevTop = 0;
var max_height = 0;
var elements = [];
elmtns.height('auto').each(function() {
var thisTop = this.offsetTop;
if (prevTop > 0 && prevTop != thisTop) {
$(elements).height(max_height);
max_height = $(this).height();
elements = [];
}
max_height = Math.max(max_height, $(this).height());
prevTop = this.offsetTop;
elements.push(this);
});
$(elements).height(max_height);
};
// run on load so it gets the size:
// can't have the same pattern for some reason or it scans the page and makes all the same height. Each row should be separate but it doesn't work that way.
$(window).load(function() {
//$('[class*="eq-"]').eqHeights();
$('.foo [class*="eq-"]').eqHeights();
$('.foo2 [class*="eq-"]').eqHeights();
});
use <div class="row foo"> for <div class="row">
and add eq-col-sm-6 eq-col-md-3 class to all your divs with col-sm-6 col-md-3
here DEMO