Hi I am parsing nested array but getting ReferenceError: player is not defined . I have state variable players which is json array as follows :
[
{
"game": "badminton",
"players": [
{
"name": "Hitesh",
"url": "https://i.imgur.com/aBpM49y.jpg",
"points": "1034.67",
"rank": "1",
"profile": "/hitesh"
},
{
"name": "Hitesh",
"url": "https://i.imgur.com/aBpM49y.jpg",
"points": "1034.67",
"rank": "1",
"profile": "/hitesh"
},
{
"name": "Hitesh",
"url": "https://i.imgur.com/aBpM49y.jpg",
"points": "1034.67",
"rank": "1",
"profile": "/hitesh"
},
{
"name": "Hitesh",
"url": "https://i.imgur.com/aBpM49y.jpg",
"points": "1034.67",
"rank": "1",
"profile": "/hitesh"
},
{
"name": "Hitesh",
"url": "https://i.imgur.com/aBpM49y.jpg",
"points": "1034.67",
"rank": "1",
"profile": "/hitesh"
}
]
},
{
"game": "football",
"players": [
{
"name": "Hitesh",
"url": "https://i.imgur.com/aBpM49y.jpg",
"points": "1034.67",
"rank": "1",
"profile": "/hitesh"
},
{
"name": "Hitesh",
"url": "https://i.imgur.com/aBpM49y.jpg",
"points": "1034.67",
"rank": "1",
"profile": "/hitesh"
},
{
"name": "Hitesh",
"url": "https://i.imgur.com/aBpM49y.jpg",
"points": "1034.67",
"rank": "1",
"profile": "/hitesh"
},
{
"name": "Hitesh",
"url": "https://i.imgur.com/aBpM49y.jpg",
"points": "1034.67",
"rank": "1",
"profile": "/hitesh"
},
{
"name": "Hitesh",
"url": "https://i.imgur.com/aBpM49y.jpg",
"points": "1034.67",
"rank": "1",
"profile": "/hitesh"
}
]
}
]
I am parsing it inside render function and creating a card based UI element for my view as follows :
render(){
// console.log(this.players);
var topplayers=[];
this.state.players.map(function(game,index){
topplayers.push(<div class="row" style={styles.row}>
<div class="col">
{game.game} Legend in Bangalore
</div>
</div>);
const row=<div class="row">;
topplayers.push({row});
game.players.map(function(player,index){
topplayers.push(<div class="col">
<div class="card">
<img class="card-img-top" src={player.url} alt="200-200" style={styles.image}/>
<div class="card-body">
<h4 class="card-title">{player.name}</h4>
<div >
<div style={styles.div}><img src="../../images/points.png" style={styles.rankimg}/>{player.points}</div>
<div style={styles.div}><img src="../../images/rank.png" style={styles.rankimg}/>{player.rank}</div>
</div>
View Profile
</div>
</div>
</div>)
}
)
const rowend=</div>;
topplayers.push({rowend})
}
)
return(
<div>
{topplayers}
</div>
);
}
But I am getting error ReferenceError: player is not defined . can someone help me where am I making mistake.
The problem is the const variables declared.
const row=<div class="row"> Here, div is marking start of html and
const rowend=</div>; here is ending tag.
So, everything in between is considered plain html. The map function is not behaving as a function but plain html. And that's why when player is put inside {}, javascript is trying to find the variable which doesn't exists.
Basically, in your code; everything written between <div class="row"> and </div> is considered html
To make it work, you need to restructure the code to something like this:
render(){
// console.log(this.players);
var topplayers=[];
this.state.players.map(function(game,index){
topplayers.push(<div class="row" style={styles.row}>
<div class="col">
{game.game} Legend in Bangalore
</div>
</div>
);
const row=<div class="row">
{game.players.map(function(player,index){
return (<div class="col">
<div class="card">
<img class="card-img-top" src={player.url} alt="200-200" style={styles.image}/>
<div class="card-body">
<h4 class="card-title">{player.name}</h4>
<div >
<div style={styles.div}><img src="../../images/points.png" style={styles.rankimg}/>{player.points}</div>
<div style={styles.div}><img src="../../images/rank.png" style={styles.rankimg}/>{player.rank}</div>
</div>
View Profile
</div>
</div>
</div>)
})
}
</div>;
topplayers.push(row)
})
return(
<div>
{topplayers}
</div>
);
}
Here, I have moved the game.players.map in {} wrapped in the <div class="row"> and </div>. This will make sure to work it like a function. Instead of pushing result into an array, I am directly returning values from the map function which will be injected between the required divs.
Hope you understand.
Please revert in case of any doubts
Related
I have an AngularJS 1.X app with a date field retrieved from a .json file. The date is being converted from a string to a date object to order the list but I need to capture the "month" part of the date to group by and for separate display but can't figure it out.
Here's my controller and HTML:
Angular Controller:
(function () {
"use strict";
angular.module("xxyyzz")
.controller("displayEventCtrl", displayEventCtrl);
function displayEventCtrl($http) {
var model = this;
model.header = "Upcoming Events";
model.events = [];
$http.get("/App/Data/eventCalendar.json")
.then(function (response) {
model.events = response.data;
console.log(model.events);
});
model.sortDate = function (event) {
var date = new Date(event.date);
return date;
}
}
})();
HTML:
<div ng-controller="displayEventCtrl as model">
<h3><i class="fa fa-calendar"></i> {{model.header}}</h3>
<div class="list-group">
<a href="#" class="list-group-item" ng-repeat="event in model.events | orderBy : -model.sortDate : true track by $index">
<div class="list-group-item-heading">
<h4>{{event.title}}</h4>
</div>
<p class="list-group-item-text">
Tuctus placerat scelerisque.
</p>
<div class="row">
<div class="col-xs-6">
<span class="small">{{event.location}}</span>
</div>
<div class="col-xs-6">
<span class="small pull-right">
{{event.startTime}} - {{event.endTime}}
</span>
</div>
</div>
<div class="row">
<div class="col-xs-9">
<span class="small">{{event.city}}, {{event.state}}</span>
</div>
<div class="col-xs-3">
<span class="small pull-right">{{event.date}}</span>
<div class="clearfix"></div>
</div>
</div>
</a>
</div>
</div>
JSON example:`
[
{
"id": 1,
"title": "Christmas Parade",
"date": "12/25/2017",
"city": "Dallas",
"state": "TX",
"location": "Galleria Mall",
"startTime": "11:00",
"endTime": "15:00"
},
{
"id": 2,
"title": "Thanksgiving Parade",
"date": "11/23/2017",
"city": "Denver",
"state": "CO",
"location": "Mile High Stadium",
"startTime": "11:00",
"endTime": "15:00"
},
{
"id": 3,
"title": "Halloween Parade",
"date": "10/31/2017",
"city": "Sleepy Hollow",
"state": "NY",
"location": "Ichabod Crane House",
"startTime": "19:00",
"endTime": "21:00"
},
{
"id": 4,
"title": "Independence Day Fireworks",
"date": "07/04/2017",
"city": "Washington",
"state": "DC",
"location": "National Mall",
"startTime": "11:00",
"endTime": "15:00"
}
]
Any help would be appreciated.
You can use js api named moment. Inject moment as dependency. You can use moment to get date in any formate you want.
moment ().get ('month');
To get the month, use the getMonth() function
var Xmas95 = new Date('December 25, 1995 23:15:30');
var month = Xmas95.getMonth();
console.log(month); // 11
My issue is that I am trying to pull the date from the array in the ng-repeat. So I can't declare the variable like above as it is part of a returned array {{event.date}}. What I need to know is how to get that date part from the nested item.
Use a custom filter to convert the text data to a Date object:
angular.module("app",[])
.filter("toDate", function() {
return function(data) {
return new Date(data);
};
})
.run(function($rootScope) {
var vm = $rootScope;
vm.Xmas95 = 'December 25, 1995 23:15:30';
})
<script src="//unpkg.com/angular/angular.js"></script>
<div ng-app="app">
Month number = {{Xmas95 | toDate | date: 'MM'}}
</div>
For more information, see
AngularJS Developer Guide - Creating Custom Filters
AngularJS date Filter API Reference
Sorry, the titles is not clear at all, but I didn't really know a better title to explain what I'm tring to do.
Basically, I've this set of data:
$scope.data = [
{"id": 0, "name": "Testing name 0", "type": 1, "details": {"pinned": true, "link": [1,2]}},
{"id": 1, "name": "Testing name 1", "type": 0, "details": {"pinned": true, "link": [4]}},
{"id": 2, "name": "Testing name 2", "type": 0, "details": {"pinned": true, "link": []}},
{"id": 3, "name": "Testing name 3", "type": 0, "details": {"pinned": true, "link": []}},
{"id": 4, "name": "Testing name 4", "type": 1, "details": {"pinned": true, "link": [4,0]}},
{"id": 5, "name": "Testing name 5", "type": 1, "details": {"pinned": true, "link": []}},
{"id": 6, "name": "Testing name 6", "type": 1, "details": {"pinned": true, "link": [1,2,3,4]}}
];
and this repeater to display only the rooms with type 1:
<ion-list class="list">
<div ng-repeat="item in data">
<ion-item class="item item-stable" ng-if="item.type==1">
{{item.name}}
</ion-item>
<div ng-if="item.type==1" ng-repeat="(key, value) in item.details">
<div ng-repeat="linkID in value" ng-if="key == 'link'">
<ion-item class="item-accordion">
{{linkID}}
</ion-item>
</div>
</div>
</div>
</ion-list>
as you can see from the code above, I'm printing a top level item with the name of all "type:" 1, and as a sublevel, all off the values inside link: [].
So far so good, but the values inside link: [] are IDs of linked posts, and instead of 1,2 or 3 I'd like to print "Testing name 1", "Testing name 2", "Testing name 3" and so on...
I can't figure out the best way. All the data is there, but I just can't display it. I tried with another ng-if, but nothing. Any help?
here's an example: http://codepen.io/nickimola/pen/dMNawj?editors=1010
Instead of using ng-if better use filter.
Live example on jsfiddle.
angular.module('ExampleApp', [])
.controller('ExampleController', function($scope) {
$scope.data = [{
"id": 0,
"name": "Testing name 0",
"type": 1,
"details": {
"pinned": true,
"link": [1, 2]
}
}, {
"id": 1,
"name": "Testing name 1",
"type": 0,
"details": {
"pinned": true,
"link": [4]
}
}, {
"id": 2,
"name": "Testing name 2",
"type": 0,
"details": {
"pinned": true,
"link": []
}
}, {
"id": 3,
"name": "Testing name 3",
"type": 0,
"details": {
"pinned": true,
"link": []
}
}, {
"id": 4,
"name": "Testing name 4",
"type": 1,
"details": {
"pinned": true,
"link": [4, 0]
}
}, {
"id": 5,
"name": "Testing name 5",
"type": 1,
"details": {
"pinned": true,
"link": []
}
}, {
"id": 6,
"name": "Testing name 6",
"type": 1,
"details": {
"pinned": true,
"link": [1, 2, 3, 4]
}
}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="ExampleApp">
<div ng-controller="ExampleController">
<ion-list class="list">
<div ng-repeat="item in data|filter:{type:1}">
<ion-item class="item item-stable">
{{item.name}}
</ion-item>
<div ng-repeat="linkID in item.details.link">
<ion-item class="item-accordion">
Lnked post--{{data[linkID].name}}
</ion-item>
</div>
<div ng-show="item.details.link.length==0">
<i>Nothing linked</i>
</div>
</div>
</ion-list>
</div>
</div>
Think that is what you expected?
<ion-content ng-app="ionicApp" ng-controller="MyCtrl">
<ion-list class="list">
<div ng-if="item.type==1" ng-repeat="item in data">
<ion-item class="item item-stable">
{{item.name}}
</ion-item>
<div ng-repeat="link in item.details.link">
<ion-item ng-if="data[link].name" class="item-accordion">
{{data[link].name}}
</ion-item>
<ion-item ng-if="!data[link].name" class="item-accordion">
Link not found: {{link}}
</ion-item>
</div>
<ion-item ng-if="item.details.link.length == 0">
Nothing
</ion-item>
</div>
</ion-list>
</ion-content>
You could load the links into the scope then reference them with the id (assuming they're in order).
<div ng-repeat="linkID in value" ng-if="key == 'link'">
<ion-item class="item-accordion">
{{links[linkID]}}
</ion-item>
</div>
I'm trying to ng-repeat and load the main menu from a JSON.
Here's the scenario.
Please check the layout screenshot or you might not be able to understand
MainMenu - Groceries,Listing,Product,Blog,Gallery,Pages,Woman,Electronics,Contact
Submenu - Corporate,Electronics,Kids,Background Isotope,Login,Sign Up
Website Layout Screenshot
JSON//
[{"title":"Corporative","link":"index_corporate.html"},
{"title":"Electronics","link":"index_corporate.html"},
{"title":"Kids","link":"index_corporate.html"},
{"title":"Background Isotope","link":"index_corporate.html"},
{"title":"Login","link":"#/login"},
{"title":"Sign Up","link":"#/register"}
]
Controller//
$http.get('data.json').success(function(dataForSubmenu){
$scope.menu = dataForSubmenu;
});
HTML//
<dt class="item">
<ul class="sf-menu">
<li><a href="index.html”>Groceries</a>//Titles to be repeated from JSON
<ul>
//Dropdown Menu Under Main Menu
<li ng-repeat="menuData in menu">{{menuData.title}}</li>
</ul>
</li>
</ul>
</dt>
<dd></dd>
<dt class="item">
<ul class="sf-menu">
<li><a href="page2.html”>Listing</a>//Titles to be repeated from JSON
<ul>
//Dropdown Menu Under Main Menu
<li ng-repeat="menuData in menu">{{menuData.title}}</li>
</ul>
</li>
</ul>
</dt>
<dd></dd>
<dt class="item">
<ul class="sf-menu">
<li><a href="page3.html”>Product</a>//Titles to be repeated from JSON
<ul>
//Dropdown Menu Under Main Menu
<li ng-repeat="menuData in menu">{{menuData.title}}</li>
</ul>
</li>
</ul>
</dt>
What I want to do is
Load the MainMenuItems using ng-repeat using data from JSON
Load DIFFERENT Sub Menus under multiple main menus using ng-repeat with data from JSON
Repeat should work with the HTML structure I have got going (Using a template for the website so can't change much in that)
your json should be something like this:
[{
"menu":[{
"mainMenuName": "Groceries",
"mainMenuLink": "link"
"subMenu": [{
"title": "Corporative",
"link": "index_corporate.html"
}, {
"title": "Electronics",
"link": "index_corporate.html"
}, {
"title": "Kids",
"link": "index_corporate.html"
}, {
"title": "Background Isotope",
"link": "index_corporate.html"
}, {
"title": "Login",
"link": "#/login"
}, {
"title": "Sign Up",
"link": "#/register"
}]
}, {
"mainMenuName": "Listing",
"mainMenuLink": "link",
"subMenu": [{
"title": "Corporative",
"link": "index_corporate.html"
}, {
"title": "Electronics",
"link": "index_corporate.html"
}, {
"title": "Kids",
"link": "index_corporate.html"
}, {
"title": "Background Isotope",
"link": "index_corporate.html"
}, {
"title": "Login",
"link": "#/login"
}, {
"title": "Sign Up",
"link": "#/register"
}]
}, {
"mainMenuName": "Product",
"mainMenuLink": "link",
"subMenu": [{
"title": "Corporative",
"link": "index_corporate.html"
}, {
"title": "Electronics",
"link": "index_corporate.html"
}, {
"title": "Kids",
"link": "index_corporate.html"
}, {
"title": "Background Isotope",
"link": "index_corporate.html"
}, {
"title": "Login",
"link": "#/login"
}, {
"title": "Sign Up",
"link": "#/register"
}]
}]
}]
once you have that json in $scope.menu then your html should look like
<dt ng-repeat="menuData in menu" class="item">
<ul class="sf-menu">
<li><a href="menuData.mainMenuLink”>{{menuData.mainMenuName}}</a>
<ul>
//Dropdown Menu Under Main Menu
<li ng-repeat="subMenuData in menuData.subMenu">{{subMenuData.title}}</li>
</ul>
</li>
</ul>
</dt>
I have not tested this out yet but it should be something similar to this. if you send me the project I can take a look.
check this answer out its similar solution to what you want: https://stackoverflow.com/a/19840244/2502933
I'm having a JSON data namely $scope.family, it contains the family.name and optionally family.child
app.controller('test', function ($scope) {
$scope.family = [
{
"name": "Siva",
"child": [
{
"name": "Suriya"
},
{
"name": "Karthick"
}
]
},
{
"name": "Kumar",
"child": [
{
"name": "Rajini"
},
{
"name": "Kamal"
},
{
"name": "Ajith"
}
]
},
{
"name": "Samer",
"child": "Ranjan"
},
{
"name": "Mahesh",
"child": "Babu"
},
{
"name": "Joseph"
}
];
});
Cases:
If family.child has one child then the name is directly assign as a string
If family.child has more than one child then the name is assign as a array of string with the property family.child.name
If family.child doesn't in the collection just show the family.name
My Expected Output UI is
Siva
Suriya
Karthick
Kumar
Rajini
Kamal
Ajith
Samer
Ranjan
Mahesh
Babu
Joseph
My HTML Source Code (I Can't able to get the expected output from this code)
<ul>
<li ng-repeat="member in family">
{{ member.name }}
<div class="liststyling" ng-if="member.child.length > 0">
<ul>
<li ng-repeat="chdMember in member.child>
{{ chdMember.name }}
</li>
</ul>
</div>
</li>
</ul>
Kindly assist me...
Please refer the fiddle here. You need some changes in the model and loop with the new modified model - newFamily instead of family.
HTML:
<div ng-app="app" ng-controller="test">
<ul>
<li ng-repeat="member in newFamily">
{{ member.name }}
<div class="liststyling" ng-if="member.child.length > 0">
<ul>
<li ng-repeat="chdMember in member.child track by $index">
{{ chdMember.name }}
</li>
</ul>
</div>
</li>
</ul>
</div>
JS:
var app = angular.module('app', []);
app.controller('test', function ($scope) {
$scope.family = [
{
"name": "Siva",
"child": [
{
"name": "Suriya"
},
{
"name": "Karthick"
}
]
},
{
"name": "Kumar",
"child": [
{
"name": "Rajini"
},
{
"name": "Kamal"
},
{
"name": "Ajith"
}
]
},
{
"name": "Samer",
"child": "Ranjan"
},
{
"name": "Mahesh",
"child": "Babu"
},
{
"name": "Joseph"
}
];
$scope.newFamily = [];
angular.forEach($scope.family, function (v, k) {
var existingChildArray = v.child;
var newChildArray = [];
if (!angular.isArray(v.child) && v.child) {
newChildArray.push({ 'name': v.child });
}
var addChild = newChildArray.length > 0 ? newChildArray : existingChildArray;
$scope.newFamily.push({ 'name': v.name, 'child': addChild });
});
});
Try this for your actual output.
<ul>
<li ng-repeat="member in family">
{{ member.name }}
<div class="liststyling">
<ul>
<li ng-repeat="chdMember in member.child">
{{ chdMember.name }}
</li>
</ul>
</div>
</li>
</ul>
$scope.family = [
{
"name": "Siva",
"child":
[
{
"name": "Suriya"
},
{
"name": "Karthick"
}
]
},
{
"name": "Kumar",
"child": [
{
"name": "Rajini"
},
{
"name": "Kamal"
},
{
"name": "Ajith"
}
]
},
{
"name": "Samer",
"child": [{name:"Ranjan"}]
},
{
"name": "Mahesh",
"child": [{name:"Babu"}]
},
{
"name": "Joseph",
"child": []
}
];
You can add the following method to your controller
$scope.isArray = function(obj) {
return angular.isArray(obj);
}
and update the markup as
<li ng-repeat="member in family">
{{ member.name }}
<div class="liststyling">
<ul ng-if="isArray(member.child)">
<li ng-repeat="chdMember in member.child">
{{ chdMember.name }}
</li>
</ul>
<ul ng-if="member.child && !isArray(member.child)">
<li>
{{ member.child }}
</li>
</ul>
</div>
</li>
This should do, I think.
Even though #MarcNuri provided a good answer. But if you are not changing the data pattern you can use this also.
HTML
<ul>
<li ng-repeat="member in family">
{{ member.name }}
<div class="liststyling" ng-if="isArray(member.child) && member.child.length > 0">
<ul>
<li ng-repeat="chdMember in member.child">
{{ chdMember.name }}
</li>
</ul>
</div>
<div class="liststyling" ng-if="!isArray(member.child) && member.child.length > 0">
<ul>
<li>
{{ member.child}}
</li>
</ul>
</div>
</li>
</ul>
JS
app.controller('test', function ($scope) {
$scope.isArray = angular.isArray;
$scope.family = [
{
"name": "Siva",
"child": [
{
"name": "Suriya"
},
{
"name": "Karthick"
}
]
},
{
"name": "Kumar",
"child": [
{
"name": "Rajini"
},
{
"name": "Kamal"
},
{
"name": "Ajith"
}
]
},
{
"name": "Samer",
"child": "Ranjan"
},
{
"name": "Mahesh",
"child": "Babu"
},
{
"name": "Joseph"
}
];
});
just notice $scope.isArray = angular.isArray; in js.Find plank HERE
Here is a simple running version of your code JSFiddle (not the best coding practices)
Keep in mind that you should use at least AngularJS 1.1.15 version at least to use ng-if.
You should simply tidy up your script a little and it'll work:
HTML:
<body ng-app="myApp">
<div ng-controller="ctrl">
<ul>
<li ng-repeat="member in family">
<h2>
{{ member.name }}
</h2>
<div class="liststyling" ng-if="member.child.length > 0">
<ul>
<li ng-repeat="chdMember in member.child">
{{ chdMember.name }}
</li>
</ul>
</div>
</li>
</ul>
</div>
</body>
Javascript:
var app = angular.module('myApp',[]);
app.controller('ctrl', MyCtrl);
function MyCtrl($scope) {
$scope.family = [
{
"name": "Siva",
"child": [
{
"name": "Suriya"
},
{
"name": "Karthick"
}
]
},
{
"name": "Kumar",
"child": [
{
"name": "Rajini"
},
{
"name": "Kamal"
},
{
"name": "Ajith"
}
]
},
{
"name": "Samer",
"child": [{name:"Ranjan"}]
},
{
"name": "Mahesh",
"child": []
},
{
"name": "Joseph"
}
];
}
I created a responsive grid in Ionic which doesn't work properly. The Grid is not responsive. So it isn't automatically adjusted on different screen size and set no linebreak if I add many Buttons or delete these to the/from the system. Either they merge into each other or all the buttons are in a column below the each other on small screens. How can i solve this?
I add or delete all the buttons in a JSON-File. From there I parse the Buttons in to the system.
JSON: 7 Buttons
[
{
"_comment": "Games",
"type": "button",
"id": "entertainmentButton",
"icon": "ion-film-marker",
"name": "Game and entertainment",
"topage": "servicePage.html",
"color": "white",
"function": "OpenLink()",
"controller": "OpenLinkCtrl",
"backgroundcolor": "#0066FF",
"font-size": "26px"
},
{
"_comment": "Logo",
"type": "button",
"id": "MainPageLogo",
"icon": "",
"image": "../img/icon/logo_moenchsweiler.png",
"name": "second link",
"topage": "",
"function": "OpenLink()",
"controller": "OpenLinkCtrl",
"color": "",
"backgroundcolor": "",
"font-size": ""
},
{
"_comment": "Logo",
"type": "button",
"id": "MainPageLogo",
"icon": "",
"image": "../img/icon/logo_moenchsweiler.png",
"name": "second link",
"topage": "",
"function": "OpenLink()",
"controller": "OpenLinkCtrl",
"color": "",
"backgroundcolor": "",
"font-size": ""
},
{
"_comment": "Logo",
"type": "button",
"id": "MainPageLogo",
"icon": "",
"image": "../img/icon/logo_moenchsweiler.png",
"name": "second link",
"topage": "",
"function": "OpenLink()",
"controller": "OpenLinkCtrl",
"color": "",
"backgroundcolor": "",
"font-size": ""
},
{
"_comment": "Logo",
"type": "button",
"id": "MainPageLogo",
"icon": "",
"image": "../img/icon/logo_moenchsweiler.png",
"name": "second link",
"topage": "",
"function": "OpenLink()",
"controller": "OpenLinkCtrl",
"color": "",
"backgroundcolor": "",
"font-size": ""
},
{
"_comment": "Logo",
"type": "button",
"id": "MainPageLogo",
"icon": "",
"image": "../img/icon/logo_moenchsweiler.png",
"name": "second link",
"topage": "",
"function": "OpenLink()",
"controller": "OpenLinkCtrl",
"color": "",
"backgroundcolor": "",
"font-size": ""
},
{
"_comment": "Logo",
"type": "button",
"id": "MainPageLogo",
"icon": "",
"image": "../img/icon/logo_moenchsweiler.png",
"name": "second link",
"topage": "",
"function": "OpenLink()",
"controller": "OpenLinkCtrl",
"color": "",
"backgroundcolor": "",
"font-size": ""
}
]
JavaScript:
var myApp = angular.module('starter', []);
myApp.config(['$sceDelegateProvider', function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
'self',
''
]);
}]);
myApp.controller('generateHTMLCtrl', function ($scope, $http, $compile, $interpolate, $templateCache) {
$http.get('myjsonfile.json').success(function (data) {
for(var i in data){
var interpolated = $interpolate($templateCache.get("tpl").trim())(data[i]);
angular.element(document.querySelector("#loadhere")).append($compile(interpolated)($scope));
}
});
});
myApp.controller("OpenLinkCtrl", function ($scope) {
$scope.OpenLink = function () {
alert("Link open");
}
});
HTML:
<body ng-app="starter" class="padding" style="text-align: center">
<div class="row responsive-md" ng-controller="generateHTMLCtrl" id="loadhere"></div>
<script type="text/ng-template" id="tpl">
<div class="col">
<a style="color:{{color}}; background-color:{{backgroundcolor}} " id="{{id}}" class="{{type}}" href="{{topage}}" ng-controller="{{controller}}" ng-click="{{function}}"> <i class="{{icon}}"><br></i>{{name}}</a>
</div>
</script>
</body>
What do I need to modify in HTMl-code that the Grid is responsive?
Edit:
It should look like here, e.g:
<div class="row responsive-sm">
<div class="col">
<button style="width: 100px; height: 100px">Test</button>
<button style="width: 100px; height: 100px">Test</button>
<button style="width: 100px; height: 100px">Test</button>
<button style="width: 100px; height: 100px">Test</button>
<button style="width: 100px; height: 100px">Test</button>
<button style="width: 100px; height: 100px">Test</button>
<button style="width: 100px; height: 100px">Test</button>
</div>
</div>
This code displays the buttons as follows:
on large screen size:
bit smaller:
very small:
Edited:
The solution:
<div ng-controller="generateHTMLCtrl" id="loadhere">
<script type="text/ng-template" id="tpl">
<a style="color:{{color}}; background-color:{{backgroundcolor}};"
id="{{id}}" class="{{type}}" href="{{topage}}"
ng-controller="{{controller}}" ng-click="{{function}}">
<i class="{{icon}}"><br></i>{{name}}
</a>
</script>
</div>
what you want is something more like this:
<div class="row" ng-repeat="image in images" ng-if="$index % 4 === 0">
<div class="col col-25" ng-if="$index < images.length">
<img ng-src="{{images[$index].src}}" width="100%" />
</div>
<div class="col col-25" ng-if="$index + 1 < images.length">
<img ng-src="{{images[$index + 1].src}}" width="100%" />
</div>
<div class="col col-25" ng-if="$index + 2 < images.length">
<img ng-src="{{images[$index + 2].src}}" width="100%" />
</div>
<div class="col col-25" ng-if="$index + 3 < images.length">
<img ng-src="{{images[$index + 3].src}}" width="100%" />
</div>
</div>
This will create a maximum of 4 images per row that wrap and stack. You can also make it more or less images per by changing ng-if statement in for rows and then the amount of images in that row
When you are on every forth image, you are going to show the row class, thus creating a new row
you will pre-allocate four columns for you row, but you would first check to see if it will ever be filled to prevent an undefined exception
If the image exists at the specified index, then add it to the column
Edit!
Try this
add this css class:
.gallery {
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
}
then put this in your html:
<ion-content ng-controller="ExampleController" ng-init="loadImages()" class="gallery">
<span ng-repeat="image in images">
<img src="{{image}}" width="150px">
</span>
since ionic is built on built on top of flex box you can make a flexbox grid in ionic.