I get an error when adding a row to the data table:
Error DataTable
I want to add a row to the beginning of the table, and then each column in a new row will have an editable text field to enter new information. I looked online through a couple of different links, and nothing came close to what I'm looking for. I am also pretty new to coding, so I might be missing out on something really obvious!
Here is my code:
HTML:
<div class="container-fluid full-width-container data-tables">
<h1 class="section-title" id="services">
<span>Data Table</span>
</h1>
<ol class="breadcrumb text-left">
<li><a href="index.html">Dashboard</a></li>
<li class="active">Data Table</li>
</ol>
<section class="row component-section">
<div class="col-md-12">
<div class="component-box">
<div class="pmd-card pmd-z-depth pmd-card-custom-view">
<div class="table-responsive">
<table id="example-checkbox" class="table pmd-table table-hover table-striped display responsive nowrap" cellspacing="0" width="100%">
<thead>
<input type="button" value="Add Link" id="addbtn" />
<tr>
<th></th>
<th>First name</th>`enter code here`
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>Tiger</td>
<td>Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
</tr>
<tr>
<td></td>
<td>Garrett</td>
<td>Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
</tr>
<tr>
<td></td>
<td>Ashton</td>
<td>Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
</div>
JS:
$(document).ready(function() {
$('#example-checkbox').DataTable({
responsive: false,
columnDefs: [ {
orderable: false,
targets:0,
} ],
select: {
style: 'multi',
selector: 'td:first-child'
},
order: [ 1, 'asc' ],
bFilter: true,
bLengthChange: true,
pagingType: "simple",
"paging": true,
"searching": true,
"language": {
"info": " _START_ - _END_ of _TOTAL_ ",
"sLengthMenu": "<span class='custom-select-title'>Rows per page:</span> <span class='custom-select'> _MENU_ </span>",
"sSearch": "",
"sSearchPlaceholder": "Search",
"paginate": {
"sNext": " ",
"sPrevious": " "
},
},
dom:
"<'pmd-card-title'<'data-table-title'><'search-paper pmd-textfield'f>>" +
"<'custom-select-info'<'custom-select-item'><'custom-select-action'>>" +
"<'row'<'col-sm-12'tr>>" +
"<'pmd-card-footer' <'pmd-datatable-pagination' l i p>>",
});
$('#addbtn').click(addrow);
function addrow() {
$('#example-checkbox').dataTable().fnAddData( [
$('#fname').val(),
$('#lname').val(),
$('#position').val(),
$('#office').val(),
$('#age').val(),
$('#start').val(),
] );
}
$("div.data-table-title").html('<h2 class="pmd-card-title-text">Data table</h2>');
} );
source
share