จะป้อนการตอบสนองของ Json ลงในตารางการออกแบบมดเปล่าได้อย่างไร

ฉันค่อนข้างใหม่ที่จะตอบสนองและต้องการป้อนการตอบสนอง json ของฉันซึ่งฉันได้รับจาก api ลงในตารางการออกแบบมดที่ว่างเปล่า และฉันไม่รู้ว่าต้องทำอย่างไร

ฉันได้แมป id กับชื่อแล้ว แต่ยังต้องการคอลัมน์อื่นทั้งหมดและอยู่ในรูปแบบตารางของการออกแบบมดด้วย

เจสันตอบกลับ

[
    {
        "id": 9685,
        "name": "Mukesh  Rai",
        "count of sub": 3,
        "count of int": 2,
        "count of po": null
    },
    {
        "id": 2085,
        "name": "Abhyudaya Singh Panwar",
        "count of sub": 1049,
        "count of int": 17,
        "count of po": 5
    },
    {
        "id": 7087,
        "name": "Amit Kumar Sharma",
        "count of sub": 363,
        "count of int": 68,
        "count of po": 10
    },
    {
        "id": 2100,
        "name": "Aditya Kumar",
        "count of sub": 724,
        "count of int": 5,
        "count of po": 1
     }
]

ไฟล์ส่วนประกอบ

  componentDidMount(){
    axios.get("http://127.0.0.1:8000/marketer_details/")
      .then(response=>{
        console.log(response)
        this.setState({posts: response.data})
      })
      .catch(error=> {
        console.log(error)
      })
    }

render() {
    const { posts } = this.state
    return(
      <div>
        List of Posts
        {
          posts.length ?
          posts.map(post => <div key = {post.id}>{post.name}</div>) : 
          null
        }
      </div>
    )

//ต้องการคอลัมน์อื่นๆ ทั้งหมดด้วย

    render(){
    const columns = [
      {
        title: 'Name',
        dataIndex: 'name',
        key: 'name',
        width: '30%',
        ...this.getColumnSearchProps('name'),
      },
      {
        title: 'Count of Submissions',
        dataIndex: 'Count',
        key: 'sub',
        width: '20%',
        ...this.getColumnSearchProps('sub'),
      },
      {
        title: 'Count of Interviews',
        dataIndex: 'Count',
        key: 'int',
        ...this.getColumnSearchProps('int'),
      },
      {
        title: 'Count of PO',
        dataIndex: 'Count',
        key: 'po',
        ...this.getColumnSearchProps('po'),
      },
    ];
    return <Table columns={columns} dataSource={this.state.posts} />;
  }
}
}
export default App

ฉันไม่เข้าใจว่าการแมปหลายรายการสามารถทำได้อย่างไรกับแต่ละแถวของคอลัมน์


person Hrithik Baishakhiya    schedule 17.06.2019    source แหล่งที่มา
comment
เหตุใดคุณจึงมีคำสั่ง return สองคำสั่งสำหรับฟังก์ชันนี้   -  person Agney    schedule 17.06.2019
comment
ฉันรู้สึกเสียใจที่พยายามบรรยายถึงสิ่งที่ฉันได้ทำไปแล้ว แต่มันไม่ได้ถูกแบ่งส่วน   -  person Hrithik Baishakhiya    schedule 17.06.2019
comment
Dataindex ควรตรงกับชื่อคุณสมบัติของ json ของคุณ .. และเพิ่มการโหลดแฟล็กอีกหนึ่งรายการ: จริงในสถานะของคุณและตั้งค่าเป็นเท็จเมื่อโหลดข้อมูล ... เริ่มต้นข้อมูลของคุณแสดงการโหลดและเมื่อคุณได้รับข้อมูลจากแบ็กเอนด์ก็จะแสดงข้อมูล ‹ คอลัมน์ตาราง={คอลัมน์} กำลังโหลด={this.state.loading} dataSource={this.state.posts} /›   -  person Hemanthvrm    schedule 17.06.2019


คำตอบ (1)


องค์ประกอบ Table ใช้โครงสร้างคอลัมน์เป็นอาร์กิวเมนต์

ในที่นี้ dataIndex หมายถึงดัชนีในข้อมูลที่จะอ้างอิงสำหรับคอลัมน์ key มีวัตถุประสงค์เพื่อให้ไม่ซ้ำกันในชื่อคอลัมน์

ในกรณีของคุณ อาร์เรย์คอลัมน์อาจเป็น:

const columns = [
    {
      name: "Name",
      dataIndex: "name"
    },
    {
      name: "Count of submissions",
      dataIndex: "count of sub"
    },
    {
      name: "Count of interviews",
      dataIndex: "count of int"
    },
    {
      name: "Count of PO",
      dataIndex: "count of po"
    }
  ];

การสาธิต Codesandbox

person Agney    schedule 17.06.2019
comment
แต่ประเด็นคือจะป้อนข้อมูล json แบบดิบในคอลัมน์เหล่านี้ได้อย่างไร ฉันควรวนซ้ำรายการและพุชข้อมูลหรือมีวิธีอื่นที่ฉันสามารถทำได้หรือไม่? - person Hrithik Baishakhiya; 17.06.2019
comment
นั่นคือสิ่งที่คุณใช้ฟิลด์ dataSource ป้อน json แบบ raw ไปที่ฟิลด์นั้น - person Agney; 17.06.2019
comment
คุณไม่จำเป็นต้องทำอะไรเลย คุณลักษณะนี้ dataSource={this.state.posts} จะจัดการเอง - person Hemanthvrm; 17.06.2019
comment
แต่ฉันคิดว่ามันจะไม่ดึงข้อมูลจาก api และนำไปไว้ในคอลัมน์ถัดไป - person Hrithik Baishakhiya; 17.06.2019
comment
@HrithikBaishakhiya มันจะทำอย่างนั้น - person Agney; 17.06.2019