Search code examples
reactjsjsxantd

ant design table with dynamic children columns


I'm using ant's table and trying to setup dynamic columns.

What I need is a table that shows a list of users with their performance for each of the classes as in the example below.

enter image description here

Details: I have a grouped column Performance that can have different sub-columns (current example shows columns science and physics). I'm calling renderContent() which sets up an object that has property children. I found this "solution" from ant's example here. The problem is that ant's example outputs children prop type string, while my function outputs prop type array. Which results in the error.

Here is a link to sandbox: https://codesandbox.io/embed/ecstatic-cookies-4nvci?fontsize=14

Note: if you uncomment children array in columns [line 46-59], you will see what my expected result should be.


Solution

  • The render method shouldn't return the object with children array. To use the render method, you would have to return a valid React component (or simply HTML tag ---like span).

    However in your case, I prefer we extract subjects before passing it into the table and then generate children array dynamically. Something like below:

    
    const renderContent = (value, row, index) => {
      return setupPerformance(value)
    
    };
    
    const setupPerformance = performance => {
      return performance.map(p => {
        const { id, title, percentage } = p;
        return <span>{percentage}%</span>
      });
    };
    const data = [
      {
        key: 0,
        firstName: "John",
        lastName: "Smith",
        performance: [
          {
            id: 1,
            title: "science",
            percentage: 75
          },
          {
            id: 2,
            title: "physics",
            percentage: 36
          }
        ]
      },
      {
        key: 1,
        firstName: "Ann",
        lastName: "Smith",
        performance: [
          {
            id: 1,
            title: "science",
            percentage: 68,
            timeSpent: 50,
            completionDate: "2019-02-07"
          },
          {
            id: 2,
            title: "physics",
            percentage: 100
          }
        ]
      }
    ];
    let subjects = data[0].performance
    const columns = [
      {
        title: "Full Name",
        children: [
          {
            title: "firstName",
            dataIndex: "firstName",
            key: "firstName"
          },
          {
            title: "lastName",
            dataIndex: "lastName",
            key: "lastName"
          }
        ]
      },
      {
        title: "Performance",
        dataIndex: "performance",
        children: 
        subjects.map(e => {
           return {
              title: e.title,
              dataIndex: "performance["+(e.id-1)+"].percentage",
              key: "key-"+e.id,
              render: value => <span>{value}%</span>
            }
        })
    
      }
    ];