1

I'm using Reactable to create a table. When clicked on the action box, I want a box with extra information about that row to appear by changing the style. This is my code so far:

var SelLines  = new Object();
var Home = React.createClass({

    handleClick: function(e) {
        console.log("##Clicked##");
    },  

    render: function() {    
        var tableRows = Data.map(function(tableRow) {
            console.log(tableRow);
            SelLines[tableRow.ID] = false;
            console.log(SelLines);
            if (SelLines[tableRow.ID]) {
                return (
                    <Tr>
                        <Td column="ID">{tableRow.ID}</Td>
                        <Td column="DESCRIPTION">{tableRow.DESCRIPTION}</Td>
                        <Td className="cellSelected" column="ACTION"><a onClick={SelLines[tableRow.ID] = true;Home.handleClick}>{tableRow.ACTION}</a></Td>
                         //// = true;Home.h ## not working
                    </Tr>
                );
            } else {
                return (
                    <Tr>
                        <Td column="ID">{tableRow.ID}</Td>
                        <Td column="DESCRIPTION">{tableRow.DESCRIPTION}</Td>
                        <Td className="cellNotSelected" column="ACTION"><a onClick={Home.handleClick}>{tableRow.ACTION}</a></Td>
                    </Tr>
                );
            }
        });
        return (
            <div>
                <div>
                    <Table className="table" id="table">
                        <Thead>
                            <Th column="ID">
                                <strong className="ID-header">ID</strong>
                            </Th>
                            <Th column="DESCRIPTION">
                                <em className="DESCRIPTION-header">DESCRIPTION</em>
                            </Th>
                            <Th column="ACTION">
                                <em className="ACTION-header">ACTION</em>
                            </Th>
                       </Thead>
                       {tableRows}
                   </Table>
                </div>
            </div>
        );
    }
});

How can I pass a parameter to handleClick?

3
  • onClick={SelLines[tableRow.ID] = true;Home.handleClick} is the equivalent to {onClick: SelLines[tableRow.ID] = true;Home.handleClick}. That's simply invalid JavaScript. The {...} can only contains expressions. Commented Mar 31, 2016 at 16:05
  • Can you fix your formatting? Also, you are setting SelLines[tableRow.ID] = false in your map function so it will never get to your if true condition. Commented Mar 31, 2016 at 17:14
  • @PL I fixed your formatting for you. Next time please post well-formatted code. Commented Apr 1, 2016 at 16:13

1 Answer 1

1

You can use function.bind() to prepend arguments to be called with when the function is invoked.

<a onClick={this.handleClick.bind(this, tableRow.ID)}>{tableRow.ACTION}</a>

handleClick: function(tableRowId, e) {
    SelLines[tableRowId] = true;
    console.log("##Clicked##", tableRowId);
}

Since you are rendering inside a map() callback you also have to bind the callback:

var tableRows = Data.map(function(tableRow){
    // ...
}.bind(this));

Or if you use ES6 you can write arrow functions to avoid needing to bind:

var tableRows(Data.map((tableRow) => {
    // ...
});

And you can write an inline arrow function as your handler which calls through to another function with some arguments:

<a onClick={(e) => this.handleClick(tableRow.ID)}>{tableRow.ACTION}</a>
Sign up to request clarification or add additional context in comments.

2 Comments

That works when I call it in the return of home, but not in he return of tableRows - how can I bind it to the parent item?
I missed your map function because of how it's formatted. You need to either bind your map callback or use an arrow function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.