2

I recently created a function that does some complicated name matching based on user input into a website and returns the result as an html file in a table format. My question is how do I add click feature on each row?

df = get_cust_info()  # returns a pandas dataframe
df.to_html('results.html')  #converts the dataframe to html

Idea is user types his name, (if he misspells it, the program suggests following names. Hence a table is returned in html format.

Now I want to add click feature on each row. The click hopefully requeries the database that I have internally and returns the right information. It has to be dynamic. By this, I mean if one row is returned, you can click on only one row. if 10 rows are returned, you should be able to click on any row. Thank you

Edit: Based on the answer below, I added the following lines but am still having trouble getting the row to click. Not sure what im doing wrong.

A sample html file

    <table border="1" class="dataframe">
     <thead>
     <tr style="text-align: right;"><th></th>
     <th>CustomerName</th>
     <th>firstName</th>
    <th>MiddleName</th>
    <th>LastName</th>
    <th>Address</th>
    <th>zip</th>
    <th>State</th>
    <th>score_first</th>
    <th>score_second</th>
    <th>score_third</th>
    </tr>
   </thead>
   <tbody>
<tr>
  <th>5046</th>
  <td>JAMES G STAHL</td>
  <td>JAMES</td>
  <td>G</td>
  <td>STAHL</td>
  <td>4090 BECK RD</td>
  <td>44256</td>
  <td>OH</td>
  <td>0.97</td>
  <td>1.0</td>
  <td>0.91</td>

<tr>
  <th>5050</th>
  <td>JONATHAN STAHL</td>
  <td>JONATHAN</td>
  <td>None</td>
  <td>STAHL</td>
  <td>4114 BECK RD</td>
  <td>44256</td>
  <td>OH</td>
  <td>0.57</td>
  <td>NaN</td>
  <td>0.91</td>
</tr>
   </tbody>
   </table>

Edit 2: Upon further search on another stackoverflow question, I tried adding this to the bottom.

 <script>
document.querySelector(".dataframe").addEventListener("click", function(e) {
var row = e.target;
while (row.matches && !row.matches("tr")) {
    row = row.parentNode;
}
if (row.matches && row.matches("tr") { 
    alert(row.innerHTML);
}
});
</script>

Still not working. Im sorry im new to javascript/jquery. If anyone can help that would be a huge help

turtle_in_mind
  • 986
  • 1
  • 18
  • 36

2 Answers2

2

The answer is to use event.target in JavaScript. You can add an event listener to the table itself and then inside the event listener detect which element was actually clicked inside it. Something like this should work:

document.querySelector(".dataframe").addEventListener("click", function(e) {
    var row = e.target;
    while (row.matches && !row.matches("tr")) {
        row = row.parentNode;
    }
    if (row.matches && row.matches("tr") { // check for row.matches because at the very top of the document tree row.matches will be undefined but row itself will be non-null
        // This is the table row element you're looking for
    }
});
Robert Moore
  • 2,207
  • 4
  • 22
  • 41
0

I managed to make the rows clickable by doing the following: I hope it helps the community as some of the answers I found here weren't helpful.

<script>
function addRowHandlers() {
var table = document.querySelector(".dataframe");
var rows = table.getElementsByTagName("tr");
for (i = 1; i < rows.length; i++) {
    var currentRow = table.rows[i];
    var createClickHandler = 
        function(row) 
        {
            return function() { 
                                    var cell = row.getElementsByTagName("td")[0];
                                    var id = cell.innerHTML;
                                    alert("id:" + id);
                             };
        };

    currentRow.onclick = createClickHandler(currentRow);
    }
}
window.onload = addRowHandlers();
</script>
turtle_in_mind
  • 986
  • 1
  • 18
  • 36