// Code goes here
$(document).ready(function() {
/*********** mainTable ***************/
var mainTable = $('#table1').dataTable({
"ajax": "https://api.myjson.com/bins/zvujb",
"columns": [{
"data": "id"
}, {
"data": "name"
}, {
"data": "subtype"
}, {
"data": "approximate_count"
}, {
"data": "time_created"
}],
"columnDefs": [{
"targets": 0,
"checkboxes": {
"selectRow": true
},
"render": function(data, type, full, meta) {
return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
}
}],
"scrollY": "200px",
}); // mainTable
/*********** SecondTable ***************/
var secondTable = $('#table2').dataTable({
"columns": [{
"data": "id"
}, {
"data": "name"
}, {
"data": "subtype"
}, {
"data": "approximate_count"
}, {
"data": "time_created"
}],
"columnDefs": [{
"targets": 0,
"checkboxes": {
"selectRow": true
},
"render": function(data, type, full, meta) {
return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
}
}],
"scrollY": "200px",
"scrollCollapse": "true"
}); // secondTable
/*********** ThirdTable ***************/
var ThirdTable = $('#table3').dataTable({
"columns": [{
"data": "id"
}, {
"data": "name"
}, {
"data": "subtype"
}, {
"data": "approximate_count"
}, {
"data": "time_created"
}],
"columnDefs": [{
"targets": 0,
"checkboxes": {
"selectRow": true
},
"render": function(data, type, full, meta) {
return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
}
}],
"scrollY": "200px",
"scrollCollapse": "true"
}); // ThirdTable
/*************** SelecT OPTION ****************/
mainTable.on('click', 'tbody tr', function() {
$(this).toggleClass('selected');
});
$('#copyToTable2,#copyToTable3').on('click', function() {
let $elem = $(this);
var table = $("#table" + $elem.attr('id').replace(/[a-zA-Z]/ig, ''));
var tbl_id = table.attr('id');
var $row = mainTable.find(".selected");
if (!$row.length) {
console.log('You must select some rows to copy first');
return;
} else {
var r = confirm("Copy to table " + tbl_id + "?");
var table_to_copy = table.dataTable();
if (r == true) {
copyRows(mainTable, table_to_copy);
console.log("Copied!");
setTimeout('console.clear()', 2000);
} else {
// do nothing..
}
}
});
}); // end of $(document).ready...
function copyRows(fromTable, toTable) {
var $row = fromTable.find(".selected");
$.each($row, function(k, v) {
if (this !== null) {
addRow = fromTable.fnGetData(this);
toTable.fnAddData(addRow);
}
});
}
/* Styles go here */
#table2_wrapper {
margin-top: 50px;
margin-left: 50px;
}
#table1_wrapper {
margin-left: 50px;
}
table.dataTable tbody tr.selected {
background-color: #b0bed9;
}
table.dataTable.display tbody tr.odd.selected>.sorting_1,
table.dataTable.order-column.stripe tbody tr.odd.selected>.sorting_1 {
background-color: #a6b3cd;
}
table.dataTable.display tbody tr:hover.selected>.sorting_1,
table.dataTable.display tbody tr.odd:hover.selected>.sorting_1,
table.dataTable.display tbody tr.even:hover.selected>.sorting_1,
table.dataTable.order-column.hover tbody tr:hover.selected>.sorting_1,
table.dataTable.order-column.hover tbody tr.odd:hover.selected>.sorting_1,
table.dataTable.order-column.hover tbody tr.even:hover.selected>.sorting_1 {
background-color: #a1aec7;
}
#Copy {
background: black;
color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables.css" rel="stylesheet" />
<link href="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables_themeroller.css" rel="stylesheet" />
<div class="allTable">
<div>
<h2>TABLE 1</h2>
<table id="table1" class="table table-bordered table-hover">
<thead>
<tr>
<th></th>
<th>Audience Name</th>
<th>Type</th>
<th>Size</th>
<th>Date Created</th>
</tr>
</thead>
</table>
</div>
<div>
<br>
<br>
<h2 id="copyToTable2">TABLE 2</h2>
<table id="table2" class="table table-bordered table-hover">
<thead>
<tr>
<th></th>
<th>Audience Name</th>
<th>Type</th>
<th>Size</th>
<th>Date Created</th>
</tr>
</thead>
</table>
</div>
<br>
<h2 id="copyToTable3">TABLE 3</h2>
<div>
<table id="table3" class="table table-bordered table-hover">
<thead>
<tr>
<th></th>
<th>Audience Name</th>
<th>Type</th>
<th>Size</th>
<th>Date Created</th>
</tr>
</thead>
</table>
</div>
</div>
だけで他の場所でホストされている外部のデモにリンクして私たちを期待していない、あなたの質問に、あなたの "* [MCVE] *" コード(該当するHTML、CSSおよびJavaScript)を含めてください見に行く。 –
ちょうどあなたのために答えを投稿しました –