I am making an AJAX call to a JSON file containing thumbnail images and website urls, I am then listing each out using AngularJS directive ng-repeat to list. The problem is the thumbnails and website urls are no longer populating my page. I reformatted my JSON file data into an array because I will be adding multiple arrays to my JSON file that will contain different objects to be used on other pages throughout the website.
JSON:
{"websites":[
{
"thumbnail": "thumbnail1.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail2.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail3.jpg",
"website": "http://somewebsite.com"
}
]}
Angular:
angular.module('myApp')
.constant("dataUrl", "../json/data.json")
.controller("websitesController", function($scope, $http, dataUrl){
$scope.data ={};
$http.get(dataUrl)
.success(function(data){
$scope.data.projects = data;
})
.error(function(error){
$scope.data.error = error;
});
});
HTML:
<ul ng-controller="websitesController">
<li ng-repeat="item in data.projects">
<img ng-src="{{item.thumbnail}}" />
<div>
<a ng-href="{{item.website}}" target="_blank"><b>Website</b></a>
</div>
</li>
<ul>
Your JSON data has a key called websites that is storing an array of websites. You are adding that object with the websites key as your $scope.data.projects. To access the array of websites, you need to look into that object, i.e., you should assign $scope.data.projects to data.websites. Change your success handler as follows:
.success(function(data){
$scope.data.projects = data.websites;
})
EDIT: data examples --
Your response data looks like this:
{"websites":[
{
"thumbnail": "thumbnail1.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail2.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail3.jpg",
"website": "http://somewebsite.com"
}
]}
But, you want it to look like this (parsed too) in order to use it in your ng-repeat:
[
{
"thumbnail": "thumbnail1.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail2.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail3.jpg",
"website": "http://somewebsite.com"
}
]
It should be $scope.data.projects=data.websites;
Related
My client is ReactJS and server is Java Jersey based.
Java Jersey produces the following nested JSON.
[
{
"projectname": "BMI",
"testRun": "5934391890034305",
"numberOfTestcases": "3",
"timeNowString": "Mon Sep 21 21:17:34 IST 2020",
"oidobjList": [
{
"oid": "5f68cb16f01c7607230d1fcd"
},
{
"oid": "5f68cb16f01c7607230d1fcf"
},
{
"oid": "5f68cb16f01c7607230d1fd1"
}
],
"fileNameObjList": [
{
"fileName": "Basispath_BMI_0_out.gif"
},
{
"fileName": "Basispath_BMI_1_out.gif"
},
{
"fileName": "Basispath_BMI_2_out.gif"
}
]
},
{
"projectname": "BMI",
"testRun": "3320691551029718",
"numberOfTestcases": "3",
"timeNowString": "Mon Sep 21 18:37:54 IST 2020",
"oidobjList": [
{
"oid": "5f68a5aaf01ca8f40b42a4e7"
},
{
"oid": "5f68a5aaf01ca8f40b42a4e9"
},
{
"oid": "5f68a5aaf01ca8f40b42a4eb"
}
],
"fileNameObjList": [
{
"fileName": "Basispath_BMI_0_out.gif"
},
{
"fileName": "Basispath_BMI_1_out.gif"
},
{
"fileName": "Basispath_BMI_2_out.gif"
}
]
}
]
My ReactJS code is as follows:
{this.props.articles.map((el, index) => (
<Table
tableHeaderColor="primary"
tableHead={["Parameter", "Value"]}
tableData={[
["Project Name: ", el.projectname],
["Test Run: ", el.testRun],
["No Of TestCases: ", el.numberOfTestcases],
// ["File Name: ", el.fileNameObjList],
["Time Stamp: ", el.timeNowString],
]}
/>
))}
The response received in client side is attached in the image
Questions:
How to render nested JSON elements in ReactJS material-ui based tableData?
How to get filenames using map and index?
Please provide solution to this issue.
1 - First you need to decide how you will render your table. If you just want to render a single table for all your nested arrays, you can generate a single array of it and render normally. You can also render multipla tables, one for each nested array, it's your choice as a programmer.
2 - You can get filenames field by doing an simple map in fileNameObjList. But if you want a single array of filenames, you can either use Array.reduce or combine the mapped arrays with Array.concat or by pushing every item with an for loop
and also, I'm not sure but i don't think Material UI table is used that way. You may be using another data-table component that uses MUI
How can I display "empName" and other details of this JSON in my table component in loop?
I'm using a third party API which provides a nested JSON object in return when I send employerID to the API URL.
After subscribing I'm storing the response in a var "AllPostedJobs"
{
"status": "All Jobs",
"result": [
{
"_id": "5c90fd3cfc7f3b0017803319",
"job_title": "Web Designer",
"job_desc": "test description ...",
"location": "Mangalore",
"experiance": "Freshers",
"job_type": "Full-Time",
"salary_package": "3L",
"job_keywords": "Photoshop, Illustrator",
"email_id": "hr#shreemithra.com",
"employerID": "5c7e99c2a7a9eb00174de2b2",
"company_name": "Shreemithra Designs",
"AppliedStudentDetails": [
{
"_id": "5c9393c1a918d60017de7e55",
"empName": "Anup",
"empID": "5c939375a918d60017de7e53",
"job_title": "Web Designer"
}
],
"__v": 1
},
{
"_id": "5c913570cb78a100177ab23a",
"job_title": "Full Stack Developer",
"job_desc": "htjhsv dhsd jh jds fjshgdfkhsdmhfd;lw eiwiwemsd. This is a sample job description.",
"location": "Pune",
"experiance": "2 Years",
"job_type": "Part-Time",
"salary_package": "8L - 10L PA",
"job_keywords": "Angular, Node JS, React, HTML5. CSS3",
"email_id": "info#shreemithra.com",
"employerID": "5c7e99c2a7a9eb00174de2b2",
"company_name": "Shreemithra Designs",
"AppliedStudentDetails": [
{
"_id": "5c9393c9a918d60017de7e56",
"empName": "Anup",
"empID": "5c939375a918d60017de7e53",
"job_title": "Full Stack Developer"
},
{
"_id": "5ca60fa5ba17730017182ca8",
"empName": "Amit Pateriya",
"empID": "5c7795acfd39640017ca4c37",
"job_title": "Full Stack Developer"
}
],
"__v": 2
}
]
}
The simplest way is when you receive data from api then send it to a
function then apply multiples loop alter your data by add keys in
front of data and that values for example
var data = [{"id":1, "name":"smith","applicant":{"roll": 32,"class":10}}];
data[0].applicantRoll = data[0].applicant.roll;
data[0].applicantClass = data[0].applicant.class;
now you can apply *ngfor easily, try this.
You can bind display data by binding controls in an HTML template to properties of your component.
The easiest way to display a component property is to bind the property name through interpolation. With interpolation, you put the property name in the view template, enclosed in double curly braces:{{AllPostedJobs.status}}.
<div id="result-container" *ngFor="let record of AllPostedJobs.result">
<another-component [record]= "record"></another-component>
</div>
Or depending on your need you can hand over entire result data to another-component.
Now your another-component, should have #Input defined to handle the incoming data:
export class AnotherComponent {
#Input() record: Array<any>;
In your another-component template:
<div *ngFor="let student of record?.AppliedStudentDetails">
<span>{{student.empName}}</span>
<span [innerText]="student.job_title"></span>
</div>
I have a json message in the below format returned from the server. I using jquery data tables to display the data in the table. I am getting some error when trying to use jquery data table. Please let me know where I am going wrong in the configuration of jquery data table to display the below json format. The configuration for jquery data table along with the json data is given below:
$(function () {
$('#dataTable').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aaData": [
{
"links": [
{
"rel": "self", // the first column data which should be displayed as link
"href": "http://localhost:8080/16"
}
],
"Country": "INDIA",
"state": "Karnataka",
"city": "Bangalore",
"cityId": 16
},
{
"links": [
{
"rel": "self",
"href": "http://localhost:8080/17"
}
],
"Country": "INDIA",
"state": "Tamilnadu",
"city": "Chennai",
"cityId": 17
}
]
});
A little look up on the jquery datatables documentation helped me to get to the solution. The trick is to use the mData and then map the columns to the attributes from the json. The link for the same is mentioned below:
http://datatables.net/release-datatables/examples/server_side/object_data.html
I'm building a phone directory app using AngularJS with JSON data. I'm new to AngularJS and JSON.
I went through the "Phonecat" tutorial on the AngularJS site and was able to build a test directory with JSON data that was similar to the tutorial.
I hit a roadblock using what I learned there and trying to build something with real data; the JSON objects have multiple arrays and I've not been able to figure out how to access the data in the arrays...
I tried to replicate my issue here: http://jsfiddle.net/4ykNX/
Thanks in advance!
//employee.json
{
"resultTruncated": false,
"containsSecureData": false,
"entries": [
{
"type": "employee",
"firstName": "Any",
"lastName": "One",
"address": [
{
"streetOne": "123 Long Street",
"streetTwo": "Big Building",
"streetThree": "Room 456",
"city": "City",
"state": "ST",
"zip": "12345"
}
],
"phone": [
{
"area": "111",
"number": "222-3333",
"extn": "444"
}
],
"email": "any#one.edu"
}
]
}
You are trying to set $scope.employees to data, when it should be data.entries
'use strict';
function EmpListCtrl($scope, $http) {
$http.get('employees.json').success(function(data) {
$scope.employees = data.entries;
});
}
Then you need to reference employee.phone instead of phone:
<div ng-app>
<div ng-controller="EmpListCtrl">
<h1>Employees</h1>
<ul>
<li ng-repeat="employee in employees">
{{employee.lastName}}, {{employee.firstName}}<br/>
{{employee.email}}
<ul>
<li ng-repeat="num in employee.phone">
{{num.area}}-{{num.number}}-{{num.extn}}
</li>
</ul>
</li>
</ul>
</div>
</div>
Here's a plnkr: http://plnkr.co/edit/L2nMu0?p=preview
Can someone point out to me what I may be doing wrong here? I have a controller pulling data from a JSON file on my server using the $http service, and passing it through an attribute to a directive. The problem is that even though I only see 4 objects in my JSON looping through it gives me 325. Further more none of the attributes are accessible to me.
My JSON
[{
"name": "Cute Shirt",
"Type": "Shirt",
"Size": "S,M,L,XL",
"Color": "R,G,B",
"SRC": "img/shirt.png"
}
,
{
"name": "Cute Shirt",
"Type": "Shirt",
"Size": "S,M,L,XL",
"Color": "R,G,B",
"SRC": "img/shirt.png"
}
,
{
"name": "Cute Shirt",
"Type": "Shirt",
"Size": "S,M,L,XL",
"Color": "R,G,B",
"SRC": "img/shirt.png"
}
,
{
"name": "Cute Shirt",
"Type": "Shirt",
"Size": "S,M,L,XL",
"Color": "R,G,B",
"SRC": "img/shirt.png"
}
]
My Controller
"use strict";
function itemControl ($http,$scope) {
$http.get('doc/products.json' ).success(function(prodata){$scope.data = prodata;});
}
My Directive
app.directive("showcase", function() {
return {
restrict: "A",
template: '{{stuff.length}}',
scope: {
stuff: "#"
}
};
});
And Finally the HTML
<div ng-controller="itemControl">
<div showcase stuff="{{data}}"></div>
</div>
From the AngularJS documentation:
# or #attr - bind a local scope property to the value of DOM attribute. The result is always a string since DOM attributes are strings.
Using the = will help
= or =attr - set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute.
You will want to change your <div showcase stuff="{{data}}"></div> to <div showcase stuff="data"></div>
Change # to =
# means it will interpolate the object to string.