How to make dataTable.js work in this example (dynamic jQuery Table)?

How to make dataTable.js work in this example (dynamic jQuery Table)?

shu595shu595 Posts: 1Questions: 1Answers: 0
edited December 2016 in Free community support

I need to use datatable to add sorting, pagination, and searching to my table that I created in this example. However, I can't get it to work. I spent quite a time mitigating the type error with no luck. Whenever I try to add data tables, I get this error "Uncaught TypeError: Cannot read property 'length' of undefined" Please help me out! Please suggest a solution. I have already used Bootstrap table and dynatable. I need datatable to solve my problem of live dom sorting. Here is my code

```

  <!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</head>
<body>
<div class="container">
  <form >
  <div class="form-group"> <!-- Email field !-->
    <label for="name" class="control-label">Name</label>
    <input type="name" class="form-control" id="name" name="name" placeholder="John Doe">
  </div>
  <div class="form-group"> <!-- Email field !-->
    <label for="mail" class="control-label">Mail</label>
    <input type="mail" class="form-control" id="mail" name="mail" placeholder="name@domain.com">
  </div>
  <div class="form-group"> <!-- Email field !-->
    <label for="mobile" class="control-label">Mobile No.</label>
    <input type="mobile" class="form-control" id="mobile" name="mobile" placeholder="xxx-xxx-xxxx">
  </div>
  <div class="form-group"> <!-- Submit button !-->
  <a id="add_row" type="submit" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete</a>
  </div>  
</form>
    <div class="row clearfix">
    <div class="col-md-12 column">
      <table  class="table table-bordered table-hover" id="tab_logic" ">
        <thead>
          <tr >
            <th class="text-center">
              #
            </th>
            <th class="text-center">
              Name
            </th>
            <th class="text-center">
              Mail
            </th>
            <th class="text-center">
              Mobile
            </th>
          </tr>
        </thead>
        <tbody>
        <tr id='addr0'></tr>
        </tbody>
      </table>
    </div>
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){    
       $("#tab_logic").dataTable();
    var i=0;
    $("#add_row").on('click',function(){
        var name= $("#name").val();
        var mail =$("#mail").val();
        var mobile=$("#mobile").val();
        console.log("THE NAME IS "+ name);
       $('#addr'+i).html("<td>"+ (i+1) +"</td><td>" +name+ "</td><td>" +mail+"</td><td> "+mobile+"</td>");
       $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
       i++; 
    });
     $("#delete_row").click(function(){
       if(i>0){
     $("#addr"+(i-1)).html('');
     i--;
     }
   });
});
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

```

I made sure that i only use jQuery once and before importing dataTables from my research.

Answers

  • kthorngrenkthorngren Posts: 21,558Questions: 26Answers: 4,994

    The error is generated by these lines:

            <tbody>
            <tr id='addr0'></tr>
            </tbody>
    

    Remove them and you will be one step closer.

    You may want to change $("#tab_logic").dataTable(); to $("#tab_logic").DataTable();. Note the upper case D.

    The code you have doesn't add rows on my system. You may want to look at the row.add() api to append your form entry:
    https://datatables.net/examples/api/add_row.html

    Kevin

This discussion has been closed.