1
I created a table using the Reacttable component and am feeding it data from an api. One of these data returns as 0 or -1 and I want to treat them so that if it comes 0 it appears "Enabled" in the table, if it is -1 it is "Disabled". I’m new to Reactjs and I don’t really know how to do it, you can give me a hand?
Follows the code:
class DataTable extends Component{
constructor(props){
super(props);
this.state = {
dadosGrupo:'',
}
this.state = {
isShowing: false
}
this.state = {
status:''
}
}
componentDidMount(){
const url ='http://xxx'
fetch(url, {
method: 'GET'
}).then(response => response.json())
.then(dados => {
this.setState({dadosGrupo: dados})
})
.then(Inativo => {this.setState({status:Inativo})})
}
openModalHandler = () => {
this.setState({
isShowing: true
});
}
closeModalHandler = () => {
this.setState({
isShowing: false
});
}
render(){
const columns = [
{
Header: "Status",
accessor: "Inativo",
style:{
textAlign:"center",
color:'white',
},
width:100,
maxWidth:100,
minWidth:100,
Cell: props => {
return(
<div>
{this.state.inativo === 0 ? <div style={{color:'green'}}>Ativado</div> : <div style={{color:'red'}}>Desativado</div>}
</div>
)
}
},
{
Header: "Cdgrupo",
accessor: "Cdgrupo",
style:{
textAlign:"center"
},
width:100,
maxWidth:100,
minWidth:100,
},
{
Header: "Grupo",
accessor: "Grupos",
sortable: false
},
{
Header: "Ação",
sortable: false,
filterable:false,
width:100,
maxWidth:100,
minWidth:100,
style:{
textAlign:"Center"
},
Cell: props =>{
return(
<div>
{this.state.isShowing ? <div className="backdrop2" ></div> : null }
<button className='modaleditar btn btn-info' onClick={this.openModalHandler}>Editar</button>
</div>
)
},
},
]
return(
<div>
<ReactTable
columns={columns}
data={this.state.dadosGrupo}
filterable
defaultPageSize={10}
noDataText={"Não foi possível carregar os dados."}
>
</ReactTable>
<ModalEdit
className="modal"
show={this.state.isShowing}
close={this.closeModalHandler}>
</ModalEdit>
</div>
)
}
}
export default DataTable;
The status I created in state and the parole within Columns was just to test this condition, I imagine it is for her that it will be done right?
The table is returning like this:
If you need more information just say!
Thanks for the Virgilio response, the way you’ve actually shown it works, but in the case these values 0 and -1 are passed from an api by json, then ideally I treat this On/Off for all objects that come in json at once instead of creating one by one. Following the reasoning of your example is possible?
– Gabriel Midão
@Gabrielmidão if you receive this API from third parties, the data already comes like this, if you have this API yes you can already build your result the way you want. From what I understand is third party (just confirm me) if it is, the answer I gave you is the answer you need, because the Component itself already plays the role of changing this for you giving the possibility as shown above. Another point is that I answered the question the way it is in the original, so any other type of doubt of higher value is not well seen here because it alters the original reason of the answers by invalidating.....
– novic
continue @Gabrielmidão if the answer answered the question question mark the answer that helped you, and then ask a new question for a new question. Thank you.
– novic
Yes, it is third party. If I understood correctly, the way you showed I would have to create from one to the right? If this is the case, it is impossible because there are more than 150 objects coming from the API. What I’m looking to understand is how to make it change the moment I request the API using Axios.
– Gabriel Midão
@Gabrielmidão is a single made! You did not understand the example for sure.
– novic
@Gabrielmidão if in the third party API does not have this possibility this is how it resolves recreating the object but, this is a line for Javascript and also in no time said who is the API, what is the documentation, the address and this is not part of your original question. But this is how I showed you how it solves and is also proposed by the Component
– novic
I read your example again with more attention and managed to resolve, thank you very much and forgive me for anything Virgilio!
– Gabriel Midão