I am trying make the rows from table 1, editable as well i would like add a new row too.
I found this sample working perfeclty here. But i could not apply in my sample here in my jsfiddle
Iam pasting the working sample as snippet here below
function addUser() {
var $source = $("#example2").DataTable();
var names = $($source.rows().nodes())
.filter(':has(:checked)')
.map(function() {
return $(this).children().first().text();
}).toArray();
console.log(names);
var $rows = $source.rows(function(i, data) {
return names.indexOf('' + data.name) != -1;
});
var data = $rows.data();
if (data.length > 0) {
$rows.remove().draw();
$("#example").DataTable().rows.add(data.toArray()).draw();
}
}
function checkAll(check) {
$('#chk').prop('checked', check == 1);
var $source = $("#example2").DataTable();
var names = $($source.rows().nodes())
.find('input[type="checkbox"]')
.each(function(i, el) {
el.checked = check == 1;
});
return false;
}
function all() {
var a = 0;
var $source = $("#example2").DataTable();
var names = $($source.rows().nodes())
.find('input[type="checkbox"]')
.each(function(i, el) {
if (el.checked) {
a++
}
console.log(a);
});
if (a == 5) {
$('#chk').prop('checked', true);
} else {
$('#chk').prop('checked', false);
}
}
$(document).ready(function() {
var dt = $('#example').dataTable();
dt.fnDestroy();
});
$(document).ready(function() {
var url = 'http://www.json-generator.com/api/json/get/cvQJpCWCuW?indent=2';
var table = $('#example').DataTable({
ajax: url,
createdRow: function(row, data, dataIndex) {
$(row).attr('id', 'row-' + dataIndex);
},
rowReorder: {
dataSrc: 'order',
},
columns: [{
data: 'name'
}, {
data: 'name'
}, {
data: 'place'
}]
});
table.rowReordering();
});
$(document).ready(function() {
var dt = $('#example2').dataTable();
dt.fnDestroy();
});
$(document).ready(function() {
var url = 'http://www.json-generator.com/api/json/get/cnmZgfsBKa?indent=2';
var table = $('#example2').DataTable({
ajax: url,
createdRow: function(row, data, dataIndex) {
$(row).attr('id', 'row-' + dataIndex);
},
rowReorder: {
dataSrc: 'order',
},
columns: [{
data: 'name'
}, {
data: 'checkbox'
}]
});
table.rowReordering();
});
$(document).ready(function() {
$('#example2').on('click', 'input[type="checkbox"]', function() {
all();
});
$('body').on('mouseenter', 'input', function() {
$('.btn').prop('disabled', true);
});
$('body').on('mouseout', 'input', function() {
$('.btn').prop('disabled', false);
});
$('#chk').on('change', function() {
console.log($(this));
if ($(this)[0].checked) {
checkAll(1);
} else {
checkAll(0);
}
});
$('#checkAll').click(function() {
checkAll(1);
});
$('#uncheckAll').click(function() {
checkAll(0);
});
$('#btnAddUser').click(function() {
addUser();
});
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script src="//cdn.rawgit.com/DataTables/RowReorder/ce6d240e/js/dataTables.rowReorder.js"></script>
<link href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="//cdn.datatables.net/rowreorder/1.2.0/css/rowReorder.dataTables.min.css" rel="stylesheet" />
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<br>
<br>
<h1>
table1
</h1>
<br>
<br>
<table id="example" class="display" width="100%" cellspacing="0">
<thead>
<tr>
<th>First name</th>
<th>Place</th>
<th>Order</th>
</tr>
</thead>
</table>
<br>
<br>
<h1>
table 2
</h1>
<br>
<br>
<table id="example2" class="display" width="100%" cellspacing="0">
<thead>
<tr>
<th>Place</th>
<th>checkbox</th>
</tr>
</thead>
</table>
<div class="col-md-12">
<div class="col-md-6">
<button type="button" class="btn btn-success" id="btnAddUser">Add a user</button>
</div>
<div class="col-md-6">select all or none
<div class="btn-group">
<div class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<input type="checkbox" name="vehicle1" value="Bike" id="chk">
<i class="fa fa-caret-down" aria-hidden="true"></i>
</div>
<ul class="dropdown-menu" role="menu">
<li><a id="checkAll">All</a></li>
<li><a id="uncheckAll">None</a></li>
</ul>
</div>
</div>
</div>
tdof of the table id and append to it.