Bootstrap grid dynamic columns - angularjs

I have the following table on my page:
<table>
<tr>
<td ng-repeat="Type in Types">
<img ng-src="../../Images/{{Type.TypeImg}}" ng-click="GoNext(Type.TypeId)" alt="{{Type.TypeName}}" />
</td>
</tr>
</table>
I want to use bootstrap grid instead of the table. So if there were 3 images on the page I'd have:
<div class="row">
<div class="col-md-4"> </div>
<div class="col-md-4"> </div>
<div class="col-md-4"> </div>
</div>
But since the number of images is dynamic ( although it is never greater then 12), i cannot use fixed numbers in col-md*. So I'd have to do something like this:
<div class="row" ng-repeat="Type in Types">
<div class="col-md-{{12 / Types.length}}">
<img ng-src="../../Images/{{Type.TypeImg}}" ng-click="GoNext(Type.TypeId)" alt="{{Type.TypeName}}" />
</div>
</div>
But now I have each image in its own row, instead of in a column(one under the another). How do i get them all in one line?

Move ng-repeat to col:
<div class="row">
<div class="col-md-{{width}}" ng-repeat="Type in Types">
{{$index}}
</div>
</div>
Plunk see in full screen mode

Related

ng-repeat changing position left to right

I have one problem with angular ng-repeat and bootstrap. I tried divide view on two parts left image, right description, but next line oposite, left description and right image. I would like something like picture below
<div class="container">
<div class="row">
<div ng-repeat="item in items">
<div class="col-xs-6">
<img src="{{item.image}}" alt="#"/>
</div>
<div class="col-xs-6">
<div class="description">
<p>{{item.description}}</p>
</div>
</div>
</div>
</div>
</div>
You can check for odd or even iteration in the repeat loop and apply your class with either float left or right respectively.
<div class="container">
<div class="row">
<div ng-repeat="item in items" ng-class-odd="'img-left'">
<div class="col-xs-6 img-holder">
<img src=".." alt="#"/>
</div>
<div class="col-xs-6 text-holder">
<div class="description">
<p>........</p>
</div>
</div>
</div>
</div>
</div>
CSS
.img-left .img-holder{
float:left;
}
.img-left .text-holder{
float:right;
}
you can handle even condition directly in CSS or like above.
This is my solution. Using ng-class we check if the current index ($index) divided by 2 is zero then we add the float classes either .left or .right.
JSFiddle Demo
<div ng-controller='MyController'>
<div ng-repeat="val in arr">
<div class="row clearfix">
<div class="col-xs-6" ng-class="$index % 2 === 0 ? 'left':'right'">
{{val}}
</div>
<div class="col-xs-6" ng-class="$index % 2 === 0 ? 'right':'left'">
<img src="http://lorempixel.com/400/200/" alt="">
</div>
</div>
</div>
</div>

Drupal 7 customize view theme

I have the HTML as below:
<div class="top-grids">
<div class="container">
<div class="col-md-4 top-grid text-center">
<div class="top-grid-pic">
<img src="images/pic01.png" title="Boots" />
<span>Boots</span>
</div>
<div class="top-grid-pic-info">
Seeall
</div>
</div>
<div class="col-md-4 top-grid text-center">
<div class="top-grid-pic">
<img src="images/pic02.png" title="Boots" />
<span>Casual</span>
</div>
<div class="top-grid-pic-info">
Seeall
</div>
</div>
<div class="col-md-4 top-grid text-center">
<div class="top-grid-pic">
<img src="images/pic03.png" title="Boots" />
<span>Formal</span>
</div>
<div class="top-grid-pic-info">
Seeall
</div>
</div>
<div class="clearfix"> </div>
</div>
</div>
In Drupal 7, I have created a view (block) and named it "Featured Products". I have added two fields - image and title. Please check the attached screen shot.
After doing the same, I have created a view file named "views-view-field--new-uc-products--block.tpl.php" and write the code as below:
<div class="top-grids">
<div class="container">
<?php print $output; ?>
</div>
and place this under the 'templates' folder.
Now I see the drupal site and the View Source file which contains the below HTML structure:
<div class="view-content">
<table class="views-table cols-0" >
<tbody>
<tr class="odd views-row-first">
<td >
<div class="top-grids">
<div class="container">
<div class="col-md-4 top-grid text-center">
<div class="top-grid-pic"><div class="product-image"><div class="main-product-image"><img typeof="foaf:Image" src="http://localhost/uberdrupal/sites/default/files/styles/uc_product/public/515sRCSoS9L._SX331_BO1%2C204%2C203%2C200_.jpg?itok=4Exqjiv3" alt="" title="" /></div></div>
</div>
</div>
</td>
<td class="views-field views-field-title" >
<div class="top-grids">
<div class="container">
<span>Twisted</span>
</div>
<div class="top-grid-pic-info">
See All
</div>
</div> </div>
</div>
Can Someone tell me how can I customize the view template so that it can show the HTML as I desired?
Thanks
Go to admin/structure/views/settings and have these settings
Then go to your view and under Advanced, you will have Theme:Information. Click it and you will be shown a list of tpl files that you can change the html as you wish.

Angular ng-repeat over 2 rows

I currently have two rows with 2 SPAN on each one.
What I'd like to do is replace these hardcoded DIV and SPAN with an ng-repeat directive.
My problem is either my 4 SPAN go over 4 rows or they all go on the same one.
Is this feasible with Angular.js?
I read stuff on ng-repeat-start and ng-repeat-end but I'm not sure this could help me here.
Here's a Plunker for you: http://plnkr.co/edit/DEf2JSTFDBvDXusJDsX7?p=preview
Thanks!
EDIT:
I used SPAN in my Plunker example for the sake of simplicity but in my real world problem, I'm using a bootstrap grid and I have, in fact, 2 <div class="col-xs-12 col-md-3"> in each <div class="row">. I hope that clarifies things enough.
Very simple with bootstrap:
<div class="row">
<div class="col-xs-6" ng-repeat="i in [1,2,3,4]">
{{i}}
</div>
</div>
You can even set the width of the wrapper div, if you do not want it to be 100%.
Plunker: http://plnkr.co/edit/J0bKs0BeGW7ltqtnQbNC?p=preview
It is better to use $even and $odd property of ng-repeat.
<div class="row">
<span ng-repeat="i in [1,2,3,4,5,6]">{{i}}
<br ng-if="$odd">
</span>
</div>
OR,
<div class="row">
<span ng-repeat="i in [1,2,3,4,5,6]">{{i}}
<br ng-if="$index%2===1">
</span>
</div>
Not nice but works
<div class="row" ng-repeat="i in [1,2,3,4]" ng-if="$even"><span>{{i}}</span><span>{{i+1}}</span></div>
Solution depends on what u want achieve
Another option is to organize your data not in flat array but as array of arrays
[
[1,2],
[3,4],
[5],
]
And make ng-repeat in ng-repeat
<div class="row" ng-repeat="i in myArray">
<span ng-repeat="j in i">{{j}}</span>
</div>
If your html looks like:
<div class="row">
<div class="col-md-3 col-xs-12">
<span>1</span>
<span>2</span>
</div>
</div>
<div class="row">
<div class="col-md-3 col-xs-12">
<span>3</span>
<span>4</span>
</div>
</div>
Then you can consider doing this with ng-repeat[no <br> needed]
<div class="row">
<div class="col-md-3 col-xs-12" ng-repeat="i in [1,3]">
<span >{{i}}</span>
<span >{{i+1}}</span>
</div>
</div>
Hope it solves your purpose.

Create Row every after 2 item in Angular ng-repeat - Ionic Grid

I need to create a strcuture as below in my app through ng-repeat.
<div class="row">
<div class="col-50">1</div>
<div class="col-50">2</div>
</div>
<div class="row">
<div class="col-50">3</div>
<div class="col-50">4</div>
</div>
Right now my code is as below:
<div class="row">
<label class="item item-radio col col-50" ng-repeat="a in question.answer">
<input type="radio" name="answers" class="a-{{ a.correct }}" value="{{ a.correct }}" ng-click='ansValue("{{ a.correct }}")'>
<div class="item-content">
<img src="img/ans/{{ a.option }}" />
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
</div>
But in the above code, its just a single row tag that I have. I wan't to somehow get the row tag contain/wrap every 2 items in the loop. What is the best way to achieve this?
Ref: Ionic Grid Doc
I managed to do it using $even.
<div ng-repeat="number in numbers">
<div class="row" ng-if="$even">
<div class="col col-50">{{numbers[$index]}}</div>
<div class="col col-50">{{numbers[$index + 1]}}</div>
</div>
</div>
Here's a working JSFiddle.
The solution from #Patrick Reck is excellent, but it forces you to repeat your code twice,
I suggest this improvement:
<div ng-repeat="number in numbers">
<div class="row" ng-if="$even">
<div class="col col-50"
ng-repeat="num in [numbers[$index],numbers[$index + 1]]">
{{num}}
</div>
</div>
</div>
this way you will write your code one time as if it is a normal ng-repeat
You can add flex-wrap: wrap to class row
http://jsfiddle.net/0momap0n/99/
<div ng-controller="MyCtrl">
<div class="row" style="flex-wrap: wrap">
<div class="col col-50" ng-repeat="number in numbers">{{number}}</div>
</div>
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.numbers = [1, 2, 3, 4, 5, 6];
}
I would write it like this, you can increase the $index % 3 to match the number of columns that you would like, this one will create 3 columns ...
<div class="row">
<div class="" ng-repeat="i in range(1, 9)">
<div class="clearfix" ng-if="$index % 3 == 0"></div>
<div class="col">
<h1>{{ i }}</h1>
</div>
</div>
</div>
This solution is using flex-flow and solves this brilliantly:
CSS
.container {
display: flex;
flex-flow: row wrap;
}
.item {
width: 50%;
}
HTML
<div class="container">
<div class="item" *ngFor="let number of numbers">
{{number}}
</div>
Try like below. you can create any number of columns by using the below code. all you need to use the size property of the ionic grid and replace noOfColumns with your what number of columns you want. in this case just use 2 for noOfColumns. it will work like a charm.
Angular grid is based on a 12 column layout with different breakpoints based on the screen size. ref: https://ionicframework.com/docs/layout/grid
<ion-grid>
<ion-row >
<ion-col ng-repeat="n in numbers" size="{{12/noOfColumns}}">
{{ n }}
</ion-col>
</ion-row>
</ion-grid>

ng-switch-when value from an ng-repeat not displaying value

I have this simple code. The problem is that "ng-switch-when" is not displaying, which means that column.stage is not being rendered correctly, although I tried displaying {{column.stage}} and it displays the correct value (1 OR 2 OR 3...)
<div class="column span3" ng-repeat="column in columns">
<h1>{{column.title}}</h1>
<div class="row" ng-repeat="shot in shots" ng-switch on="shot.stage">
<h6 ng-switch-when="{{column.stage}}">{{shot.title}}</h6>
</div>
</div>
You need to put ng-switch on="shot.stage" in its own element like:
<div class="column span3" ng-repeat="column in columns">
<h1>{{column.title}}</h1>
<div class="row" ng-repeat="shot in shots">
<div ng-switch on="shot.stage">
<h6 ng-switch-when="{{column.stage}}">{{shot.title}}</h6>
</div>
</div>
</div>

Resources