I'm trying to include external data/value into a v-for loop in VueJS. Have no idea how to go about it. Basically I have data/value in value as follows:
External Data
{{ time.one }}
{{ time.two }}
NOTE: I am receiving the data exactly as above as its coming from an API
JSON (own data)
{
"persons": [
{
"title": "Prof",
"name": "SomeProf"
},
{
"title": "Dr",
"name": "SomeDr"
},
]
}
And the loop is basic v-for loop
<ul v-for="person in persons">
<li> {{ person.title }}{{ person.name }} - <!-- Inc. external data {{ time.one }} -->
</ul>
The end result being:
Prof. SomeProf - 10:00pm
Dr. SomeDr - 09:00am
Thank You
I don't know if I understood your question properly, but you can also just modify the data right away.
Let's say you have your own data and make a call to the API when the component is created. What you can do is:
import persons from '#/data/persons'
export default {
data() {
return {
dataToLoop: []
}
},
async created() {
const timeData = await yourApiCall();
let results = [];
for (let i in persons) {
results.push({
...persons[i],
time: timeData[i] // or whatever key matches the person
})
}
this.dataToLoop = results;
}
}
And then all you need to do is loop through "dataToLoop" in your component.
Related
I have a some.json file like this:
{
"disneyland-paris": {
"lang": "de"
},
"hanoi": {
"lang": "de"
}
}
… that I want to get into a nunjucks template with:
pipe(data(function() {
return JSON.parse(fs.readFileSync(".../some.json"))
}))
.pipe(nunjucksRender())
How would I access this data within nunjucks?
This does not work:
{{ some }}
or
{{ some.json }}
One of many approaches you could take is use gulp nunjucks render for your gulp project. If you decide to go this route, you can use the following steps as a proof-of-concept to achieve your goal. And if not, you can borrow ideas from the following steps anyway!
Step 1 - Within your project, "you could" structure your JSON data like so in a file called Languages.js :
const languages = [
{
"group": [{
"location":"disenyland-paris",
"lang": "de"
},
{
"location":"hanoi",
"lang": "de"
},
]
}];
module.exports = languages;
Step 2 - From your gulpfile.js, assuming you are running a gulp project, call your JSON data, then reference it within your Nunjucks logic as an environmental global...
...
const nunjucksRender = require('gulp-nunjucks-render');
const Languages = require('./src/models/Languages');
...
const manageEnvironment = function (environment) {
environment.addGlobal('mLangauges',Languages);
}
...
function genNunJucks(cb) {
return src(['src/views/*.html'])
.pipe(nunjucksRender({
path: ['src/views/', 'src/views/parts'], // String or Array
ext: '.html',
inheritExtension: false,
envOptions: {
watch: true,
},
manageEnv: manageEnvironment,
loaders: null
}))
.pipe(dest('pub')); //the final destination of your public pages
cb();
}
//then do more stuff to get genNunJucks() ready for gulp commands...
Step 3 - From within your Nunjucks template file, call the data like so...
{% for sections in mLangauges %}
{% for mgroup in sections.group %}
<p>{{mgroup.location}}</p>
<p>{{mgroup.lang}}</p>
{% endfor %}
{% endfor %}
All that is left to do is run your gulp project :)
Tip - If you change your JSON data while working, you may need to re-build your gulp project to see the udpated JSON data on your web page. Re-building can be as simple as running 'npm run build' if you set it up right in your package.json file.
In eleventy, I'm using the 'eleventy-cache-assets' utility to retrieve API data from TMDB and place it in a cache. The JSON is successfully retrieved and stored in the cache. I can also use the Nunjucks dump filter to dump the full JSON to a page. However, I cannot run a for loop over the JSON content. It's behaving as if the data does not exist. I'm likely making a schoolboy error here but I can't see it.
Here's the JS that retrieves the data (successfully).
module.exports = async function () {
try {
// Grabs either the fresh remote data or cached data (will always be fresh live)
let json = await Cache(
`https://api.themoviedb.org/3/movie/upcoming?api_key=${TOKEN}&language=en-GB®ion=GB`,
{
duration: "1d", // 1 day
type: "json",
}
);
return {
films: json,
};
} catch (e) {
return {
films: 0,
};
}
};
Here is how I'm trying to loop through the content. The else condition is returning. When I remove the else condition, nothing is returned (just the empty ul). If I've targeted the node incorrectly, I should have x number of empty li tags, but I don't.
<ul>
{% for film in films %}
<li>{{ results.title }}</li>
{% else %}
<li>This displays if the 'films' collection were empty</li>
{% endfor %}
</ul>
The issue is likely just that you're wrapping the result from the API in an additional object, and eleventy adds an additional wrapper based on the file name of the data file, so you need to modify the way you access the results.
According to the TMDB documentation, the API will return something like this (JSON-encoded):
const json = {
page: 1,
results: [
{/** ... */},
{/** ... */},
{/** ... */},
]
}
Then you're wrapping that result in another object:
return {
films: json,
};
Finally, eleventy makes that data available to templates under a variable name based on the name of your data file — i.e. the file contains your API call. You didn't mention the name of the file, let's assume it lives in your data directory as tmdb.js. That means the variable accessible to your template will look something like this:
const tmdb = {
films: {
page: 1,
results: [
{/** ... */},
{/** ... */},
{/** ... */},
]
}
}
See the documentation on how eleventy parses data files for more information.
This means you can access the results in this way:
<ul>
{% for film in tmdb.films.results %}
<li>{{ film.title }}</li>
{% else %}
<li>This displays if the 'films' collection were empty</li>
{% endfor %}
</ul>
Note also that your original code used {{ results.title }} which isn't defined in your scope, I changed that to use the loop variable film. Also, adjust the tmdb variable to the name of your data file.
I would also recommend not wrapping the response of the API in an additional object (the key films also implies it's a list of films, not the complete API response). It doesn't really add anything and only increases complexity.
I´m trying to loop over a content. I tried some answers found here in StackOverflow but none works for me.
I have this JSON
{
"-KmdNgomUUnfV8fkzne_":{
"name":"Abastecimento"
},
"-KmdNgzguGKbCEfyQ3F0":{
"name":"Consórcios"
},
"-KmdNh9_jtBlFqY1Y_Rj":{
"name":"Consultoria aeronáutica"
},
"-KmdNhKNCVEiJ2Ou8bUV":{
"name":"Cursos e treinamentos"
},
"-KmdNhVRoKfaCM--304M":{
"name":"Financiamento"
},
"-KmdNhfIC6fA0kDJcVBq":{
"name":"Hangaragem"
},
"-KmdNhu0X-PteQMyHp_n":{
"name":"Mecânica"
},
"-KmdNi6ZmmYXnoOTXoC5":{
"name":"Táxi Aéreo"
},
"-KmdNiHFhiX_crXB1L2R":{
"name":"Outros"
}
}
I´m trying to loop using this code
<button ion-item *ngFor="let cat of subcategories" (click)="onSubcategorySelect(cat)">
{{ cat.name }}
</button>
It's not working. What am I doing wrong?
Angular's ng-for requires any element with Iterable interface to work and it's not the case for a plain Object. You need to "convert" this object in an array, the easier way is with forEach or a for/in loop:
Anywhere in your code you'll iterate through your object and save it in a new variable to be used in ngFor:
public newSubcategories: any[] = []; // DECLARE A NEW EMPTY ARRAY IN THE TOP OF YOUR CLASS
this.subcategories.forEach(item => {
this.newSubcategories.push(a);
});
If you need to mantain the key use for/in to push a new object with the key.
public newSubcategories: any[] = []; // DECLARE A NEW EMPTY ARRAY IN THE TOP OF YOUR CLASS
for(let key in this.subcategories){
this.newSubcategories.push({
key: key,
name: this.subcategories[key].name
});
}
Finally your HTML
<button ion-item *ngFor="let cat of newSubcategories" (click)="onSubcategorySelect(cat)">
{{ cat.name }}
</button>
With this you'll be able to use ng-for in your new array.
Hope this helps.
in angular I have a rootscope object like this:
{"id":12,"email":"testmail#test.com","given_name":"John","family_name":"Doe","car":"BMW"}
How can I acces each parameter individually?
Now I just do
{{ currentUser }}
And doing
{{ currentUser.email }}
obviously doesn't work. I'm guessing something is wrong with this JSON object?
Thank you
There is anything wrong with your json object, check this way to set data on the rootScope. Maybe is helpful for you.
angular.module('app', [])
.factory('currentUser', [
function () {
return {
"id": 12,
"email": "testmail#test.com",
"given_name": "John",
"family_name": "Doe",
"car": "BMW"
};
}])
.run([
'$rootScope', 'currentUser', function ($rootScope, currentUser) {
$rootScope.currentUser = currentUser;
}
]);
then later you can call {{ currentUser.email }} on templates and $scope.currentUser.email on code.
you can update the data on the controllers injecting $rootScope.
First time attempted ractive and got errors. [Object object] printed repeatedly.
Tried to merge both files into one (problem is that the collection and ad are different so how to combine both?)
Advertisements and non-advertisements would be printed out respectively, meaning advertisement items should be printed out first in freewall layout and after that, non-ad items will be printed out.
Somewhere in code i might have gone wrong.
Ractive Template
<script id="thumbnail-tmpl" type="text/ractive">
{{items}}
{{#if advertised}}
<div class="thumb">
<span class="thumb-caption">
{{title}}
<small>{{subtitle}}</small>
</span>
</div>
{{else}}
<div class="thumb">
<span class="thumb-caption">
{{title}}
<small>{{subtitle}}</small>
</span>
</div>
{{/#if advertised}}
{{items}}
</script>
Script
<script>
;(function() {
var finalObj = $.merge(collections, ad);
$.getJSON(finalObj)
.then(function(response){
new Ractive({
el: document.querySelector('[data-freewall]'),
template: '#thumbnail-tmpl',
data:{
items: response,
advertised: response.advertised
}
})
});
})();
</script>
HTML
<div data-freewall></div>
How to combine two json into one? They will be two different files so will pull json from files later.
Advertised is not working for conditional to print ad and non-ad items. Tried tutorial conditional on ractive website..
Is it possible to print ad items first then non-ad items second before laying them out in freewall layout (in case if you don't know what freewall is, http://vnjs.net/www/project/freewall/?
help will be appreciated.
Updated: Still have problems
Keep on seeing test message -
[object Object],success,[object Object],[object Object],success,[object Object]
Also {{/if}} and {{/each}} are giving illegal errors
$.when(
$.getJSON('pathto/test/collection.json'),
$.getJSON('pathto/test/ad.json')
).then(function(collections, advertised){
var items = collections.concat(advertised);
alert(items);
items.sort(function(a, b){
if(a.advertised){
return b.advertised ? 0 : -1;
}
else{
return b.advertised ? 1 : 0;
}
});
var ractive = new Ractive({
el: document.querySelector('[data-freewall]'),
template: '#thumbnail-tmpl',
data:{
items: items
}
});
});
In json files from two urls
{
"advertised": [
{ "title": "Rabbit",
"subtitle": "Nibble",
"advertised": true
},
{ "title": "Dog",
"subtitle": "Woof",
"advertised": true
},
{ "title": "Cat",
"subtitle": "Purr",
"advertised": true
}
]
}
{
"collections": [
{ "title": "Horse",
"subtitle": "Na~~",
"advertised": false
},
{ "title": "Turkey",
"subtitle": "Gobble",
"advertised": false
},
{ "title": "Goat",
"subtitle": "Baaaa",
"advertised": false
},
{ "title": "Snake",
"subtitle": "hissssss",
"advertised": false
}
]
}
Still not seeing anything display on the page - show blank even if errors are corrected. I am wondering if the json files are a reason -- because we can't access to collection and advertised in json files?
Help please and appreciate it
The reason you're getting [object Object] is that you're trying to print {{items}} directly - use {{#each items}}...{{/each}} instead (or just {{#items}}...{{/items}} if you prefer the compact syntax). There's also an error with {{/#if advertised}} - use {{/if}} instead.
The easiest way to combine data from two different JSON files, if you're using jQuery AJAX, is probably to nest the callbacks...
$.getJSON('path/to/collections.json').then(function (collections) {
$.getJSON('path/to/advertised.json').then(function (advertised) {
// code goes here
});
});
...though it would be more ideal to use deferreds so that you can fetch the files in parallel:
$.when(
$.getJSON('path/to/collections.json'),
$.getJSON('path/to/advertised.json')
).then( function ( a, b ) {
var collections = a[0].collections, advertised = b[0].advertised;
// code goes here
});
Now for the code to combine the two arrays. You don't need to use jQuery for this, since you're just concatenating two arrays:
var items = collections.concat( advertised );
You can sort them however you like using items.sort(sortFunction). Feed that into your Ractive instance, and everything works as expected (there's an example sort function there).
Once you've done that, you can use the freewall plugin on the container element.