how does foreach works in react-redux - node.js

I am making a nodejs app which can fetch flights from the KIWI api, it returns a json list, and since you parameters like from and to, it should return a list of flights.
I can successfully get that, but when I want to display everything I dont know how to do it.
This is my render:
render(){
return (
<table className="table table-hover">
<thead>
<tr>
<th>Flight #</th>
<th>From</th>
<th>To</th>
</tr>
</thead>
<tbody>
{this.props.flights.map(this.renderFlights)}
</tbody>
</table>
);
}
and
renderFlights(flightData){
// add key to make them unique
return(
<tr>
<td>{flightData.data[0].id}</td>
<td>{flightData.data[0].mapIdfrom}</td>
<td>{flightData.data[0].mapIdto}</td>
</tr>
);
}
{this.props.flights.map(this.renderFlights)} just maps the first one in the array, I know that I have to use foreach, but I dont know how I can use this to print everything in the list, flight id plus the from and to, so when u fetch the flights u get around 15, and I want to be able to display all the 15 flights can someone help me out here?
this returns undefined for forEach:
<tbody>
{
this.props.flights.array.forEach(element => {
this.renderFlights
})
}
</tbody>

I found your API and test interface here: https://skypickerbookingapi1.docs.apiary.io/#reference/check-flights/checkflights/check_flights?console=1
So it seems that you are getting this object for your response:
{
"server_time": 1516568910,
"flights_checked": false,
"extra_fee": 0,
// blah blah,
"flights": [
// first flight
{
"bags_recheck_required": false,
"dtime_unix": 1524646800,
"extra": "",
"atime_unix": 1524651600,
"priority_boarding": {
"currency": null,
"price": null,
"is_possible": false
},
"price": 313,
"currency": "NOK",
"price_new": 313,
// blah blah
},
// second flight
{
"bags_recheck_required": true,
"dtime_unix": 1524683400,
"extra": "",
"atime_unix": 1524691800,
"priority_boarding": {
"currency": null,
"price": null,
"is_possible": false
},
"price": 1560,
"currency": "NOK",
"price_new": 1560,
// blah blah
},
// more and more flights
So I'm guessing that your "this.props.flights" is referring to the "flights" property of the above object.
Now, you need to use "map", not "foreach":
this.props.flights.map(this.renderFlights) //this is correct
And your callback function should be:
renderFlights(one_single_flight){
// add key to make them unique
return(
<tr>
<td>{one_single_flight.id}</td>
<td>{one_single_flight.src_country}</td>
<td>{one_single_flight.dst_country}</td>
</tr>
);
}
}

This is how I was able to get the result I wanted:
renderFlights(flightData){
const result = flightData._results;
var rows = [];
for (var i = 0; i < result; i++) {
rows.push(
<tr key={i} >
<td>{flightData.data[i].mapIdfrom}</td>,
<td>{flightData.data[i].mapIdto}</td>,
</tr>
);
}
return <tbody>{rows}</tbody>;
}
This prints all the available flights for some certain date.

Related

How to convert the few jSON value within an array to HTML table and return as JSON itself in groovy

I am new to groovy , so please help me here , how to achieve the below req. The below is the Input jSon array, I want to write Groovy/JS to loop over and form html tags for fields within "Organization"
[
{
"Organization_Role_Assignments_group" : [
{
"Organization" : "Global",
"Organization_Type" : "Location Hierarchy",
"Role_Name" : "Business Process Adviser"
},
{
"Organization" : " Technology)",
"Organization_Type" : "Supervisory",
"Role_Name" : "Manager"
}
],
"NT_Name" : "Test",
"Badge_ID" : "2",
"AttachmentLink" : "<br /><a xml=\"lang\" href=\"https:">Click here to
view </a>"
}
]
From the above I need to create HTML HTML table and return as JSON value
[
{
"Value" :
"
<table style="width: 100%;" border="1"><tbody><tr><td style="width: 30%;font-weight:bold;font-
size:100%;">Role Name</td>
<td style="width: 50%;font-weight:bold;font-size:100%;">Organization</td>
<td style="width: 25%;font-weight:bold;font-size:100%;">Organization Type</td>
</tr><tr><td style="font-size:90%;">Manager</td>
<td style="font-size:90%;">1</td>
<td style="font-size:90%;">Cost Center</td></tr><tr>
<td style="font-size:90%;">Cost Center Manager</td>
<td style="font-size:90%;">3</td><td style="font-size:90%;">Cost Center</td></tr><tr>
</tr></tbody></table><br>
<a href="URL">
Click here to view</a>
",
"User_Name":"Test",
"URL":"someURL"
}]
I am able to generate output using the below script: However the data is split into two elements and I need it in one :
String HTML_ORGANIZATION_TABLE_HEADER = "<table style=\"width:
100%;\" border=\"1\"><tbody><tr><td style=\"width: 30%;font-
weight:bold;font-size:100%;\">Role Name</td><td style=\"width:
50%;font-weight:bold;font-size:100%;\">Organization</td><td
style=\"width: 25%;font-weight:bold;font-size:100%;\">Organization
Type</td></tr>";
String HTML_TABLE_ROW_START = "<tr><td style=\"font-
size:90%;\">";
String HTML_TABLE_CELL = "</td><td style=\"font-size:90%;\">";
String HTML_TABLE_ROW_END = "</td></tr>";
String HTML_TABLE_FOOTER = "</tbody></table>";
String EMPTY_STRING = "";
String HTML_NEW_LINE = "<br>";
StringBuilder tableBuilder = new StringBuilder();
// Add table Header
tableBuilder =
tableBuilder.append(HTML_ORGANIZATION_TABLE_HEADER);
// Add rows for each history entry
tableBuilder = tableBuilder.append(HTML_TABLE_ROW_START).append(Role_Name).append(HTML_T ABLE_CELL)
.append(Organization).append(HTML_TABLE_CELL).append(Organization_Type).append(HTML_TABLE_ROW_END);
// Add table Footer
tableBuilder = tableBuilder.append(HTML_TABLE_FOOTER);
// Add hyperlink to attachment
if (AttachmentLink != null && !AttachmentLink.equals(EMPTY_STRING)) {
tableBuilder = tableBuilder.append(HTML_NEW_LINE).append(AttachmentLink);
}
val = tableBuilder.toString();
The output which I am getting is:
[
{
"NT_Name" : "Test",
"AttachmentLink" : "<table style="width: 100%;" border="1"><td style="width: 30%;font-weight:bold;font-size:100%;">Role Name<td style="width: 50%;font-weight:bold;font-size:100%;">Organization<td style="width: 25%;font-weight:bold;font-size:100%;">Organization Type<td style="font-size:90%;">Business Process Adviser<td style="font-size:90%;">Global<td style="font-size:90%;">Location Hierarchy<a xml="lang" href="https://">Click here to view >"
},
{
"NT_Name" : "Test",
"AttachmentLink" : "<table style="width: 100%;" border="1"><td style="width: 30%;font-weight:bold;font-size:100%;">Role Name<td style="width: 50%;font-weight:bold;font-size:100%;">Organization<td style="width: 25%;font-weight:bold;font-size:100%;">Organization Type<td style="font-size:90%;">Manager<td style="font-size:90%;"> Technology (1000503320)<td style="font-size:90%;">Supervisory<a xml="lang" href="https://">Click here to view"
}
]

Modification/anomaly between ES versions 7.10.2 and 7.16.3?

The following mapping and query DSL dict work OK when the version of ES is 7.10.2, but they never work where the version of ES is 7.16.3:
mappings = \
{
"mappings": {
"properties": {
"esdoc_text": {
"type": "text",
"term_vector": "with_positions_offsets",
"fields": {
"stemmed": {
"type": "text",
"analyzer": "english",
"term_vector": "with_positions_offsets",
}
}
}
}
}
}
search dict:
data = \
{
'query': {
'simple_query_string': {
'query': self.search_string,
'fields': [
self.text_field
]
}
},
'highlight': {
'fields': {
self.text_field: {
'type': 'fvh',
'pre_tags': [
'<span style="background-color: yellow">',
'<span style="background-color: skyblue">',
'<span style="background-color: lightgreen">',
'<span style="background-color: plum">',
'<span style="background-color: lightcoral">',
'<span style="background-color: silver">',
],
'post_tags': [
'</span>', '</span>', '</span>',
'</span>', '</span>', '</span>',
]
}
},
'number_of_fragments': 0
}
}
with version 7.10.2, setting self.text_field to "esdoc_text" (field name) returns highlighted results using a standard analyser, and setting self.text_field to "esdoc_text.stemmed" returns highlighted results using an english analyser.
but with version 7.16.3, with the identical mapping and identical query DSL dict, hits are produced, but they never contain a "highlight" key.
Has anyone got any idea why this might be? Has any modification crept in between 7.10.2 and 7.16.3 which might explain this? Anyone now how to change things?

how to call data by who contains from another table in mongodb nodejs

I don't know how to explain but I will try.
I have two tables in MongoDB. They are:
-Games
-Players
Two data of games table:
{season: 4, game: 1}
{season: 4, game: 2}
Four data of player table:
{season: 4, game: 1, player: St. Yusuf, points: 100}
{season: 4, game: 1, player: St. Isaac, points: 80}
{season: 4, game: 2, player: St. Yusuf, points: 90}
{season: 4, game: 2, player: St. Isaac, points: 100}
So, they were the datas. And I want to write them to ejs file like this:
Game 1:
St. Yusuf | 100pts
St. Isaac | 80pts
Game 2:
St. Isaac| 100pts
St. Yusuf| 90pts
But when I try to write them, they get writes like this:
Game 1:
St. Yusuf | 100pts
St. Isaac | 80pts
St. Isaac| 100pts
St. Yusuf| 90pts
Game 2:
St. Yusuf | 100pts
St. Isaac | 80pts
St. Isaac| 100pts
St. Yusuf| 90pts
*The sorting of points are doesn't matter. The case is I want to get write the players with only same game numbers, not all games each other...
So, how to fix it? Here is the backend codes:
Games.find({}, (err, gamesData) => {
Players.find({}, (err, playersData) => {
res.render('score-board', {games: gamesData, players: playersData })
})
})
Here is the ejs codes:
<% games.forEach(game => { %>
<table>
<thead>
<tr>
<th>Player</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<% players.forEach(player => { %>
<tr>
<td class="player-name"><%- player.player %></td>
<td class="player-name"><%- player.points%></td>
</tr>
<% }) %>
</tbody>
</table>
<% }) %>
Here is the Games table's structure:
const mongoose = require('mongoose')
const Schema = new mongoose.Schema({
season: {type:String},
game: {type:String}
}, {timestamps:true})
module.exports = mongoose.model('games', Schema)
to achieve the result you are looking for you should restructure your tables to something like this:
{
"Games" : [
{
"season":4,
"game":1,
"Players": [
{
"player" :"St. Yusuf",
"points" : 100
},
{
"player" :"St. Isaac",
"points" : 80
}
]
},
{
"season":4,
"game":2,
"Players": [
{
"player" :"St. Yusuf",
"points" : 90
},
{
"player" :"St. Isaac",
"points" : 100
}
]
}
]
}
and your .ejs file would look something like:
<% games.forEach(game => { %>
<table>
<thead>
<tr>
<th>Player</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<% games.Players.forEach(player => { %>
<tr>
<td class="player-name"><%- games.Players.player %></td>
<td class="player-name"><%- games.Players.points%></td>
</tr>
<% }) %>
</tbody>
</table>
<% }) %>
Games table schema:
const mongoose = require('mongoose')
const Schema = new mongoose.Schema({
season: {type:String},
game: {type:String},
Players: [
{
player: {type:String},
points: {type:Number}
}
]
}, {timestamps:true})
module.exports = mongoose.model('games', Schema);

how to sort a dictionary by the value of a key in the dictionary?

I wrote my own wishlist in python3 flask (with apache mod_wsgi)
this is my data structure for the wishlist items:
{
},
"": {
"images": {
"pic": ""
},
"buylinks": {
"": {
"link": "",
"price": ""
}
},
"want": "",
"comments": ""
}
}
for example:
"Nintendo Joy-Con (L/R) - Gray": {
"images": {
"pic": "wishlist/joycons-grey.jpg"
},
"buylinks": {
"Amazon": {
"link": "https://www.amazon.com/dp/B01N6QKT7H",
"price": "66.99"
}
},
"want": "7/10",
"comments": "extra joycons for playing with friends on my switch"
},
right now every time I update the json file containing the wishlist data the apache server jinja template spits it iut in a different order
my jinja template looks like
<table>
<tr>
<th>Name</th>
<th>Image(s)</th>
<th>Links To Buy</th>
<th>Want/10</th>
<th>Comments</th>
</tr>
{% for key in wishlist.keys() %}
<tr>
<td><b>{{ key }}</b></td>
<td>{% for image in wishlist[key]["images"].keys() %}
<img src="{{ url_for('static', filename=wishlist[key]["images"][image]) }}" width="250px">
{% endfor %}</td>
<td>{% for merchant in wishlist[key]["buylinks"].keys() %}
{{ merchant}} (~${{ wishlist[key]["buylinks"][merchant]["price"] }})
<br>{% endfor %}</td>
<td><a> {{ wishlist[key]["want"] }} </a></td>
<td> {{ wishlist[key]["comments"] }}</td>
</tr>
{% endfor %}
</table>
how do I modify the {% for key in wishlist.keys() %} line to sort the wishlist by the value of the want key such that 10/10 is at the beginning and 0/10 is at the end? (the value to use to sort the example item would be "7/10")
I think I need to use the "sorted" function but I'm not sure how to
so to get this to work I had to add
wishlist_keys = sorted(wishlist.keys(), reverse=True, key=lambda x: wishlist[x]["want"] )
to my flask python code and pass wishlist_keys into render_template()
and change the template to
{% for key in wishlist_keys %}

How to divide a column to have multiple rows in b-table in bootstrap vue?

I am creating SPA using vue. I have JSON array :
[
{
date: new Date(2076, 5, 10),
customer: {id: 0,name: "Foo bar",tol: "Some tol",city: "Some City",},
items: [
{code: "gs",name: "Generic Shoes",cost: 500,quantity: 5},
{code: "nf",name: "North Facing Jacket",cost: 5000,quantity: 5},
{code: "lp",name: "Lee Vice Jeans Pant",cost: 1500,quantity: 15}
],
}
]
which now contains one object that has date, customer and items primarily. I want to make table that will contain date, customer and items as fields, and each row of table will contain multiple row of items.
Something like this :
,
This thing has only one row, but as you can imagine there might be multiple row for multiple {date, customer, items[]}.
This was best I was able to do :
<b-container>
<b-table responsive="true" striped hover :items="DraftList" :fields="fields">
<template slot="[date]" slot-scope="data">{{data.value|formatDate}}</template>
<template slot="[customer]" slot-scope="data">{{data.value.name}}</template>
<template slot="[items]" slot-scope="data">{{data.value}}</template>
</b-table>
</b-container>
<script>
import { mapState } from "vuex";
export default {
data() {
return {
fields: [
{ key: "date", sortable: true },
{
key: "customer",
label: "Customer's Name",
sortable: true
},
{
key: "items",
label: "Item List",
sortable: true
}
]
};
},
computed: {
...mapState(["DraftList"])
},
mounted() {},
filters: {
formatDate: date => {
if (date instanceof Date) {
let month = "" + (date.getMonth() + 1);
let day = "" + date.getDate();
let year = date.getFullYear();
if (month.length < 2) month = "0" + month;
if (day.length < 2) day = "0" + day;
return [year, month, day].join("-");
}
return null;
}
}
};
</script>
I am stuck, what should I do now? I cannot properly term my searches either.
Solved it using v-for and rowspan
<b-table-simple responsive="true" hover outlined>
<colgroup>
<col />
<col />
</colgroup>
<colgroup>
<col />
<col />
<col />
</colgroup>
<colgroup>
<col />
<col />
</colgroup>
<b-thead head-variant="light">
<b-tr>
<b-th rowspan="2">Date</b-th>
<b-th rowspan="2">Customer's Name</b-th>
<b-th colspan="4">Items</b-th>
</b-tr>
<b-tr>
<b-th>code</b-th>
<b-th>Name</b-th>
<b-th>Cost</b-th>
<b-th>Quantity</b-th>
</b-tr>
</b-thead>
<b-tbody v-for="(draft,index) in DraftList" :key="index">
<b-tr>
<b-td :rowspan="draft.items.length+1">{{draft.date|formatDate}}</b-td>
<b-td :rowspan="draft.items.length+1">{{draft.customer.name}}</b-td>
</b-tr>
<b-tr v-for="(item, itemIndex) in draft.items" :key="itemIndex">
<b-td>{{item.code}}</b-td>
<b-td>{{item.name}}</b-td>
<b-td>{{item.cost}}</b-td>
<b-td>{{item.quantity}}</b-td>
</b-tr>
</b-tbody>
</b-table-simple>

Resources