javascript - Find table row by a given cell value and delete that row - Stack Overflow

I have a table with 3 columns-<table id="my_list"><thead><tr><th>colu

I have a table with 3 columns-

<table id="my_list">
    <thead>
    <tr>
        <th>column 1</th>
        <th>column 2</th>
        <th>column 3</th>
    </tr>
    </thead>
    <tr>
        <td>value 1</td>
        <td>value 2</td>
        <td>value 3</td>
    </tr>
</table>

I'm writing a function that searches the table for a given_value and deletes the row that contains it-

function delete_row (given_value) {
    var table_row = $("td").filter(function() {
        return $('#my_list').text() == given_value;
    }).closest("tr");

    // Removing the row from `my_list` table
    table_row.remove();

}

But this is not deleting the row.

Also, I only want to search the text values in column 2, so if there's a faster way to search the table, that would be great to know too.

I have a table with 3 columns-

<table id="my_list">
    <thead>
    <tr>
        <th>column 1</th>
        <th>column 2</th>
        <th>column 3</th>
    </tr>
    </thead>
    <tr>
        <td>value 1</td>
        <td>value 2</td>
        <td>value 3</td>
    </tr>
</table>

I'm writing a function that searches the table for a given_value and deletes the row that contains it-

function delete_row (given_value) {
    var table_row = $("td").filter(function() {
        return $('#my_list').text() == given_value;
    }).closest("tr");

    // Removing the row from `my_list` table
    table_row.remove();

}

But this is not deleting the row.

Also, I only want to search the text values in column 2, so if there's a faster way to search the table, that would be great to know too.

Share Improve this question edited Jul 22, 2019 at 17:01 kev asked Jul 22, 2019 at 16:20 kevkev 2,8815 gold badges25 silver badges49 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

I think this is what you are looking for, but you can loop over the td elements and check their text and if you get a match, delete the tr element.

Edit: If you wanted to specify an id, you could add that to the function like such. Then just give the function the id name and what value to look for.

function delete_row(id, given_value) {
  var td = $("#" + id + " td");
  $.each(td, function(i) {
    if ($(td[i]).text() === given_value) {
      $(td[i]).parent().remove();
    } 
  });
}

delete_row('my_list', 'value 1');
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="my_list">
  <thead>
  <tr>
    <th>column 1</th>
    <th>column 2</th>
    <th>column 3</th>
  </tr>
  </thead>
  <tr>
    <td>value 1</td>
    <td>value 2</td>
    <td>value 3</td>
  </tr>
</table>

The filter function actually passes a couple things as arguments, including the current element. To check the text of that element, rather than the text of "#my_list", use $(el).text().

After that, 'trim' the value of the text, to remove the whitespace before and after the text. Otherwise its paring ' value 2 ' with 'value 2', and it will not produce a match.

As a side note, the standard within JavaScript is to use camelCase instead of snake_case.

function deleteRow(givenValue) {
  var tableRow = $("td")
    .filter(function(index, el) {
      return (
        $(el) // <-- the <td> element
          .text()
          .trim() === givenValue
      );
    })
    .closest("tr");
  // Removing the row from `my_list` table
  tableRow.remove();
}

Are you sure given_value matches the html text? There's spaces before and after text which may be your problem. Also, make sure table_row is not empty after the filter function.

function delete_row(given_value) {
    var table_row = $("td").filter(function () {
        return $('#my_list').text() == given_value;
    });

    if (table_row.length > 0) {
        // Removing the row from `my_list` table
        table_row.closest("tr").remove();
    } else {
        console.log('nothing matching given_value!')
    }
}

Try this simple code:

function RemoveFromTable(tblID, VALUE){
     $("#"+tblID).find("td:contains('"+VALUE+"')").closest('tr').remove();
}

Call the function as follows:

RemoveFromTable('table ID','value')

You need to match <td> instead of the entire table. Then trim the value to remove any leading/trailing white spaces. Then use the index parameter from the filter function to get the nth <td> element (using eq) and check its value for the desired value, and then remove it.

function delete_row (given_value) {
    var table_row = $("td").filter(function(idx) {
        return $("td").eq(idx).text().trim() == given_value;
    });

    // Removing the row from `my_list` table
    table_row.remove();

}

I would also suggest to set the value to be an empty string so as to not disrupt the structure of the table.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745251964a4618731.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信