React实现疫情情况列表展示

爱被打了一巴掌 2023-03-03 06:29 60阅读 0赞

文章目录

    • 准备工作
    • 具体实现
    • 成果展示:

准备工作

在前面学习了React的基础知识以后此篇博客讲解的是如何通过使用React的基础语法来模拟从后端或者是爬虫获取到json数据进行基础的显示,并利用公司提供的前端组件组件地址进行渲染处理(组件依赖安装等查看网站有具体信息)。

Json数据可以搜索网易疫情地图,进入控制台点击network,选中XHR列查看数据,将整个json数据拷贝到自己的工程目录中,然后在index.js中进行引用:import jsonData from './list-total.json'

image-20200730144848427

具体实现

  1. import React, { Component} from 'react';
  2. import ReactDOM from 'react-dom';
  3. import jsonData from './list-total.json'
  4. import { Table } from 'tinper-bee';
  5. import 'bee-table/build/Table.css';
  6. /** * 这部分学习的是React 来展示地图信息 */
  7. console.log(jsonData)
  8. let allworldObj ={
  9. // 就是相当于先新定义一个对象。
  10. }
  11. jsonData.data.areaTree.forEach((item,i)=>{
  12. //
  13. if(allworldObj[item.name]==undefined){
  14. /** * 用来判断是否真的存在 因为我们看到有些值的定义是undefined * 需要先进行一个初始化 * */
  15. allworldObj[item.name] ={
  16. confirm:0,
  17. suspect:0,
  18. heal:0,
  19. dead:0
  20. }
  21. }
  22. /** * 对于一些值为null的存在,也要进行初始化。 */
  23. item.today.confirm= item.today.confirm? item.today.confirm:0;
  24. item.today.suspect= item.today.suspect? item.today.suspect:0;
  25. item.today.heal = item.today.heal? item.today.heal: 0;
  26. item.today.dead=item.today.dead?item.today.dead:0;
  27. allworldObj[item.name] = {
  28. // 对信息进行统计处理 计算: 等于当前的值加上新获取到的值。
  29. confirm:allworldObj[item.name].confirm+item.today.confirm,
  30. suspect:allworldObj[item.name].suspect+ item.today.suspect,
  31. heal: allworldObj[item.name].heal+ item.today.heal,
  32. dead: allworldObj[item.name].dead+item.today.dead
  33. }
  34. });
  35. let allList =[]// 定义一个空的数组
  36. for (const key in allworldObj) {
  37. allworldObj[key].name =key
  38. // 将信息的名字也加载到里面
  39. allList.push(allworldObj[key])
  40. }
  41. allList.sort((a,b)=>{
  42. // 顺序排列
  43. return a.confirm<b.confirm?1:-1
  44. })
  45. console.log(allList)
  46. function ListItem(props){
  47. /** * 利用平台提供的组件 * import { Table } from 'tinper-bee'; * 导入表格信息。 * */
  48. const columns = [
  49. { title: "序号",dataIndex: "index",key: "index",width: 80},
  50. { title: "国家名称",dataIndex: "orderCode",key: "orderCode",width: 200},
  51. { title: "确诊人数",dataIndex: "supplierName",key: "supplierName",width: 200},
  52. { title: "成功治愈",dataIndex: "type_name",key: "type_name",width: 200},
  53. { title: "怀疑人数",dataIndex: "purchasing",key: "purchasing",width: 200},
  54. { title: "死亡人数:",dataIndex: "purchasingGroup",key: "purchasingGroup",width: 200},
  55. ];
  56. const data = [
  57. {
  58. index:props.index+1,
  59. orderCode: props.data.name,
  60. supplierName: props.data.confirm,
  61. type_name: props.data.heal,
  62. purchasing:props.data.suspect,
  63. purchasingGroup:props.data.suspect,
  64. key:'1'
  65. }
  66. ];
  67. return(
  68. <Table
  69. className="demo"
  70. columns={ columns}
  71. data={ data} />
  72. )
  73. }
  74. class Bll extends React.Component{
  75. constructor(props){
  76. super(props)
  77. }
  78. render(){
  79. let WorldList = this.props.list.map((item,index)=>{
  80. return(
  81. <ListItem key ={ index} data={ item} index={ index}></ListItem>
  82. )
  83. });
  84. return(
  85. <div>
  86. <h1>全球病理</h1>
  87. { WorldList}
  88. </div>
  89. )
  90. }
  91. }
  92. ReactDOM.render(
  93. <Bll list={ allList}/>,
  94. document.querySelector('#root')
  95. )

成果展示:

image-20200730151708672

发表评论

表情:
评论列表 (有 0 条评论,60人围观)

还没有评论,来说两句吧...

相关阅读