So I'm trying to make the addition of project easier for me with a json data.
Basically I'm creating blocks of projects and each project comes with an image, however even when the id == to the name I gave the image, the image does not render. Is there is any option for that or should I just give up on json files ?
what I'm trying to re-do
What I'm actually getting
The reactjs code
import Pdata from "../../api/projects.json";
import p1 from "../../img/Project/PoleAnglais.png";
import p2 from "../../img/Project/I-Art.png";
import p3 from "../../img/Project/Hestia.png";
import p4 from "../../img/Project/EvlV1.png";
import p5 from "../../img/Project/Kelly.png";
import p6 from "../../img/Project/EthLnyV2.png";
import { Component } from "react";
class Plist extends Component {
render() {
return (
<div
className="project-list"
data-aos="fade-right"
data-aos-duration="1200"
>
{Pdata.map((projectDetail, index) => {
return (
<div className="project-block">
<h2 className="project-title">{projectDetail.title}</h2>
<p className="date">{projectDetail.date}</p>
<p className="project-desc">{projectDetail.desc}</p>
<img src={projectDetail.id} alt="" />
<p className="madewith">made with {projectDetail.tags}</p>
</div>
);
})}
</div>
);
}
}
export default Plist;
The json data
[
{
"id": "p1",
"title": "Pole Anglais",
"date": "16/10/2019",
"desc": "This project was in association with Filip Zafirovski, my English teacher by the time who wanted students to get a source of inspiration by publishing articles and/or their work. It was my very first web project, and was kind of hard to pull off but I still enjoyed it.Since for the very first time i coded for a project and not myself.",
"tags": "Loads of crap"
},
{
"id": "p2",
"title": "Project I.Art",
"date": "3/07/2021",
"desc": "In France to go to college you have to get a diploma, which requires multiple exams to be validated. One of the subjects I had to do a presentation on was Art. I decided to create an idea around an Artificial Intelligence who would create art based on the likes and dislikes of the spectator. This panel is a website made for the occasion.",
"tags": "Html,Scss, & AOS librairie"
},
{
"id": "p3",
"title": "Hestia Real Estate",
"date": "18-26/10/2021",
"desc": "At the very start of my student life #hetic, They grouped student randomly to make a project. The subject of the project was to create an agency, a fake web-app and website that sells premium submarines to plus ultra rich people. For that project I designed the website of the agency, and the app for the complex.",
"tags": "Html & Scss"
},
{
"id": "p4",
"title": "EvL First Design",
"date": "30/10/2021",
"desc": "Before the design and dev of this portfolio, I had made a portfolio where I only putted my socials link. All of that because I had no idea of what to put on it. Even if I was satisfied with the first version it did not in any case represented the mood and emotion I wanted it to give. And so I gave birth to the actual design of the website on the 11/11/2021",
"tags": "Nextjs & Scss"
},
{
"id": "p5",
"title": "Kelly's Portfolio",
"date": "3/07/2021",
"desc": "Sometimes after arriving at my college, I met a freshly made friend who wanted to publish her portfolio. She knew how to design and do plenty others thing. To She didn't really like to code and was making her website with Wix. To which I proposed to remake her website by coding it myself.",
"tags": "VueJs & Scss"
},
{
"id": "p6",
"title": "EthLny V2",
"date": "11-12/11/2021",
"desc": "After doing the amazing portfolio of Kelly, I was kind of disappointed with my own. So I decided to remake a new design. Use a Random language, study the color psychology, searched a tagline. And TA-DA here it is, the website you're in right now is the result of 7 hours of researching, designing and coding and debugging.",
"tags": "ReactJs, Scss & AOS librairy"
}
]
Put your images into object, so keys would be ids from json,and values - URLs to your images. Right now you are just passing string like "p1" and "p5" into src. It is not equivalent to passing value of variable with name p1 or p5.
Fast way to create such object would be this:
import p1 from "../../img/Project/PoleAnglais.png";
import p2 from "../../img/Project/I-Art.png";
import p3 from "../../img/Project/Hestia.png";
import p4 from "../../img/Project/EvlV1.png";
import p5 from "../../img/Project/Kelly.png";
import p6 from "../../img/Project/EthLnyV2.png";
let images = {
p1, p2, p3, p4, p5, p6
}
/**
It is same as let images = {p1: p1, p2: p2, p3: p3, p4: p4, p5: p5, p6: p6};
*/
Then, inside your component use id from JSON as a key:
<img src={images[projectDetail.id]} alt=""/>
This way you will get actual value of variable p1 (and others).
I think the image is rendering but it is just too small to see
try adding width and height.
<img style={{width: 200px, height: 200px}} src={projectDetail.id} alt="" />
I think this will solve your problem. instead of importing each image, either create a map that points P* to your image addresses or rename your images in a way that you can do this:
{Pdata.map((projectDetail, index) => {
return (
<div className="project-block">
<h2 className="project-title">{projectDetail.title}</h2>
<p className="date">{projectDetail.date}</p>
<p className="project-desc">{projectDetail.desc}</p>
<img src={`../../img/Project/${projectDetail.id}.png`} alt="" /> // <====the change
<p className="madewith">made with {projectDetail.tags}</p>
</div>
);
})}
you can also add an image prop to your json pointing to the right image, but the json might be out of your hands to change.
Related
I have a carousel that auto populates data based on the context object in the payload preview in a tool I use called IAS. Each object has a product array with data inside of it. However if the product array is empty, I don't want the carousel to show at all. I tried to do the below in my Handlebars but even when there was product data, nothing showed :( .
{{#if products}}
{{#each products}}
<ul>
<li class="pr__item-wrapper">
<p class="pr__title">{{attributes.name.value}}</p>
</li>
</ul>
{{/each}}
{{/if}}
When product data is available, it is set up like this:
"products" : {
"attributes":{
"imageUrl": {
"value": "https://us.devirobot.com/dw/image/v2/BFXP_DEV/on/demandware.static/-/Sites-
master-catalog-irobot/default/dwb8b39e90/images/large/roomba/j755020_1.jpg?sw=1800"
},
"subheader": {
"value": null
},
"savingsAmount": {
"value": "100"
},
"name": {
"value": "iRobot Roomba j7+ Robot Vacuum, iRobot Roomba Replenishment Kit for j & i
Series & Replacement Dirt Disposal Bags, 3-Pack"
}
}
}
However, sometimes product data will return empty:
"products" : [
]
When it's empty, I don't want the unordered list to show. Does my if statement need to be modified, or do I need a boolean to return "true" if product data is coming in?
The code below is my data.jsx file where I used to store images, the title of the images, and description of the images. I have a problem importing images from another folder named Images. The Images folder and data.jsx file are both inside my component file
The vs code compiled it successfully but when I run it I just cannot see the result.
import img1 from './Images/jisoo.png'
import img2 from './Images/blackpink.png'
import img3 from './Images/jennie.jpg'
export const sliderItems = [
{
id:1,
img:{img1},
title:"CNY SALES",
desc:"DON'T COMPROMISE ON STYLE! GET FLAT 30% OFF FOR THE BEST SELLING PRODUCTS.",
bg:"f5fafd",
},
{
id:2,
img:{img2},
title:"ANY SALES",
desc:"DON'T COMPROMISE ON STYLE! GET FLAT 30% OFF FOR THE BEST SELLING PRODUCTS.",
bg:"fcf1ed",
},
{
id:3,
img:{img3},
title:"BNY SALES",
desc:"DON'T COMPROMISE ON STYLE! GET FLAT 30% OFF FOR THE BEST SELLING PRODUCTS.",
bg:"fbf0f4",
}]
Click on this link and you can the picture of my source code
When you use { img } outside jsx(html) this will create an js object.
You are doing something like this which is not correct.
{
id:3,
img:{img3 : img3},
title:"BNY SALES",
desc:"DON'T COMPROMISE ON STYLE! GET FLAT 30% OFF FOR THE BEST SELLING PRODUCTS.",
bg:"fbf0f4",
}
Use like this
{
id:3,
img:img3,
title:"BNY SALES",
desc:"DON'T COMPROMISE ON STYLE! GET FLAT 30% OFF FOR THE BEST SELLING PRODUCTS.",
bg:"fbf0f4",
}
I have a file inside my react project, glossaryItems.json. The file looks like this:
{
"glossary": [
{
"name": "Constant",
"pageNumber": "33",
"definition": "A value that cannot change while the program is running.",
},
{
"name": "Debugging",
"pageNumber": "45",
"definition": "The process of finding and reducing the number of defects in a computer program.",
},
{
"name": "Algorithm",
"pageNumber": "4",
"definition": "A strictly defined finite sequence of well-defined statements that provides the solution to a problem."
}
]
}
I have another file, glossaryPage.tsx where I would like to display each glossary item within a tab. I am not sure how to access the json file in order to use it within the tsx file. I ended up changing the json file to a .ts file and exported it as so:
export const glossaryItems =
[
{
"glossary": [
{
"name": "Constant",
"pageNumber": "33",
"definition": "A value that cannot change while the program is running.",
},
{
"name": "Debugging",
"pageNumber": "45",
"definition": "The process of finding and reducing the number of defects in a computer program.",
},
{
"name": "Algorithm",
"pageNumber": "4",
"definition": "A strictly defined finite sequence of well-defined statements that provides the solution to a problem."
}
]
}
]
And then imported it inside glossaryPage.tsx. I want to be able to get the each part of the json separately to be able to use it inside the tabs. So I would have one tab labeled "Constant", a second tab, "Debugging", a third tab "Algorithm" and under each tab display that information such as pagenumber and definition that applies to that tab. I tried mapping over just the glossary but was unable to. I had to map over the glossaryItems.
const GlossaryPage = () => {
const terms = glossaryItems.map(({glossary}, key) => (
<div key={key}>
{glossary.map(({name, pageNumber, definition}, key) => (
<div key={key}>
<p>{name}</p>
</div>
))}
</div>
))
return (
<SprkTabsPanel
isDefaultActive
tabBtnChildren={terms[0]} //this is where the terms are printing out on the tab
tabBtnAnalyticsString="tab-1"
tabBtnDataId="tab-1"
>
</SprkTabsPanel>
I thought that by indexing the terms it would give me the term at that index but it gives me all of the terms. This is what it looks like:
How can I get the individual values of the object?
Any help would be greatly appreciated!
Importing JSON
In order to import a .json file, you simply need to enable support in your tsconfig.json file. Set "resolveJsonModule": true inside the compilerOptions property. Now you can import the data from the JSON file as a default import.
Docs: Resolve JSON Module
Mapping Your Object
I had a look at the documentation for the Spark Design system and it seems like you need to create a separate SprkTabsPanel component for each tab. All of the individual tab panels go inside of one SprkTabs component.
import React from "react";
import { SprkTabs, SprkTabsPanel } from "#sparkdesignsystem/spark-react";
import glossaryItems from "./glossaryItems.json";
const GlossaryPage = () => {
return (
<SprkTabs idString="glossary-tabs">
{glossaryItems.glossary.map(({ name, pageNumber, definition }, key) => (
<SprkTabsPanel tabBtnChildren={name} key={key}>
<p>{definition}</p>
<p>Page Number: {pageNumber}</p>
</SprkTabsPanel>
))}
</SprkTabs>
);
};
export default GlossaryPage;
i have Array of image urls:
"image": [
"https://i.imgur.com/QHQaUxi.jpg",
"https://i.imgur.com/GNjH4mZ.jpg",
"https://i.imgur.com/py9IkGa.jpg",
],
my YachtDetailsData looks like this:
{
"id": "1",
"name": "Rarity",
"title": "Modern facilities, sophisticated style and chic deck areas make luxury yacht RARITY one of the most sought-after charter yachts in her class. Her large beam of 10m offers extra onboard space with wide deck areas, making RARITY the ideal yacht tocharter for events such as the Grand Prix",
"description": "The 180'5 / 55m Custom motor yacht 'Rarity' was built in 2008 by Rossi Navi and last refitted in 2017. Officina Italiana Design is responsible for her beautiful exterior and interior design. Previously named Syna, her interior combines timeless styling and beautiful furnishings. She offers large volumes, an open plan Main Salon and dining room, a fully equipped gym and a sky lounge with 220-inch cinema screen. Rarity’s interior layout sleeps up to 12 guests in 6 guest cabins.",
"price": "235.000€",
"guests": "12 guests",
"length": "55 meters",
"build": "2008",
"cabins": "06",
"refit": "2017",
"maxSpeed": "15.5 kt",
"builder": "Rossinavi",
"location": "Mediterranean",
"crew": "12",
"coverImage": "https://i.imgur.com/U7yw2EF.jpg",
"image": [
"https://i.imgur.com/QHQaUxi.jpg",
"https://i.imgur.com/GNjH4mZ.jpg",
"https://i.imgur.com/py9IkGa.jpg",
"https://i.imgur.com/4n3sZxb.jpg",
"https://i.imgur.com/8CbKNUm.jpg",
"https://i.imgur.com/R5eIKmQ.jpg",
"https://i.imgur.com/zEJ8Oxa.jpg",
"https://i.imgur.com/PfuoqQQ.jpg",
"https://i.imgur.com/IgxuEA7.jpg",
"https://i.imgur.com/8GNUaSx.jpg",
"https://i.imgur.com/3kUArby.jpg",
"https://i.imgur.com/1xhp0DT.jpg",
"https://i.imgur.com/flmYnDc.jpg"
],
"video": ""
},
i have got 7 objects with image array and i want do display each image in React image slider:
import YachtDetailsData from "../data/YachtList.json"
interface Props {
match: any
}
const YachtDetails: React.FC<Props> = ({match}) => {
const yachtData = YachtDetailsData.find(yacht => yacht.id === match.params.id)
const slider = (
<AwesomeSlider>
<div><img src={yachtData?.image[]} alt=""/></div>
</AwesomeSlider>
);
i don't no what to do. Do i need to map them? I am beginner so any help is welcome.
Try this..
const YachtDetails: React.FC<Props> = ({ match }) => {
// here im just spreading/pushing the object in an array, so it will make use
// of the `find` method which is an array method.
const newYachtData = [{...YachtDetailsData}]
const yachtData = newYachtData.find(
(yacht) => parseInt(yacht.id) === match.params.id
);
const slider = (
<AwesomeSlider>
<div>
{yachtData?.image.map((image, key) => (
<div key={key}>
<img src={image} alt="" />
</div>
))}
</div>
</AwesomeSlider>
);
};
Apologies for the big Title, I couldn't come up with any thing better. Let me explain the issue I am having.
I have to render three cards where each of them share same look-feel, means each of them have a header section and a body section. I am using ng-repeat to get data from model and render these cards.
Code:
<div class="card">
<h1>{{card.title}}</h1>
<div ng-repeat="widget in card.widgets" class="widget">
<h2>{{widget.title}}</h2>
{{widget.type}}
</div>
</div>
Now, each of these Card's body should have different UI in it. For example, One card's body might have a chart using Hight Charts, Another one might just want to use a UI from jQuery UI library, etc..
How would I achieve it when I am looping using ng-repeat? Let me know if my starting direction is correct?
The model data look like this:
[
{
"id": "c001",
"title": "CARD 1",
"widgets": [
{
"title": "title 1.1",
"type": "line-graph"
},
{
"title": "title 1.2",
"type": "bar-chart"
}
]
},
{
"id": "c002",
"title": "CARD 2",
"widgets": [
{
"title": "title 2.1",
"type": "graph"
},
{
"title": "title 2.2",
"type": "bar-chart"
}
]
},
{
"id": "c003",
"title": "CARD 3",
"widgets": [
{
"title": "title 3.1",
"type": "line-graph"
},
{
"title": "title 3.1",
"type": "bar-chart"
}
]
}
]
Looking for help on this.
Thank You.
The solution for your problem is to combine both ng-switch and ng-repeat to achieve the effect you are attempting to get. Basically you would repeat over the items and switch between its type. Angular only includes the part of the DOM which matches the switch.
Here is a rough idea / html of what you should be doing.
<div class="card">
<h1>{{card.title}}</h1>
<div ng-repeat="widget in card.widgets" class="widget">
<h2>{{widget.title}}</h2>
<div ng-switch on="widget.type">
<div ng-switch-when="line-graph">
<!--do something here relevant to line graph-->
<div line-graph="widget.data"></div>
</div>
<div ng-switch-when="graph">
<!--do something here relevant to graph-->
<div graph="widget.data"></div>
</div>
<!-- and so on... add more if you need-->
</div>
</div>
</div>
I'm newer to Angular, but I believe you could encapsulate all of it through a directive. The idea would be we would have our own directive that we add to a div and then pass in the type and from there the directive could handle the logic and create the appropriate chart/element/whatever and do all the creation in the javascript file for the directive.
<div chart="line-graph"></div>
<div chart="bar-chart"></div>
module.directive('chart', function(){
return{
//logic to build various charts with different libraries here
}
});
It might be a pretty complicated directive to write, but it would be an elegant way to write it. #ganaraj did have that above with div graph="widget.data" inside of the ng-switch, but didn't really mention the directive part, just the switching. That would make the individual directives simpler, so may be the better overall approach, if each type is going to be vastly different.
This post below from simpulton is really good covering the directives portion of it. You can even make it a step further and make it more like a widget where the tag could be (his "act five: widget directive") and then to that pass in the type and the data to go with it. He is writing a directive to do animantion on some cirlces created in CSS, so there's no reason you couldn't use it to apply some highchart or jQuery UI code. It's really well written and includes code in jsfiddle so you can see it work as well.
http://onehungrymind.com/angularjs-directives-basics/