[英]Dynamic rendering of sections/divs according to an array of objects in React.JS
I have an array of objects to show data on the web page and all the div should according to key in the array object (in this order). 我有一个对象数组来显示网页上的数据,所有div应该根据数组对象中的键(按此顺序)。
var priceSize = <div>HTML goes here</div>
var agentDetails = <div>HTML goes here</div>
var mappedHouses = <div>HTML goes here</div>
var sideComps = <div>HTML goes here</div>
var priceDays = <div>HTML goes here</div>
render() {
return (
report_sequence.map((data) =>
<div className={data.key}>
{
data.key === 1 ? subjectProperty :
data.key === 2 ? priceSize :
data.key === 4 ? priceDays :
data.key === 5 ? sideComps :
data.key === 6 ? mappedHouses :
data.key === 9 ? agentDetails : ''
}
</div>
)
);
}
This is my array : 这是我的阵列:
var report_sequence = [
{key : 1, value : "SUBJECT PROPERTY OVERVIEW"},
{key : 2, value : "PRICE & SIZE GRAPH"},
{key : 9, value : "AGENT SUMMARY"},
{key : 4, value : "PRICE & DAYS ON MARKET GRAPH"},
{key : 6, value : "MAPPED HOUSES"},
{key : 5, value : "SIDE BY SIDE COMPARISION"}
]
I expect div arrangement according to my report sequence. 我希望根据我的报告顺序进行div安排。 Please help for the same.
请帮助相同。
A simple solution to dynamically render the HTML of your <div>
component collection (ie priceSize
, agentDetails
, etc) would via a switch
statement. 动态呈现
<div>
组件集合的HTML(即priceSize
, agentDetails
等)的简单解决方案将通过switch
语句。
First, ensure that your HTML is defined as a valid component (ie leading capitalization): 首先,确保将HTML定义为有效组件(即主导大写):
var SubjectProperty = <div>HTML goes here</div>
var PriceSize = <div>HTML goes here</div>
var AgentDetails = <div>HTML goes here</div>
var MappedHouses = <div>HTML goes here</div>
var SideComps = <div>HTML goes here</div>
var PriceDays = <div>HTML goes here</div>
Next, update the component that renders the report_sequence
array. 接下来,更新呈现
report_sequence
数组的组件。 One way to introduce the switch
idea mentiond above would be via a locally defined method: 引入上面提到的
switch
思想的一种方法是通过本地定义的方法:
render() {
/* Define a local helper that renders the component corresponding to
a specific key */
const renderItem = (key) => {
switch(key) {
case 1: return <SubjectProperty/>;
case 2: return <PriceSize/>;
case 4: return <PriceDays/>;
case 5: return <SideComps/>;
case 6: return <MappedHouses/>;
case 9: return <AgentDetails/>;
}
return null;
}
/* Invoke locally defined renderItem() method to dynamically render
each list item based on item's key */
return report_sequence.map((data) =>
<div key={data.key} className={data.key}>
{ renderItem(data.key) }
</div>)
}
Here you have yours components mocks: 在这里你有你的组件模拟:
var PriceSize = <div>HTML goes here</div>
var AgentDetails = <div>HTML goes here</div>
var MappedHouses = <div>HTML goes here</div>
var SideComps = <div>HTML goes here</div>
var PriceDays = <div>HTML goes here</div>
Here is map will render proper component 这里是map将呈现适当的组件
const componentMap = {
[1]: () => <PriceSize />
[2]: () => <AgentDetails />
[3]: () => <MappedHouses />
[4]: () => <SideComps />
[5]: () => <PriceDays />
}
And here you have your render function and: 在这里你有你的渲染功能和:
key
property do allow identifying them somehow by React, I assumed that you have some property like ID in this data set (can be index or anything unique) key
属性时,允许通过React以某种方式识别它们,我假设你在这个数据集中有一些像ID这样的属性(可以是索引或任何唯一的) value
for given key
and if it is I call it. key
某些value
,如果是,我调用它。 Code: 码:
render() {
report_sequence.map((data) =>
return (
<div className={data.key} key={data.id}>
{this.componentMap[data.type] && this.componentMap[data.type]()}
</div>
);
)
}
Both approach will work fine for dynamic data rendering. 这两种方法都适用于动态数据呈现。
return (
report_sequence.map((data) =>
<div className={data.key}>
{
data.key === 1 ? subjectProperty :
data.key === 2 ? priceSize :
data.key === 4 ? priceDays :
data.key === 5 ? sideComps :
data.key === 6 ? mappedHouses :
data.key === 9 ? agentDetails : ''
}
</div>
)
//Second approach using switch according to Dacre Denny
const renderItem = (key) => {
switch(key) {
case 1: return subjectProperty;
case 2: return priceSize;
case 4: return priceDays;
case 5: return sideComps;
case 6: return mappedHouses;
case 9: return agentDetails;
default: return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.