Detect clicked TR go through REACT function

Asked

Viewed 50 times

0

How to detect the TH I clicked and pass parameter in React?

I wanted to make an ordination, but I’m having a hard time getting past a parameter in a TH function that I clicked, can someone help me with this?

ordertableBy = async(ev) => {
    console.log(ev.target.value);
    alert('orderby' + ev.target.value);
}
<tr>
    <th onClick={ev => this.ordertableBy(ev)}>Nome</th>
    <th onClick={ev => this.ordertableBy(ev)}>Email</th>
    <th onClick={ev => this.ordertableBy(ev)}>Telefone</th>
</tr>

Is returning order Undefined

1 answer

0


Very simply you can do so

<th onClick={ev => this.ordertableBy("headerNome")}>Nome</th>
<th onClick={ev => this.ordertableBy("headerEmail")}>Email</th>
<th onClick={ev => this.ordertableBy("headerTelefone")}>Telefone</th>

so that your ordertableby would simply be

ordertableBy = async(value) => {
    console.log(value);
    alert('orderby' + value);
}

If you want to use the event, would be something like

<th value="headerNome" onClick={ev => this.ordertableBy(ev)}>Nome</th>
<th value="headerEmail" onClick={ev => this.ordertableBy(ev)}>Email</th>
<th value="headerTelefone" onClick={ev => this.ordertableBy(ev)}>Telefone</th>

ordertableBy = async(ev) => {
    console.log(ev.target.value);
    alert('orderby' + ev.target.value);
}

PS: Usually used and not when it comes to the table header

  • Our thanks Joseph! Saved! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.