백엔드에서 얻은 JSON 데이터에는 동적 키와 값이 있습니다. 아래 데이터는 첫 번째 열 값을 내 요구 사항 인 rowSpan으로 변환해야하는 예제 메서드 데이터 중 하나입니다. 행 범위에 대해 가능하고 셀 데이터의 숫자 값을 비교하고 아이콘을 추가하십시오 ..? 나에게 해결책을 줘
data= [
{carrier: "Emirates", range: "min", "Jul 29": 1275, "Jul 30": 1325},
{carrier: "Emirates", range: "med", "Jul 29": 1275, "Jul 30": 1338},
{carrier: "Emirates", range: "max", "Jul 29": 1275, "Jul 30": 1375},
{carrier: "rehlat", range: "min", "Jul 29": 1138, "Jul 30": 1306},
{carrier: "rehlat", range: "med", "Jul 29": 1198, "Jul 30": 1330},
{carrier: "rehlat", range: "max", "Jul 29": 1258, "Jul 30": 1354}]
내가 사용하는 코드는 다음과 같습니다.
const RenderRow = (props) =>{
return props.keys.map((key, index)=>{
return <td>{props.data[key]}</td>
})
}
class Mycomponent extends Component {
constructor(props){
super(props);
this.getHeader = this.getHeader.bind(this);
this.getRowsData = this.getRowsData.bind(this);
this.getKeys = this.getKeys.bind(this);
}
getKeys = function(){
return Object.keys(this.props.data[0]);
}
getHeader = function(){
var keys = this.getKeys();
return keys.map((key, index)=>{
return <th key={key}>{key.toUpperCase()}</th>
})
}
getRowsData = function(){
var items = this.props.data;
var keys = this.getKeys();
return items.map((row, index)=>{
return <tr key={index}><RenderRow key={index} data={row} keys={keys}/></tr>
})
}
render() {
return (
<div>
<table>
<thead>
<tr>{this.getHeader()}</tr>
</thead>
<tbody>
{this.getRowsData()}
</tbody>
</table>
</div>
);
}
}
내 출력 여기에 이미지 설명 입력
예상 출력 여기에 이미지 설명 입력
codesandbox에 추가 된 코드 여기에 링크 설명 입력