Xander Selorm
Xander Selorm

Reputation: 718

How to add and remove <tr> to table based on checkbox selection in jQuery

I'm trying to create a users' permission table which will display the type of permission to assign a user, based on a selection from a checkbox.

I have tried several means I know about and also searched but I always seem to get stuck everytime. I got a similar code from this answer, jQuery append and remove values from checkbox to textarea , which works. But when I try implementing it in my work, it doesn't seem to work.

<table id="permissionsTable">
  <thead>
    <th>Name</th>
    <th>Slug</th>
    <th>Description</th>
  </thead>
  <tbody id="tableBody">

  </tbody>
</table>

<div id="alloptions">
  <ul>
    <li>
      <input type="checkbox" name="option1" value="Play" />Play</li>
    <li>
      <input type="checkbox" name="option1" value="Create" />Create</li>
    <li>
      <input type="checkbox" name="option1" value="Delete" />Delete</li>
  </ul>
</div>

And here's the jQuery Code:

var checkboxes = $("input[type='checkbox']");
var permsTable = $("#permissionsTable");
var valuer = [];


checkboxes.on('change', function() {
  var values = checkboxes.filter(':checked').map(function(x, y) {
    return y.value;
  }).get();

  checkboxes.filter(':checked').each(function(a, b) {

    valuer.push($(this).val());

  });


  if (checkboxes.is(':checked')) {


    permsTable.find('tbody').detach();
    permsTable.append($('<tbody>'));


    $.each(valuer, function(c, d) {
      //alert(x + " : " + y);
      var nameCol = d.substr(0, 1).toUpperCase() + d.substr(1) + " " + "Game";

      var slugCol = d.toLowerCase() + "-" + "Game";
      var descriptionCol = "Allow user to " + d.toUpperCase() + " a " + "Game";

      $("#permissionsTable").find('tbody').append("<tr><td>" + nameCol + "</td><td>" + slugCol + "</td><td>" + descriptionCol + "</td></tr>");

    });


  } else {
    permsTable.find('tbody').detach();
    permsTable.append($('<tbody>'));
    btnGeneratePermissions.css("display", "none");
  }
});

I expect to add the values of the checkboxes to the table row when checkbox is checked; and then remove the row upon uncheck.

Upvotes: 0

Views: 1175

Answers (2)

Sabrina Yu
Sabrina Yu

Reputation: 11

You can just use the append function. The logic will be quite easy.

 $(".chx").change(function() {
   var row = document.createElement("tr");
   
    if(this.checked) { 
      var v = $(this).val();                   
      row.innerHTML = `<td>${v} Game</td>
                       <td>${v.toLowerCase()}-Game</td>
                       <td>Allow user to ${v.toUpperCase()} a Game</td>`;      
    row.setAttribute("name",$(this).val()); 
      $('#tableBody').append(row);  
    }
  else{    
    $("tr[name="+$(this).val()+"]").remove();
  }
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  
<body>
 
<table id="permissionsTable">
  <thead>
    <th>Name</th>
    <th>Slug</th>
    <th>Description</th>
  </thead>
  <tbody id="tableBody">
    
  </tbody>
</table>

<div id="alloptions">
  <ul>
    <li>
      <input type="checkbox" value="Play" class="chx" />Play</li>
    <li>
      <input type="checkbox" value="Create" class="chx"/>Create</li>
    <li>
      <input type="checkbox" value="Delete" class="chx" />Delete</li>
  </ul>
</div>
</body>
</html>

Upvotes: 0

Jon P
Jon P

Reputation: 19802

Using a Template Literal, this is pretty easy

$("#alloptions").on("change", "[name=option1]", function(){
  var mode = $(this).val();
  const rowTemp = `<tr data-mode=${mode}>
<td><span style="text-transform: capitalize;">${mode} Game</span></td>
<td><span style="text-transform: lowercase;">${mode}</span>-Game</td>
<td>Allow user to <span style="text-transform: uppercase;">${mode}</span></td>
</tr>`;

  if($(this).is(":checked")) {
    $("#tableBody").append(rowTemp)
  }else{    
   $("[data-mode='"+ mode +"']").remove();
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<table id="permissionsTable">
  <thead>
    <th>Name</th>
    <th>Slug</th>
    <th>Description</th>
  </thead>
  <tbody id="tableBody">

  </tbody>
</table>

<div id="alloptions">
  <ul>
    <li>
      <input type="checkbox" name="option1" value="Play" />Play</li>
    <li>
      <input type="checkbox" name="option1" value="Create" />Create</li>
    <li>
      <input type="checkbox" name="option1" value="Delete" />Delete</li>
  </ul>
</div>

Upvotes: 1

Related Questions