简体   繁体   English

如何做React-Table动态标头?

[英]How to do React-Table Dynamic Header?

I want to give a date to the titles in the columns, but I get an error when I try to subtract one day. 我想给列中的标题给出一个日期,但是当我尝试减去一天时出现错误。 I'd appreciate it if you could help. 如果你能提供帮助,我会很感激的。

     codes : 

          render(){
          const t = new Date();


          const t1 = new Date();
          t1.setDate(t1.getDate() - 1);


         columns = [{
          {Header:t},
          {Header:t1}
         }]

         return{ 
          <ReactTable
          Columns={columns}
          />
         }
       }

I want t1 to fall for 1 day no problem in the title of t shows today's date, but I do not show the t1 title I want a day deficiency and gives the following error. 我希望t1下降1天没有问题在今天的日期显示t的标题,但我没有显示t1标题我想要一天缺乏并给出以下错误。

{
Objects are not valid as a React child (found: Tue Apr 09 2019 12:19:07 GMT+0300 (GMT+03:00)). If you meant to render a collection of children, use an array instead.
    in div (created by ReactTable)
    in div (created by ThComponent)
    in ThComponent (created by ReactTable)
    in div (created by TrComponent)
    in TrComponent (created by ReactTable)
    in div (created by Thead)
    in Thead (created by ReactTable)
    in div (created by TableComponent)
    in TableComponent (created by ReactTable)
    in div (created by ReactTable)
    in ReactTable (at drivingHours.jsx:229)
    in div (created by Col)
    in Col (created by Context.Consumer)
    in ForwardRef(Bootstrap(Col)) (at drivingHours.jsx:228)
    in div (created by Row)
    in Row (created by Context.Consumer)
    in ForwardRef(Bootstrap(Row)) (at drivingHours.jsx:227)
    in div (created by Col)
    in Col (created by Context.Consumer)
    in ForwardRef(Bootstrap(Col)) (at drivingHours.jsx:226)
    in DrivingHours (created by Connect(DrivingHours))
    in Connect(DrivingHours) (at driverReports.jsx:42)
    in div (created by TabPane)
    in Transition (created by Fade)
    in Fade (created by TabPane)
    in TabPane (created by Context.Consumer)
    in ForwardRef(Bootstrap(TabPane)) (created by Context.Consumer)
    in ForwardRef(ContextTransform) (created by Tabs)
    in div (created by TabContent)
    in TabContent (created by Context.Consumer)
    in ForwardRef(Bootstrap(TabContent)) (created by Tabs)
    in TabContainer (created by Tabs)
    in Tabs (created by Uncontrolled(Tabs))
    in Uncontrolled(Tabs) (at src/index.js:127)
    in ForwardRef (at driverReports.jsx:27)
    in section (at driverReports.jsx:26)
    in ControlledTabs (created by Connect(ControlledTabs))
    in Connect(ControlledTabs) (created by Route)
    in Route (at app/index.js:48)
    in Switch (at app/index.js:41)
    in main (at app/index.js:40)
    in div (created by Row)
    in Row (created by Context.Consumer)
    in ForwardRef(Bootstrap(Row)) (at app/index.js:28)
    in div (created by Container)
    in Container (created by Context.Consumer)
    in ForwardRef(Bootstrap(Container)) (at app/index.js:27)
    in App (created by Connect(App))
    in Connect(App) (created by Route)
    in Route (created by withRouter(Connect(App)))
    in withRouter(Connect(App)) (at src/index.js:14)
    in div (at src/index.js:13)
    in Router (created by ConnectedRouter)
    in ConnectedRouter (created by Connect(ConnectedRouter))
    in Connect(ConnectedRouter) (at src/index.js:12)
    in Provider (at src/index.js:11)}
    ```


The problem is what your value is Object. 问题是你的价值是什么。

You should convert it to string. 你应该将它转换为字符串。

For example, new Date().getTime() or new Date().toString() will be displayed. 例如,将显示new Date().getTime()new Date().toString()

Just warp your t1 in string template, like below 只需在字符串模板中扭曲你的t1,如下所示

columns = [{
      {Header:t},
      {Header:`${t1}`}
     }]

     return{ 
      <ReactTable
      Columns={columns}
      />
     }
   }

I solved the problem : 我解决了这个问题:

codes : 代码:

      render(){
      const t = new Date();


      const t1 = new Date();
      t1.setDate(t1.getDate() - 1);


     columns = [{
      {Header:t},
      {Header:t1.toISOString().split('T')[0]} /// format fixed, solved problem.
     }]

     return{ 
      <ReactTable
      Columns={columns}
      />
     }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM