23 Ağustos 2022 • 20 dakikalık okuma
BSTable JavaScript eklentisi ile, statik HTML tablolara dinamik olarak verilerini güncelleme özelliği kazandırılabilir.
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>BSTable</title>
<!-- Bootstrap core CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
<!-- JQuery library -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body id="page-top" class="sidebar-toggled">
<div role="main" class="container" style="padding-top:60px;">
<div style="margin-top:20px;">
<h2>BSTable</h2>
<!-- static table -->
<table class="table table-striped table-bordered" id="table1">
<thead class="thead-dark">
<tr>
<th scope="col">Sıra</th>
<th scope="col">Kod</th>
<th scope="col">Şehir</th>
</tr>
</thead>
<tbody id="table1-body">
<tr>
<th scope="row">1</th>
<td>39</td>
<td>Kırklareli</td>
</tr>
<tr>
<th scope="row">2</th>
<td>40</td>
<td>Kırşehir</td>
</tr>
<tr>
<th scope="row">3</th>
<td>41</td>
<td>Kocaeli</td>
</tr>
<tr>
<th scope="row">4</th>
<td>42</td>
<td>Konya</td>
</tr>
<tr>
<th scope="row">5</th>
<td>43</td>
<td>Kütahya</td>
</tr>
<tr>
<th scope="row">6</th>
<td>44</td>
<td>Malatya</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
Html tabloları dinamik hale getirmek için; BSTable JavaScript eklentisi indirilir ve bstable.js dosyası html dosyasına eklenir. Sonrasında aşağıdaki örnek kodda olduğu gibi tablo özellikleri ayarlanır;
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>BSTable</title>
<!-- Bootstrap core CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
<!-- JQuery library -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body id="page-top" class="sidebar-toggled">
<div role="main" class="container" style="padding-top:60px;">
<div style="margin-top:20px;">
<h2>BSTable</h2>
<button id="table1-new-row-button" class="btn btn-dark">Yeni Satır</button>
<!-- dynamic table -->
<table class="table table-striped table-bordered" id="table1">
<thead class="thead-dark">
<tr>
<th scope="col">Sıra</th>
<th scope="col">Kod</th>
<th scope="col">Şehir</th>
</tr>
</thead>
<tbody id="table1-body">
<tr>
<th scope="row">1</th>
<td>39</td>
<td>Kırklareli</td>
</tr>
<tr>
<th scope="row">2</th>
<td>40</td>
<td>Kırşehir</td>
</tr>
<tr>
<th scope="row">3</th>
<td>41</td>
<td>Kocaeli</td>
</tr>
<tr>
<th scope="row">4</th>
<td>42</td>
<td>Konya</td>
</tr>
<tr>
<th scope="row">5</th>
<td>43</td>
<td>Kütahya</td>
</tr>
<tr>
<th scope="row">6</th>
<td>44</td>
<td>Malatya</td>
</tr>
</tbody>
</table>
</div>
</div>
<script src="bstable.js"></script>
<script>
var bsTable1 = new BSTable("table1", {
$addButton: $('#table1-new-row-button'),
onEdit:function(rowElement) {
console.log("EDITED:", rowElement);
let rowAsJQuery = $(rowElement);
console.log("EDITED (as jquery):", rowAsJQuery);
},
advanced: {
columnLabel: ''
}
});
bsTable1.init();
</script>
</body>
</html>
Html tablo verileri harici bir api ya da web servisten alınarak tüm verilerde düzenleme, ekleme, silme ve güncelleme işlemleri yapılabilir. Örneğin; jsonplaceholder.typicode.com/users api servisinden örnek data çekiliyor;
Aşağıdaki örnek kodda olduğu gibi tablonun düzenleme, silme, güncelleme, yeni satır ekleme gibi her işlem adımındaki verilere ulaşılabilir, ilgili veriler web servise gönderilebilir;
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>BSTable</title>
<!-- Bootstrap core CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
<!-- JQuery library -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body id="page-top" class="sidebar-toggled">
<div role="main" class="container" style="padding-top:60px;">
<div style="margin-top:20px;">
<h2>BSTable</h2>
<button id="table2-new-row-button" class="btn btn-dark">Yeni Satır</button>
<table class="table table-striped table-bordered" id="table2">
<thead class="thead-dark">
<tr>
<th scope="col">Sıra</th>
<th scope="col">ID</th>
<th scope="col">Ad Soyad</th>
<th scope="col">Email</th>
<th scope="col">Adres</th>
</tr>
</thead>
<tbody id="table2-body">
</tbody>
</table>
</div>
</div>
<script src="bstable.js"></script>
<script>
var lastOrderingValue = 0;
var deletedRowID = 0;
function getUsers(){
$.get('https://jsonplaceholder.typicode.com/users')
.done(resultData => {
console.log("resultData: ", resultData)
for(var i = 0; i < resultData.length; i++) {
lastOrderingValue = i + 1;
var item = '<th scope="row">'+ lastOrderingValue +'</th><td>' + resultData[i].id +'</td><td>' + resultData[i].name + '</td><td>' + resultData[i].email +'</td><td>' + resultData[i].address.city +'</td>'
let row = document.createElement("tr");
row.innerHTML = item;
document.getElementById("table2-body").append(row);
}
bsTable2.init();
})
.fail(errorMessage => console.log("errorMessage", errorMessage));
}
var bsTable2 = new BSTable("table2", {
editableColumns:"1,2",
$addButton: $('#table2-new-row-button'),
onEdit:function(rowElement) {
console.log("EDITED:", rowElement);
var cols = rowElement.childNodes;
var tableRowData = getTableData(cols);
$.post("https://jsonplaceholder.typicode.com/users", tableRowData, function(responseData) {
console.log("responseData: ", responseData);
});
},
onBeforeDelete: function(rowElement){
console.log("onBeforeDelete: ", rowElement);
deletedRowID = parseInt(rowElement[0].childNodes[1].innerText);
console.log("deletedRowID", deletedRowID);
},
onDelete: function() {
console.log("onDelete");
console.log("deletedRowID", deletedRowID);
$.ajax({
type: 'DELETE',
url: 'https://jsonplaceholder.typicode.com/users/' + deletedRowID,
contentType: 'application/json',
dataType: 'json',
success: function(responseData){
console.log('İstek gönderildi. Gelen yanıt: ', responseData);
reorderRows();
},
error: function(errorMessage){ console.log('Hata oluştu: ', errorMessage); } // An error occurred during the request.
});
}, // Called after deletion
onAdd: function() {
console.log("onAdd");
lastOrderingValue++;
var table2 = document.getElementById("table2");
table2.rows[table2.rows.length-1].cells[0].innerText = lastOrderingValue;
}, // Called when added a new row
advanced: {
confirmQuestion: 'Bu satırı silmek istediğinizden emin misiniz?',
columnLabel: ''
}
});
function reorderRows(){
// reorder rows
var table2ths = document.getElementById("table2-body").getElementsByTagName("th");
console.log("table2ths", table2ths);
lastOrderingValue = 0;
for(var i=0; i<table2ths.length; i++)
{
lastOrderingValue++;
table2ths[i].innerText = lastOrderingValue;
//console.log("table2row", table2ths[i].innerText);
}
}
function getTableData(cols){
var data = {};
for(var i = 1; i < cols.length-1; i++){
switch(i){
case 1:
data.id = cols[i].innerText;
break;
case 2:
data.name = cols[i].innerText;
break;
case 3:
data.email = cols[i].innerText;
break;
case 4:
data.address = {};
data.address.city = cols[i].innerText;
break;
default:
break;
}
}
console.log("data", data);
return data;
}
getUsers();
</script>
</body>
</html>