Retrieve Data from JSON Data Structure in Vue.js - json

I want retrieve Data from meal table (json data schema) to a table form in the browser. The retrievement works but unfortunately I do not know how to retrieve the data of the weekdays from my meal table and show them in the table.
How can I retrieve Monday, Tuesday, Wednesday, Thursday and Friday???
JSON DATA SCHEMA:
// 20200901152958
// http://localhost:8080/mealtable/weekly/1
{
"id": 1,
"calendarWeek": 1,
"mealTableWeek": {
"Monday": {
"id": 4,
"name": "Burger",
"type": "fleischhaltig",
"price": 2.60
},
"Tuesday": {
"id": 3,
"name": "Reishänchenpfanne",
"type": "fleischhaltig",
"price": 2.60
},
"Wednesday": {
"id": 1,
"name": "Vollkornnudeln",
"type": "vegan",
"price": 2.30
},
"Thursday": {
"id": 5,
"name": "Kartoffelpüree",
"type": "vegetarisch",
"price": 2.30
},
"Friday": {
"id": 2,
"name": "Falafel",
"type": "vegan",
"price": 1.90
}
}
}
MealTablWeekComponent.vue:
<template>
<div class="container">
<h3>Meal Table</h3>
<div class="container">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th>Meal</th>
<th>Type</th>
<th>Price in $</th>
</tr>
</thead>
<tbody>
<tr v-for="d in mealTables.mealTableWeek" v-bind:key="d.id">
<th></th>
<td>{{d.name}}</td>
<td>{{d.type}}</td>
<td>{{d.price}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<script>
import MealTableDataService from "#/service/MealTableDataService";
export default {
name: "MealTableWeekComponent",
data() {
return {
mealTables: {
id: " ",
mealTableWeek: {
name: {
id: " ",
name: " ",
type: " ",
price: " "
}
}
}
};
},
methods: {
refreshMealTable() {
const id = this.$route.params.id;
MealTableDataService.retrieveMealTableById(id)
.then(response => {
console.log(response)
this.mealTables = response.data;
});
}
},
created() {
this.refreshMealTable();
}
};
</script>
<style scoped lang="scss">
</style>
View in the Browser:

<tr v-for="(d, key) in mealTables.mealTableWeek" v-bind:key="d.id">
<td>{{key}}</td>
<td>{{d.name}}</td>
<td>{{d.type}}</td>
<td>{{d.price}}</td>
</tr>

Related

How do I group HTML table with the same value from array (Javascript)

I have an array with appointment data like this which I want to make tables from:
[
{
day: '20/10/2020',
subject: 'meeting',
client: 'Rob'
location: 'Office
},
{
day: '21/10/2020',
subject: 'meeting',
client: 'Lisa'
location: 'Town'
},
{
day: '21/10/2020',
subject: 'meeting',
client: 'Kevin'
location: 'Office
},
{
day: '22/10/2020',
subject: 'meeting',
client: 'Kevin'
location: 'Home'
}
]
my html file:
<div *ngFor="let appointment of appointments" class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th> Day </th>
<th>Where </th>
<th> Client</th>
<th>Subject</th>
</tr>
</thead>
<tfoot>
<tr>
<td> <small>{{appointment.day}}</small></td>
<td> <small>{{appointment.location}} </small> </td>
<td><small>{{appointment.client}}</small> </td>
<td><small>{{appointment.subject}} </small></td>
</tfoot>
</table>
</div>
This produces a table per appointment, but how can I make it such that the appointments on the same day appear below eachother, without theader inbetween. So like this: (visualisation)
any help is appreciated
There is a very simple way to solve your issue, let's do it. We will make use of the lodash library, so first of all you will need to import.
Installing via NPM:
npm i lodash
npm i --save-dev #types/lodash
And importing to our project:
import * as _ from 'lodash';
So then the magic happens, with our just imported friend and its groupBy() method:
let result = _.groupBy(this.appointments, (appointment) => {
return appointments.day;
});
console.log(result);
The result from the console will be:
{
"20/10/2020": [
{
"day": "20/10/2020",
"subject": "meeting",
"client": "Rob",
"location": "Office"
}
],
"21/10/2020": [
{
"day": "21/10/2020",
"subject": "meeting",
"client": "Lisa",
"location": "Town"
},
{
"day": "21/10/2020",
"subject": "meeting",
"client": "Kevin",
"location": "Office"
}
],
"22/10/2020": [
{
"day": "22/10/2020",
"subject": "meeting",
"client": "Kevin",
"location": "Home"
}
]
}

How to fetch nested JSON to table with VueJs?

I need to create a table with Vue and the data are from a JSON. I don't know how to set for the td table tag the "Jogosultsagok" two "Nev" values.
I tried row.Jogosultsagok[0].Nev format, but this not work, I write down the code parts and the final error for this.
HTML
<tbody>
<tr v-for="row in get_rows()">
<td>{{row.Id}}</td>
<td>{{row.Nev}}</td>
<td>{{row.Jogosultsagok[0].Nev}}</td>
<td>{{row.Jogosultsagok[1].Nev}}</td>
</tr>
</tbody>
Vue
data: {
rows:[]
},
created:function() {
fetch('https://api.myjson.com/bins/7jr55')
.then(res => res.json())
.then(res => {
this.rows = res;
})
},
methods: {
"get_rows": function get_rows() {
var start = (this.currentPage-1) * this.elementsPerPage;
var end = start + this.elementsPerPage;
return this.rows.slice(start, end);
},
},
JSON
[
{
"Id": 1,
"Nev": "László",
"Jogosultsagok": [
{
"Id": 1,
"Nev": "Módosítás"
},
{
"Id": 2,
"Nev": "Olvasás"
}
],
"Eletkor": 25,
"Regisztralt": "2019-01-31 06:45:51.557Z",
"Munkahely": {
"Id": 12,
"Nev": "Cég 1"
}
},
{
"Id": 2,
"Nev": "Péter",
"Jogosultsagok": [
{
"Id": 2,
"Nev": "Olvasás"
}
],
"Eletkor": 44,
"Regisztralt": "2011-01-31 09:23:51.234Z",
"Munkahely": {
"Id": 34,
"Nev": "Cég 2"
}
},
...
I get this error: TypeError: Cannot read property 'Nev' of undefined
The problem occures because the JSON response is not consistent:
row.Jogosultsagok[1] is not always defined as the nested Array sometimes only contains one element.
You can fix the error by leaving the table cell empty in case the value is missing:
<template>
<tbody>
<tr v-for="row in get_rows()">
<td>{{row.Id}}</td>
<td>{{row.Nev}}</td>
<td>{{row.Jogosultsagok[0].Nev}}</td>
<td v-if="row.Jogosultsagok[1]">{{row.Jogosultsagok[1].Nev}}</td>
<td v-else></td>
</tr>
</tbody>
</template>

Create a table from REST object by setting data in columnar manner

I have a REST service, which returns this object:
[
{
"id": 100,
"value": "2452"
},
{
"id": 100,
"value": "2458"
},
{
"id": 1,
"value": "2457"
},
{
"id": 1,
"value": "2459"
},
{
"id": 4,
"value": "2460"
},
{
"id": 5,
"value": "3458"
}
]
Now, using this GET service, I want the following table to be built in angular and show it in UI.
100 1 4 5
-------------------
2452 2457 2460 3458
2458 2459
i.e. the unique ids should create the header and the list of values associates to each header value will be appended to respective column.
I have tried something with ng-repeat like this:
<table border="1">
<tr>
<th ng-repeat="column in cols">{{column.id}}</th>
</tr>
<tr>
<td ng-repeat="column in cols">
<md-list>
<md-list-item class="md-2-line" ng-repeat="val in column.values">
<md-checkbox ng-model="item.done"></md-checkbox>
<div class="md-list-item-text">
??
</div>
</md-list-item>
</md-list>
</td>
</tr>
But still wondering how to do the same? Please help.
Try use groupBy filter.
var app = angular.module('anApp', ['angular.filter']);
app.controller('aCtrl', function($scope) {
$scope.data = [
{
"id": 100,
"value": "2452"
},
{
"id": 100,
"value": "2458"
},
{
"id": 1,
"value": "2457"
},
{
"id": 1,
"value": "2459"
},
{
"id": 4,
"value": "2460"
},
{
"id": 5,
"value": "3458"
}
];
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
<div ng-app="anApp" ng-controller="aCtrl">
<table border="1">
<tr>
<th ng-repeat="col in data | groupBy: 'id'">{{col[0].id}}</th>
</tr>
<tbody>
<tr>
<td ng-repeat="col in data | groupBy: 'id'">
<table>
<tr ng-repeat="c in col">
<td> {{c.value}}</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</div>

How To Total Sum Inside Of Array Values In Angularjs?

I am using MEAN stack in my application with AngularJS as my front-end. How to total sum inside of array values in angularjs , actually we have used ng-repeat inside of one more ng-repeat to fetch the value in table and I got the fetched value like 250 in Sprice then I'm expecting to totalsum the sprice value like as a ans= 250.
My Plunker.
We have used two ng-repeat to get the sprice values in table because sprice is in inside of the [array].
We need to calculate total sum of sprice of value in table.
We have filter functionality to calculate the totalsum of ng-module value if it is in without array.
Actually we dont' know how to use resultValue= in ng-repeat.
For example: sprice value is 250 in table means the totalsum value need to be in 250, if sprice value is 250 and 300 means expecting answer like 550.
Without array totalsum functionality: My Plunker
With array totalsum functionality: My Plunker
My Controller:
.filter('sumOfValue', function () {
return function (data, key) {
debugger;
if (angular.isUndefined(data) && angular.isUndefined(key))
return 0;
var sum = 0;
angular.forEach(data,function(v,k){
sum = sum + parseFloat(v[key]);
});
return sum.toFixed(2);
}
})
My Data:-
$scope.orders = [
{
"_id": "5836b64083d9ce0f0078eae8",
"user": {
"_id": "579bdf6123f37f0e00a40deb",
"displayName": "Table 1"
},
"__v": 8,
"total": "1824",
"ordercar": [],
"orderfood": [
{
"qty": "1",
"confirm": "placed",
"sprice": 250,
"price": 250,
"customise": "With Onion,Without Onion",
"name": "Baasha Pizza"
}
],
"phone": null,
"order_source": "",
"comment": "",
"payment_mode": "",
"nop": null,
"rating": null,
"bill": false,
"complete": false,
"laundry": false,
"clean": false,
"roomservice": false,
"napkin": false,
"waiter": false,
"water": false,
"name": "fgg",
"created": "2016-11-24T09:43:28.413Z",
"isCurrentUserOwner": true
}]
My Html:-
<table ng-table="tableParams" class="table table-bordered ">
<thead>
<tr>
<th rowspan="2">s.no</th>
<th rowspan="2"> sprice </th>
</tr>
</thead>
<tr ng-repeat="order in orders">
<td>{{$index + 1}}</td>
<td ng-repeat="ram in order.orderfood">{{ram.sprice }}</td>
</tr>
<tr>
<td>sum</td>
<td>{{resultValue | sumOfValue:'sprice'}}</td>
</tr>
</table>
*We expecting for total sum in array values.
Used ng-repeat:
<tr ng-repeat="order in orders">
<td>{{$index + 1}}</td>
<td ng-repeat="ram in order.orderfood">{{ram.sprice }}</td>
ng-repeat is not looping properly .. I fixed it .. Check below snippet and also i added one more array to it.......... let me know..
var app = angular.module('plunker', []);
app.filter('sumOfValue', function () {
return function (data, key) {
debugger;
if (angular.isUndefined(data) && angular.isUndefined(key))
return 0;
var sum = 0;
angular.forEach(data,function(v,k){
sum = sum + parseFloat(v[key]);
});
return sum.toFixed(2);
}
}).controller('MainCtrl', function($scope) {
$scope.orders = [
{
"_id": "5836b64083d9ce0f0078eae8",
"user": {
"_id": "579bdf6123f37f0e00a40deb",
"displayName": "Table 1"
},
"__v": 8,
"total": "1824",
"ordercar": [],
"orderfood": [
{
"qty": "1",
"confirm": "placed",
"sprice": 250,
"price": 250,
"customise": "With Onion,Without Onion",
"name": "Baasha Pizza"
},
{
"qty": "2",
"confirm": "placed",
"sprice": 250,
"price": 250,
"customise": "With Onion,Without Onion",
"name": "Baasha Pizza"
}
],
"phone": null,
"order_source": "",
"comment": "",
"payment_mode": "",
"nop": null,
"rating": null,
"bill": false,
"complete": false,
"laundry": false,
"clean": false,
"roomservice": false,
"napkin": false,
"waiter": false,
"water": false,
"name": "fgg",
"created": "2016-11-24T09:43:28.413Z",
"isCurrentUserOwner": true
}]
});
body {
font-size: 14px;
}
table
{
border-collapse:collapse;
}
table, td, th
{
border:1px solid black;
}
td{
padding: 2px;
}
.servicetaxinclusivetrue:before{
color: green!important;
content: "\f00c";
}
.servicetaxinclusivefalse:before{
color: red!important;
content: "\f00d";
}
.servicetaxexclusivetrue:before{
color: green!important;
content: "\f00c";
}
.servicetaxexclusivefalse:before{
color: red!important;
content: "\f00d";
}
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<script data-require="angular.js#1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
</head>
<body ng-controller="MainCtrl">
<table ng-table="tableParams" class="table table-bordered ">
<thead>
<tr>
<th rowspan="2">s.no</th>
<th rowspan="2"> sprice </th>
</tr>
</thead>
<tbody ng-repeat="order in orders">
<tr ng-repeat="ram in resultValue=(order.orderfood)">
<td>{{$index + 1}}</td>
<td >{{ram.sprice }}</td>
</tr>
<tr>
<td>sum</td>
<td>{{resultValue | sumOfValue:'sprice'}}</td>
</tr>
</tbody>
</table>
</body>
</html>
Here you can call function using "ng-init", so that angularjs will call function before execution of "ng-repeat".
In that function you can perform iteration and calculate total and hold it in scope variable to display it.

Angular.js - Nested JSON scope with Angular

{
"imports": {
"imported": [
{
"date": "19/9/2014",
"item": [
{
"sn": "3366698",
"type": "Food",
"weight": "10tn."
},
{
"sn": "3366699",
"type": "Eqipment",
"weight": "20kg."
}
]
},
{
"date": "20/9/2014",
"item": [
{
"sn": "3366700",
"type": "Electronics",
"weight": "100pt."
},
{
"sn": "3366701",
"type": "Food",
"weight": "5tn."
}
]
}
]
}
}
I have this json and I am not sure if it's in the right structure. I am trying to render each item type (duplicates included) as table header by the following $.getJSON method:
$scope.items = data.imports.item;
and HTML as:
<table border="1" >
<thead>
<tr>
<th ng-repeat="item in items">{{item.type}}</th>
</tr>
</thead>
</table>
But I couldn't succeed. What am I doing wrong?
EDIT: jsfiddler
I don't see you are using the imported propery at all, try
$scope.items = data.imports.imported;
since imported is your array and not item.
<table border="1" >
<thead>
<tr>
<th ng-repeat="item in items">{{item.type}}</th>
</tr>
</thead>
</table>
Your json ist broken, paste your json here:
http://jsonformatter.curiousconcept.com/
You'll see that data.imports.item would be undefined.
Correct JSON to access would look like:
{
"imports": {
"item": [
{
"sn": "3366698",
"type": "Food",
"weight": "10tn."
},
{
"sn": "3366699",
"type": "Eqipment",
"weight": "20kg."
}
]
}
}
Also access your data after:
<th ng-repeat="item in items">
{{item["type"]}}
</th>