How to use the moment library in React Bootstrap table's column definition?

拥有回忆 提交于 2021-01-29 10:24:26

问题


My code looks as following:

const columns = [{
  dataField: 'Id',
  text: 'Order ID'
}, {
  dataField: 'Date',
  text: 'Date'
}, {
  dataField: 'Total',
  text: 'Total'
}];

And it displays date in React Bootstrap table. But, the date is in Json format that I don't need.

I've tried to use the moment library to format date in this way:

const columns = [{
  dataField: 'Id',
  text: 'Order ID'
}, {
  dataField: '{moment(Date).format("DD-MM-YYYY")}',
  text: 'Date'
}, {
  dataField: 'Total',
  text: 'Total'
}];

But, the date column is empty.

How can I use the moment library to format the dates for columns in React Bootstrap table's library? Or there is some another way to do it?


回答1:


column.formatter can help you. There's a online demo: this




回答2:


Here are the sample code in rendor method

render () {
        let columns = []
        columns = [
         { name: 'createAt',
        displayName: 'my-Date',
        dataFormat: function callback (cell, row, rowIndex, colIndex) { moment(cell).format('DD.MM.YYYY h:mm') },
        hideFilter: true,
        isKey: true,
        width: '140'
    }]


来源:https://stackoverflow.com/questions/51278910/how-to-use-the-moment-library-in-react-bootstrap-tables-column-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!