I am using RestApi and trying to use fetch to jsonplaceholder but it shows undefined in the final result.
enter image description here
This is the code:
function searchUser(event) {
event.preventDefault();
const searched_random_users = random_users.filter((user) => user.email.includes(event.target.value));
products_container.innerHTML = '';
searched_random_users.forEach((random_user) => {
products_container.innerHTML += `
<div class="card col-4">
<h3 class="card-text">${random_user.name}</h3>
<h5 class="card-text">${random_user.username}</h5>
<h5 class="card-text">${random_user.email}</h5>
<h5 class="card-text">${random_user.phone}</h5>
</div>
`;
});
}
random_user_add_button.addEventListener('click', addRandomUser);
function addRandomUser(event) {
event.preventDefault();
fetch('https://jsonplaceholder.typicode.com/users')
.then((response) => response.json())
.then((user) => {
random_users.push(user);
products_container.innerHTML += `
<div class="card col-4">
<h3 class="card-text">${user.name}</h3>
<h5 class="card-text">${user.username}</h5>
<h5 class="card-text">${user.email}</h5>
<h5 class="card-text">${user.phone}</h5>
</div>
`;
});
}
Related
I'm trying to display a preview of some images when a user selects them before he uploads them, but I'm new to vue and I can't figure out how to apply the background image url style to each div (it must be with background image), here is my UploadComponent so far:
<template>
<div class="content">
<h1>{{ title }}</h1>
<div class="btn-container">
<label class="button" for="images">Select Images</label>
<input type="file" id="images" name="images[]" multiple="multiple" #change="selectFiles"/>
</div>
<br><br>
<div class="block">
<h3>Selected images</h3>
<div v-for="image in images" v-bind:key="image" class="image-result" v-bind:style="{ backgroundImage: 'url(' + image + ')' }">
</div>
</div>
<div class="btn-container">
<button type="button" class="submit-btn" v-on:click="uploadImages">Submit Images</button>
</div>
<br><br>
<div class="block">
<h3>Uploaded images</h3>
<div class="files-preview">
</div>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'UploadComponent',
data: () => ({
title: 'Select your images first and then, submit them.',
images: []
}),
methods:{
selectFiles(event){
var selectedFiles = event.target.files;
var i = 0;
for (i = 0; i < selectedFiles.length; i++){
this.images.push(selectedFiles[i]);
}
for (i = 0; i < this.images.length; i++){
let reader = new FileReader(); //instantiate a new file reader
reader.addEventListener('load', function(){
console.log(this);
}.bind(this), false); //add event listener
reader.readAsDataURL(this.images[i]);
}
},
uploadImages(){
const data = new FormData();
data.append('photos', this.images);
axios.post('url')
.then(res => {
console.log(res);
});
}
}
}
</script>
The divs are being created but I'm missing the part where I assign the background image, how can I solve this?
You can't just put a File directly as the parameter for background-image. You started using FileReader but didn't do anything with the result.
Here's what I would do below, I converted your images array into a list of objects that have a file property and a preview property, just to keep the file and preview linked together.
Vue.config.productionTip = false;
Vue.config.devtools = false;
new Vue({
el: "#app",
data: () => {
return {
title: 'Select your images first and then, submit them.',
images: []
}
},
methods: {
selectFiles(event) {
var selectedFiles = event.target.files;
for (var i = 0; i < selectedFiles.length; i++) {
let img = {
file: selectedFiles[i],
preview: null
};
let reader = new FileReader();
reader.addEventListener('load', () => {
img.preview = reader.result;
this.images.push(img);
});
reader.readAsDataURL(selectedFiles[i]);
}
},
uploadImages() {
const data = new FormData();
data.append('photos', this.images.map(image => image.file));
axios.post('url')
.then(res => {
console.log(res);
});
}
}
});
.image-result {
width: 100px;
height: 100px;
background-size: contain;
background-repeat: no-repeat;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app" class="content">
<h1>{{ title }}</h1>
<div class="btn-container">
<label class="button" for="images">Select Images</label>
<input type="file" id="images" name="images[]" multiple="multiple" #change="selectFiles" />
</div>
<br><br>
<div class="block">
<h3>Selected images</h3>
<div id="target-photos">
</div>
<div v-for="image in images" class="image-result" v-bind:style="{ backgroundImage: 'url(' + image.preview + ')' }">
</div>
</div>
<div class="btn-container">
<button type="button" class="submit-btn" v-on:click="uploadImages">Submit Images</button>
</div>
<br><br>
<div class="block">
<h3>Uploaded images</h3>
<div class="files-preview">
</div>
</div>
</div>
Using Angular, I wanted to make an animated display list. The problem is that I am getting this error:
Objects are in the array and show up on the screen, but are not animated.
This is what the HTML code looks like, where I iterate through the table and display the elements that I want to be slowly displayed (animation):
<section [#listAnimation]="articles.length">
<div *ngFor="let article of articles">
<div class="card border-0">
<div class="row no-gutters">
<div class="col-md-4 mt-3">
<div class="container-text-image">
<img [src]="article.mainImageSrc" onerror="this.onerror=null; this.src='..\..\..\assets\notFound.png';" class="card-img" [alt]="article.mainImageFullName">
<div class="label bottom-left">
<h3 class="label-text">
{{article.category}}
</h3>
</div>
</div>
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title mt-0">{{article.title}}</h5>
<p *ngIf="article.description != null" class="card-text description-text">{{article.description}}</p>
</div>
</div>
</div>
</div>
</div>
</section>
This is what the animation code looks like in a component:
const listAnimation = trigger('listAnimation', [
transition('* <=> *', [
query(':enter',
[style({ opacity: 0 }), stagger('60ms', animate('600ms ease-out', style({ opacity: 1 })))],
),
query(':leave',
animate('200ms', style({ opacity: 0 })),
)
])
]);
#Component({
selector: 'app-articles',
templateUrl: './articles.component.html',
styleUrls: ['./articles.component.css'],
animations: [listAnimation]
})
Here's a snippet of code where I get my backend data into an array:
private fetchArticles = (): void => {
this.loaderService.show();
this.articlesService
.getPortalArticles(this.getArticlesParams())
.pipe(
finalize(() => {
this.setArticleMainImg();
this.loaderService.hide();
})
)
.subscribe(
(result) => {
this.articlesCount = result.totalItemsCount;
this.articles = result.items;
},
(error) => { }
);
}
Any ideas what might be causing the error? I realize that I can add {optional: true} to the animation code, but this will only get rid of the error and the animation will still not work.
I have created a partial view and I added the rendering in Index page. I have a button (See More) and every time I am calling controller with ajax and append the new data in the screen.
The problem is when i have no more results to show and a user keeps pressing the button I append multiple times "no results found".
Is there a way to prevent this?
My Index:
<div class="sectionMain" id="stopscroll">
#foreach (var item in Model)
{
if (item.restDetails != null && item.restDetails.Count() != 0)
{
#*<label class="sectionLabels" style="padding-left: 3%;">Restaurants</label>*#
#Html.Hidden("nextItems")
<div id="test" class="sectionSeeMore">
#Html.Partial("_RestaurantDetails", item.restDetails)
</div>
<div class="row">
<div class="col-5"></div>
<div class="col-2" style="text-align:center;">
<button id="btnsubmit" class="buttonCategory" onclick="callseemore(this)" style="font-family:'Fredoka-One';height: 36px;">See More</button>
</div>
<div class="col-5"></div>
</div>
}
break;
}
</div>
<script>
function callseemore(a){
var postcode = $("#inputPostCode").val();
var category = "";
var slides = document.getElementsByClassName("buttonCategoryMain");
for (var i = 0; i < slides.length; i++) {
if (slides.item(i).className == "buttonCategoryMain activebtnMain") {
category = slides.item(i).value;
}
}
var offerButton = document.getElementById("sidebarOfferBtn").classList.value;
var isofferparam = "False";
if (offerButton == "activebtn") {
isofferparam = "True";
}
var nextItems= $("#nextItems").val();
$.ajax({
url: "#Url.Action("RestaurantPaging", "Home")",
type: 'POST',
data: ({ categoryparam: category, postcodeparam: postcode, nextitemparam: nextItems, isOfferparam: isofferparam}),
cache: false,
success: function (result, status, xhr) {
debugger;
$('#tblSeeMore').append(result);
var asf = xhr.getResponseHeader("nextItems");
document.getElementById("nextItems").value =asf ;
}
})
}
</script>
PartialView:
#model IEnumerable<Restaurants.Models.RestaurantDetail>
#if (Model.Count() == 0)
{
<h3 style="text-align:center;font-family:'Fredoka-One'">No Results Found</h3>
}
else
{
<div style="display:flex;flex-flow:row wrap;" id="tblSeeMore">
#foreach (var itemRest in Model)
{
<div class="newRestaurantsSection" id="newRestaurantsSection">
<div class="newRestaurantBox">
#if (itemRest.RestaurantImgPath != null)
{
<img class="restImage" src="#Url.Content(itemRest.RestaurantImgPath)">
}
else
{
<img class="restImage" src="~/Content/Assets/Images/no_image.png" />
}
<div class="restMethods">
#if (itemRest.Pickup)
{
<span style="display: inline-block; border-right: 1px solid lightgrey;padding-right:3px;">
<img class="restMethodImg" src="~/Content/Assets/Images/takeaway.png" />
#*<i class="fas fa-box fa-2x"></i>*#
</span>
}
#if (itemRest.Delivery)
{
<span style="display: inline-block; border-right: 1px solid lightgrey;padding-right:3px;">
#*<i class="fas fa-motorcycle fa-2x"></i>*#
<img class="restMethodImg" src="~/Content/Assets/Images/delivery.png" />
</span>
}
#if (itemRest.OnlinePayments)
{
#*<i class="fas fa-credit-card fa-2x"></i>*#
<img class="restMethodImg" src="~/Content/Assets/Images/visa.png" />
}
</div>
<div class="row restLogoNameKitchenType">
<div class="col-xs-1" style="flex-grow: 0;">
#if (itemRest.Logo != null)
{
<img class="restLogo rounded-circle" src="#Url.Content(itemRest.Logo)">
}
else
{
<img class="restLogo rounded-circle" src="~/Content/Assets/Images/no_image.png" />
}
</div>
<div class="col-xs-9 restKitchenTypeContent">
<label class="restName">#itemRest.RestaurantName</label>
<br />
#{ string kitchenTypeFull = "";}
#foreach (var kitchenType in itemRest.RestaurantCategories)
{
kitchenTypeFull += "," + #kitchenType.Category.Description;
}
#if (kitchenTypeFull.Length > 0)
{
kitchenTypeFull = kitchenTypeFull.TrimStart(new Char[] { ',' });
<label>#kitchenTypeFull</label>
}
</div>
</div>
<div class="row restAddressPhoneOffer">
<div class="col-md-12">
<div class="row">
<div>
<i class="fas fa-map-marker-alt"></i>
</div>
<div class="col" style="margin-left:-20px">
#itemRest.Address1,<br /> #itemRest.Area, #itemRest.PostalCode<br /> #itemRest.City
</div>
</div>
<div class="row">
<div>
<i class="fas fa-phone-alt"></i>
</div>
<div class="col" style="margin-left:-20px">
#itemRest.PhoneNumber
</div>
<div>
#if (itemRest.Offer)
{
<img style="width:60px;" class="restOfferImg" src="~/Content/Assets/Images/gift.png" />
}
else
{
<img style="width:60px;height:60px;" src="~/Content/Assets/Images/no_image _blank.png" />
}
</div>
</div>
</div>
<div class="col-md-12" style="margin-top:10px;margin-bottom:10px;padding-left:0px;padding-right:10px;">
#if (itemRest.MenuLink != null && itemRest.TableLink != null)
{
<div class="rowMenuTableLink">
<button class="rowMenuTableLinkM">
#Html.Raw(itemRest.MenuLink)
</button>
<button class="rowMenuTableLinkT">
#Html.Raw(itemRest.TableLink)
</button>
</div>
}
else if (itemRest.MenuLink == null && itemRest.TableLink == null)
{
<div class="rowTableLink" style="height:44px">
</div>
}
else
{
if (itemRest.MenuLink != null)
{
<div class="col-md-12 rowMenuLink">
<button class="restMenuResrBtns">
#Html.Raw(itemRest.MenuLink)
</button>
</div>
}
if (itemRest.TableLink != null)
{
<div class="col-md-12 rowTableLink">
<button class="restMenuResrBtns">
#Html.Raw(itemRest.TableLink)
</button>
</div>
}
}
</div>
</div>
</div>
</div>
}
</div>
#*#Html.ActionLink("See More", "RestaurantPaging", "Home", new { JSONModel = Json.Encode(Model) }, null)*#
}
<script type="text/javascript">
$(document).ready(function () {
var el = document.getElementsByClassName("newRestaurantBox");
var sidebar = document.getElementById('scrollableSidebar').style.display;
var sidebarclasses = document.getElementById('scrollableSidebar').classList.value;
var newRestaurantsView = document.querySelectorAll("[id='newRestaurantsSection']");
for (var i = 0, ilen = el.length; i < ilen; i++) {
if (sidebarclasses != "sectionSidebar sectionSidebarMin" && sidebar != "none") {
$(newRestaurantsView[i]).addClass('newRestsSectionSidebar');
$(newRestaurantsView[i]).removeClass('newRestaurantsSection');
}
else {
$(newRestaurantsView[i]).addClass('newRestaurantsSection');
$(newRestaurantsView[i]).removeClass('newRestsSectionSidebar');
}
}
})
</script>
print screen example:
You just have to remove the previously appended "No Results Found" and show whatever your post method gives as a response. i.e
Create a separate div inside #tblSeeMore which will contain "No Results Found"
<div id="divNoResults">
</div>
and change your ajax post call to this:
$.ajax({
url: "#Url.Action("RestaurantPaging", "Home")",
type: 'POST',
data: ({ categoryparam: category, postcodeparam: postcode, nextitemparam: nextItems, isOfferparam: isofferparam}),
cache: false,
success: function (result, status, xhr) {
debugger;
if(result == "No Results Found"){
$('#divNoResults').empty();
$('#divNoResults').append(result);
}
else
$('#tblSeeMore').append(result);
//rest of the code
}
})
Or you can just change the display of #divNoResults based on the result from the post method
<div id="divNoResults" style="display:none">
<p>No Results Found</p>
</div>
$.ajax({
url: "#Url.Action("RestaurantPaging", "Home")",
type: 'POST',
data: ({ categoryparam: category, postcodeparam: postcode, nextitemparam: nextItems, isOfferparam: isofferparam}),
cache: false,
success: function (result, status, xhr) {
debugger;
if(result == "No Results Found"){
$('#divNoResults').show();
}
else
$('#tblSeeMore').append(result);
//rest of the code
}
})
I need to show the data I'm fetching, but none is showing, I tried ngIf and ngFor
I'm using angular 6, and trying to display data from movies in the Html, but data seems to be empty, I tried a lot of solutions but none seems to work.
<app-logo></app-logo>
<app-options></app-options>
<div style="display:none;" id="root"></div>
<div class="col s6">
<button routerLink="/halfhour" (click)=searchData() class="options" id="menos-30-min" value="25 min">Menos de 30 min</button>
</div>
<div *ngIf="data?.data">
<ng-container *ngFor="let data of data">
<div class="row">
<div class="col s5">
<img class="backgrounds" src="{{data?.Poster}}">
<div class="background" style="background-image: url({{data?.Poster}}); background-size: cover; background-position: center center; background-repeat: no-repeat; background-attachment: fixed; height:300px; width: 100%;"></div>
</div>
<div class="col s7">
<div class="information">
<p class="title">Title: {{data?.Title}}</p>
<p class="runtime">Runtime: {{data?.Runtime}}</p>
<p class="other-data">Year: {{data?.Year}}</p>
<p class="other-data"> Genre: {{data?.Genre}}</p>
<p class="resumen"> Plot: {{data?.Plot}}</p>
<p class="other-data"> Tipo: {{data?.Type}}</p>
</div>
</div>
</div>
</ng-container>
</div>
<h1>{{id}}</h1>
#Component({
selector: 'app-half-hour',
templateUrl: './half-hour.component.html',
styleUrls: ['./half-hour.component.css']
})
export class HalfHourComponent implements OnInit {
constructor(private http: Http) {
}
data: any [];
id;
getRandom() {
const omdbData = ['0386676', '1865718', '0098904', '4508902', '0460649', '2861424', '0108778', '1305826', '0096697', '0149460'];
const randomItem = omdbData[Math.floor(Math.random() * omdbData.length - 1) + 1];
return this.id = randomItem;
}
searchData() {
this.getRandom();
this.http
.get('https://cors-anywhere.herokuapp.com/http://www.omdbapi.com/?i=tt' + this.id + '&apikey=')
.pipe(map((res: Response) => res.json()))
.subscribe(data => {
this.data = Array.of(this.data);
console.log(data);
console.log(this.id);
});
}
ngOnInit() {
}
}
The properties of data show as empty.
HTTP request call should be added inside ngOninit(). Here is the updated code
ngOnInit(){
this.data = [];
this.getRandom();
this.http
.get('https://cors-anywhere.herokuapp.com/http://www.omdbapi.com/?i=tt' + this.id + '&apikey=')
.pipe(map((res: Response) => res.json()))
.subscribe(data => {
this.data = (data) ? data.slice() : [];
console.log(data);
console.log(this.id);
});
}
Im getting the above error message but dont understand why as everything is defined as far as i can see.
Heres my html:
Apparently, the error occurs on line 1
<div *ngIf="pager.index < quiz.questions.length">
<h5 class="flow-text"><span [innerHTML]="quiz.questions[pager.index].text"></span></h5>
<br>
<div class="row text-left">
<div class="col s12 m12" *ngFor="let answer of quiz.questions[pager.index].answers">
<div class="answer">
<input type="checkbox" [checked]="answer.selected" (change)="onSelect(answer);"/><label class="black-text flow-text"> {{answer.text}} </label>
</div>
</div>
</div>
<footer class="row">
<div class="col s6 m6"></div>
<div class="col s6 m6">
<div *ngIf="pager.index < quiz.questions.length - 1">
<button id="nextButton" class="btn-flat black-text right flow-text" (click)="goTo(pager.index + 1);">Next</button>
</div>
<div *ngIf="pager.index == quiz.questions.length - 1">
<button id="nextButton" class="btn-flat black-text right flow-text" (click)="goTo(pager.index + 1);">Finish</button>
</div>
</div>
</footer>
</div>
<div *ngIf="pager.index == quiz.questions.length">
{{ selections }}
</div>
here is the component.ts
import { Component, OnInit, EventEmitter } from '#angular/core';
import { QuizService } from '../services/quiz.service';
import { Answer, Question, Quiz } from '../models/index';
import {MaterializeAction} from "angular2-materialize";
#Component({
selector: 'app-quiz',
templateUrl: './quiz.component.html',
styleUrls: ['./quiz.component.sass'],
providers: [QuizService]
})
export class QuizComponent implements OnInit {
quiz: Quiz;
pager = {
index: 0,
size: 1,
count: 1
};
selections: [string]
constructor(private quizService: QuizService) { }
ngOnInit() {
this.loadQuiz()
}
loadQuiz() {
this.quizService.get().subscribe(res => {
this.quiz = new Quiz(res);
this.pager.count = this.quiz.questions.length;
});
}
goTo(index: number) {
if (index >= 0 ) {
this.pager.index = index;
}
}
onSelect(answer: Answer) {
if (answer.selected = true) {
this.selections.splice(this.selections.indexOf(answer.text), 1);
} else {
this.selections.push(answer.text);
}
answer.selected = !answer.selected
}
}
i dont understand why its happening as despite the error compiling the code works and runs as it should, apart from the check boxes which i have asked in another question.
You should be using safe navigation operator in this case
*ngIf="pager.index < quiz?.questions?.length"
And
*ngIf="pager.index == quiz?.questions?.length"
It seems quiz variable is undefined, define it like this quiz: Quiz={}