Meteor - "each" listing in view - html

I have this part of code:
{{#each cards}}
<div class="row-fluid">
<div class="card span4">
<p>{{content}}</p>
</div>
</div>
{{/each}}
and issue with setting it up to do something like this:
<div class="row-fluid">
<div class="card span4">
<p>{{content}}</p>
</div>
<div class="card span4">
<p>{{content}}</p>
</div>
<div class="card span4">
<p>{{content}}</p>
</div>
</div>
<div class="row-fluid">
<div class="card span4">
<p>{{content}}</p>
</div>
....
Anyone have any idea how to do so? (I know that I can render directly from Meteor.render() but I want to avoid it if possible)

Try out this split helper function which will split an array into sub-arrays, each with up to n elements:
if (Meteor.isClient) {
Handlebars.registerHelper("split", function(array, n) {
var groups = _.groupBy(array, function(element, index) {
return Math.floor(index/n);
});
return _.toArray(groups);
});
}
{{#each split cards 3}}
<div class="row-fluid">
{{#each this}}
<div class="card span4">
<p>{{content}}</p>
</div>
{{/each}}
</div>
{{/each}}
Note that your cards helper function must return an array for this to work, not a collection cursor.

Related

How to iterate json in angular?

I am getting the following json from a database, and I want to do an ngfor;but I don't know how to do it with this kind of JSON
link codebeautify.org
<div class="col-md-4" *ngFor="let referencia of referencias.rows">
<div class="card text-center">
<div class="card-header">
</div>
<div class="card-body">
{{referencia.2}}
</div>
</div>
</div>
You need to parse your json return from the API first before using it:
In the ts file where you receive your data:
referencias = JSON.parse(referencias);
And since each referencia from referencias.rows is an Array, you access it data like this referencia[arrayIndex]:
<div class="col-md-4" *ngFor="let referencia of referencias.rows">
<div class="card text-center">
<div class="card-header">
</div>
// Changed referencia.2 to referencia[2]
<div class="card-body">
{{referencia[2]}} // Should show 'AYALA'
</div>
</div>
</div>
In your ts file, I am assuming referencias is the parsed json response of the API. The below solution is to get all the rows.
<div class="col-md-4" *ngFor="let referencia of referencias['rows']">
<div class="card text-center">
<div class="card-header">
</div>
<div class="card-body" *ngFor="let rowData of referencia">
{{rowData}}
</div>
</div>
Hope this works for you.

Vue Components style display differently on page than when not used as components

I am working on moving chunks of code from being static code within my Views components into actual reusable Components.
I have moved what is called my TrainerCards code into a separate component and the cards used to display nicely as 3 to a row when they were hard coded in my TrainerIndex.vue, but now that they are being passed as a component, they only render as 1 per row, and I don't know why.
TrainerIndex.vue -
<div class="row">
<div class="col-md-4">
<TrainerCards v-for="trainer in orderBy(filterBy(trainers, searchText), sortAttribute, sortAscending)" :key="trainer.id" :trainer="trainer"/>
</div>
</div>
`
TrainerCards.vue -
<template>
<div class="card card-profile">
<div class="card-img-top">
<router-link v-bind:to="'/trainers/' + trainer.id">
<img class="img" :src="(trainer.avatar)" />
</router-link>
</div>
<div class="card-body">
<h4 class="card-title">{{trainer.full_name}}</h4>
<h6 class="card-category">{{trainer.location}}</h6>
<div class="card-footer">
<TrainerTags v-for="tag in trainer.tags" :key="tag.id" :tag="tag"/>
</div>
</div>
</div>
</template>
Any idea on how to get them to display properly?
Your col-md-4 class is what makes them fill 1/3 of the area, but that is now outside your component and not repeating. Move that div inside:
<div class="row">
<TrainerCards v-for="trainer in orderBy(filterBy(trainers, searchText), sortAttribute, sortAscending)" :key="trainer.id" :trainer="trainer"/>
</div>
<template>
<div class="col-md-4">
<div class="card card-profile">
<div class="card-img-top">
<router-link v-bind:to="'/trainers/' + trainer.id">
<img class="img" :src="(trainer.avatar)" />
</router-link>
</div>
<div class="card-body">
<h4 class="card-title">{{trainer.full_name}}</h4>
<h6 class="card-category">{{trainer.location}}</h6>
<div class="card-footer">
<TrainerTags v-for="tag in trainer.tags" :key="tag.id" :tag="tag"/>
</div>
</div>
</div>
</div>
</template>

Cards Bootstrap 4 in one line

I'm trying to make my cards (panels) set next to each other and I'm using the templates at mdbootstrap.com.
My code:
<div class="row">
<div class="col-md-12">
<div class="card mdb-color lighten-2 text-center z-depth-2">
<div class="card-body">
<p class="white-text mb-0"> <?php echo convertCurrency("1.00", "POUND", "DOLLAR");
?>.</p>
</div>
</div>
<Br>
<div class="card mdb-color lighten-2 text-center z-depth-2">
<div class="card-body">
<p class="white-text mb-0">btc<i class="fa fa-bitcoin" aria-hidden="true">
</i> = <?php echo $info['rate'];
?></p>
</div>
</div>
</div>
</div>
I tried to give it a class of d-inline but it's not working...
The cards I'm using https://mdbootstrap.com/components/panels/
Just in case someone is looking at this, an easy way to do it is to use the card-deck class. So, for example:
<div class=card-deck>
<!-- First Card -->
<div class="card">
<div class="card-title">Title here</div>
<div class="card-body">Body here</div>
</div>
<!-- Second Card -->
<div class="card">
<div class="card-title">Title here</div>
<div class="card-body">Body here</div>
</div>
</div>
Just remember that all the cards go in the card-deck div. A drawback of this is if you have lets say 6 cards, it'll put all of the cards on the same line instead of breaking them up in multiple lines. Whenever I want cards to be on multiple lines I just use another card-deck. Hope this helps someone, cheers.
One way of doing what you want is to put both cards in two separate columns, each, i.e each box card should be inside a div with class col-md-6.
The following code shows two cards, side by side in desktop browser window size.
<div class="row">
<div class="col-md-6">
<div class="card mdb-color lighten-2 text-center z-depth-2">
<div class="card-body">
<p class="white-text mb-0">واحد جنيه سوداني = <?php echo convertCurrency("1.00", "SDG", "USD")." دولار امريكي";?>.</p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card mdb-color lighten-2 text-center z-depth-2">
<div class="card-body">
<p class="white-text mb-0">بتكوين <i class="fa fa-bitcoin" aria-hidden="true"></i> = <?php echo $info['rate']." جنيه سوداني";?></p>
</div>
</div>
</div>
</div>

Bootstrap Dynamic Layout Change

I am currently using bootstrap on a website which is displaying an ecommerce store.
The current layout is 3 columns of items within 4 rows, but I am wanting to implement a button which will change it to 2 columns.
I have done so, however the code seems to be causing some issues in that the layout changes to
Two Items
One Item
Two Items
One Item
Each one Item row has a large area of white space where an item could go.
The Code below shows one "Row" which is in the three column layout. I have used javascript which changes the class 'col-sm-4' to 'col-sm-6'. Where is my mistake here?
<div class="row"><!-- row 2 -->
<div class="col-sm-4 product-category-display">
<div class="row">
<img src="assets/img/products/women/jackets_coats/raina_parka.png" class="product-image">
</div>
<div class="row product-status">
<span class="products-status-sale">Sale</span>
</div>
<div class="row">
<p class="product-name col-sm-12"<p>Raina Waterproof Parka</p>
</div>
<div class="row product-price">
<span>£99.00</span>
</div>
</div>
<div class="col-sm-4 product-category-display">
<div class="row">
<img src="assets/img/products/women/jackets_coats/april_jacket.png" class="product-image">
</div>
<div class="row product-status">
<span></span>
</div>
<div class="row">
<p class="product-name col-sm-12">April Waterproof Front Pocket Jacket</p>
</div>
<div class="row product-price">
<span>£99.95</span>
</div>
</div>
<div class="col-sm-4 product-category-display">
<div class="row">
<img src="assets/img/products/women/jackets_coats/free_delivery.png" class="product-image">
</div>
<div class="row product-status">
<span></span>
</div>
<div class="row">
<p></p>
</div>
<div class="row product-price">
<span></span>
</div>
</div>
</div><!-- end row 2 -->
The columns should only add upto 12. When you change 4 to 6 its becomes 18.
<div class="row">
<div class="col-lg-4"></div><div class="col-lg-4"></div><div class="col-lg-4"></div>
</div>
Problem solved. I just had to remove all of the rows and tweak the JS to add the class

How to apply class to div dynamically using AngularJS

I want to switch to div with class="col-md-x" depending upon the number of student object i am receiving.
E.x: Sample
if i am getting only one JSON object then i want to a div with class should be like(class="col-md-12").
if am getting two JSON object then i want to switch to a div with class="col-md-6".
<div class="container">
<div class="row">
<div class="col-md-12">
<div clss="row">
<div ng-repeat="c in vm.students" class="col-md-12">
<div calss="thimbnail">
<div class="Option div 1">
</div>
</div>
</div>
<div ng-repeat="c in vm.students" class="col-md-6">
<div calss="thimbnail col-md-">
<div class="Option div 2">
</div>
</div>
</div>
<div ng-repeat="c in vm.students" class="col-md-4">
<div calss="thimbnail col-md-">
<div class="Option div 3">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Use ng-class like below,
ng-class="{class}[condition]"
In your case ,
ng-class="{1:'col-md-12', 2:'col-sm-6'}[vm.students.length]"
For your reference, Detailed explanation
You have calss instead of class in the example code. I hope that is a typo.
To address to your problem you can make use of ng-class where you can assign classes based on conditions
More document on ng-class
Code Snippet:
<div class="container">
<div class="row">
<div class="col-md-12">
<div clss="row">
<div ng-repeat="c in vm.students" ng-class="{col-md-12:vm.students.length === 1,col-md-6:vm.students.length === 2">
<div calss="thimbnail">
<div class="Option div 1">
</div>
</div>
</div>
</div>
</div>
</div>
</div>