โดย Ik Kat » 29/06/2017 2:03 pm
การสร้างตารางถือเป็นส่วนสำคัญในการสร้างเว็บไซต์ เปรียบเสมือนองค์ประกอบหลักเลยก็ว่าได้ วันนี้เราจะมาพูดถึงพื้นฐานในการสร้างตารางแบบ dynamic ด้วยภาษา
html แบบง่าย ๆ โดยการใช้
jQuery
โค้ด: เลือกทั้งหมด
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Table</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#createRows").click(function(){
var yourname = "val";
var email = "val";
var tel = "val";
var hobby = "val";
var tr = "<tr>";
tr = tr + "<td>" + yourname + "</td>";
tr = tr + "<td>" + email + "</td>";
tr = tr + "<td>" + tel + "</td>";
tr = tr + "<td>" + hobby + "</td>";
tr = tr + "<td>" + tr + "</td>";
tr = tr + "</tr>";
$('#myTable > tbody:last').append(tr);
});
$("#deleteRows").click(function(){
if ($("#myTable tr").length != 1) {
$("#myTable tr:last").remove();
}
});
$("#clearRows").click(function(){
//$("#myTable tbody tr:not(:first-child)").remove(); // remove all
//$("#myBody").empty(); // remove all
$('#myTable > tbody:last').empty(); // remove all
});
});
</script>
<meta charset=utf-8 />
</head>
<body>
<center>
<h2>สร้าง Dynamic Table ให้กับ HTML Table โดยใช้ jQuery</h2>
<br/>
<table width="750" border="1" id="myTable">
<!-- head table -->
<thead>
<tr>
<td width="20%"> <div align="center">yourName </div></td>
<td width="20%"> <div align="center">Email </div></td>
<td width="20%"> <div align="center">Tel </div></td>
<td width="20%"> <div align="center">Hobby </div></td>
</tr>
</thead>
<!-- body dynamic rows -->
<tbody></tbody>
</table>
<br />
<input type="button" id="createRows" value="Add">
<input type="button" id="deleteRows" value="Del">
<input type="button" id="clearRows" value="Clear">
<center>
</body>
</html>
ในส่วนนี้เราได้มีการเพิ่มปุ่ม Add , Del และ Clear
- MT19 29-6-60(11).png (41.6 KiB) Viewed 3426 times
- MT19 29-6-60(12).png (44.61 KiB) Viewed 3426 times
การสร้างตารางถือเป็นส่วนสำคัญในการสร้างเว็บไซต์ เปรียบเสมือนองค์ประกอบหลักเลยก็ว่าได้ วันนี้เราจะมาพูดถึงพื้นฐานในการสร้างตารางแบบ dynamic ด้วยภาษา [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2026-html-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]html[/url] แบบง่าย ๆ โดยการใช้ [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/%E0%B8%AA%E0%B8%AD%E0%B8%99-jquery/2776-%E0%B8%9A%E0%B8%97%E0%B8%97%E0%B8%B5%E0%B9%88-1-jquery-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]jQuery[/url]
[code]<!DOCTYPE html>
<html>
<head>
<title>Dynamic Table</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#createRows").click(function(){
var yourname = "val";
var email = "val";
var tel = "val";
var hobby = "val";
var tr = "<tr>";
tr = tr + "<td>" + yourname + "</td>";
tr = tr + "<td>" + email + "</td>";
tr = tr + "<td>" + tel + "</td>";
tr = tr + "<td>" + hobby + "</td>";
tr = tr + "<td>" + tr + "</td>";
tr = tr + "</tr>";
$('#myTable > tbody:last').append(tr);
});
$("#deleteRows").click(function(){
if ($("#myTable tr").length != 1) {
$("#myTable tr:last").remove();
}
});
$("#clearRows").click(function(){
//$("#myTable tbody tr:not(:first-child)").remove(); // remove all
//$("#myBody").empty(); // remove all
$('#myTable > tbody:last').empty(); // remove all
});
});
</script>
<meta charset=utf-8 />
</head>
<body>
<center>
<h2>สร้าง Dynamic Table ให้กับ HTML Table โดยใช้ jQuery</h2>
<br/>
<table width="750" border="1" id="myTable">
<!-- head table -->
<thead>
<tr>
<td width="20%"> <div align="center">yourName </div></td>
<td width="20%"> <div align="center">Email </div></td>
<td width="20%"> <div align="center">Tel </div></td>
<td width="20%"> <div align="center">Hobby </div></td>
</tr>
</thead>
<!-- body dynamic rows -->
<tbody></tbody>
</table>
<br />
<input type="button" id="createRows" value="Add">
<input type="button" id="deleteRows" value="Del">
<input type="button" id="clearRows" value="Clear">
<center>
</body>
</html>[/code]
ในส่วนนี้เราได้มีการเพิ่มปุ่ม Add , Del และ Clear
[attachment=1]MT19 29-6-60(11).png[/attachment]
[attachment=0]MT19 29-6-60(12).png[/attachment]