 |
ช่วยทีค่ะ เรื่อง Drag and Drop Table เนื่องจากหนูลองหาข้อมูล เกี่ยวกับเรื่อง Drag and Drop table |
|
 |
|
|
 |
 |
|
ตัวอย่างก็ประมาณนี้แหล่ะ น้องอาจจะลืมโหลดไฟล์ js บางตัวหรือเปล่า หรือไม่ได้ใส่ แท็ก div ที่เค้าเขียนไว้โชว์ผลลัพธ์
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.tablednd_0_5.js"></script>
<h3>Ex2.</h3><br />
<div id="debug" style="float:right;"></div>
<table id="table-1" style="border:solid #CCC 1px;" cellpadding="2">
<tr id="1"> <td>1</td> <td>One</td> <td>some text</td> </tr>
<tr id="2"> <td>2</td> <td>Two</td> <td>some text</td> </tr>
<tr id="3"> <td>3</td> <td>Three</td> <td>some text</td> </tr>
<tr id="4"> <td>4</td> <td>Four</td> <td>some text</td> </tr>
</table>
<br />
<h3>Ex2.</h3><br />
<div id="debugArea"></div>
<table id="table-2" style="border:solid #CCC 1px;" cellpadding="2">
<tr id="1"> <td>1</td> <td>One</td> <td>some text</td> </tr>
<tr id="2"> <td>2</td> <td>Two</td> <td>some text</td> </tr>
<tr id="3"> <td>3</td> <td>Three</td> <td>some text</td> </tr>
<tr id="4"> <td>4</td> <td>Four</td> <td>some text</td> </tr>
</table>
<script type="text/javascript">
$(document).ready(function() {
// Initialise the first table (as before)
$("#table-1").tableDnD();
// Make a nice striped effect on the table
$("#table-2 tr:even').addClass('alt')");
// Initialise the second table specifying a dragClass and an onDrop function that will display an alert
$("#table-2").tableDnD({
onDragClass: "myDragClass",
onDrop: function(table, row) {
var rows = table.tBodies[0].rows;
var debugStr = "Row dropped was "+row.id+". New order: ";
for (var i=0; i<rows.length; i++) {
debugStr += rows[i].id+" ";
}
$("#debugArea").html(debugStr);
},
onDragStart: function(table, row) {
$("#debugArea").html("Started dragging row "+row.id);
}
});
});
</script>
|
 |
 |
 |
 |
Date :
2011-02-09 14:51:14 |
By :
chineji |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หรือลองดูที่ jQuery UI ก็ได้นะจะมี drag & drop อยู่ มีตัวอย่างการอัพเดทฐานข้อมูลด้วย คือตอนนี้เราะลากไปแล้วก็จะอัพเดทตำแหน่งดังกล่าวลงฐานข้อมูลให้
ลองดูในกูเกิลนะ jQuery UI ดูที่ demo
|
 |
 |
 |
 |
Date :
2011-02-09 14:56:29 |
By :
chineji |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โอ้ หนูพลาดเองค่ะ ตอนแรกหนูเอาส่วนของ java script ไว้ด้านบนสุด
แต่เอ เรื่อง Update database ในส่วนของ UI ที่พี่บอกอ่ะค่ะ หนูลอง ดูในส่วนของ Demo แล้ว ไม่ทราบว่าเรื่อง ของ Database อยู่ในส่วนไหนเหรอคะ
ขอบคุณมากจริงๆค่ะ คิดไม่ออกมาตั้งหลายวัน ^^
|
 |
 |
 |
 |
Date :
2011-02-09 15:49:35 |
By :
naked13 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่คะ ช่วยต่อ Code ให้หน่อยค่ะ
คือ เนื่องจากหนูต้องการ ส่งค่าไปบันทึกใน database เมื่อเราเปลี่ยนตำแหน่งแล้วอ่ะค่ะ
หนูรู้แค่ว่า ต้องใส่ ไว้ใน functon onDrop อ่ะค่ะ
รบกวนช่วยหน่อยนะคะ
|
 |
 |
 |
 |
Date :
2011-02-09 17:42:30 |
By :
naked13 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนด้วยค่ะ หรือช่วยบอกแนวทางหน่อยนะคะ
|
 |
 |
 |
 |
Date :
2011-02-09 20:42:17 |
By :
naked13 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีใครยังไม่นอนบ้างคะ ช่วยตอบหนูทีนะคะ
|
ประวัติการแก้ไข 2011-02-10 11:38:51
 |
 |
 |
 |
Date :
2011-02-09 23:56:56 |
By :
naked13 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้เข้ามาดูต่อเลยเมื่อวานไปข้างนอกกลับมาซะดึก ขอโทษทีแล้วกัน
ก็ลองเอานี่ไปดูแล้วกันนะตัวอย่าง
http://www.webresourcesdepot.com/dynamic-dragn-drop-with-jquery-and-php
ซึ่งมันก็หลักการคล้ายๆกันกับโค๊ดชุดที่แล้วนั่นแหล่ะครับ ที่ผมเขียน demo ให้ดู มันจะส่งค่าลำดับมาให้ เราก็แค่เอาลำดับเหล่านั้นเป็นบันทึกลงฐานข้อมูลโดยน้องจะต้องเขียนเป็น ajax ส่งค่านั้นเพื่อไปอัพเดทฐานข้อมูล
ตัวอย่างการใช้งานก็เช่น การใช้จัดลำดับรูปภาพ จะเอารูปไหนแสดงก่อนหลัง หรือการจัดลำดับสมาชิกว่าอันไหนมีความสำคัญมากก็ลากมาให้อยู่ข้างบนสุดเป็นต้น ซึ่งอันนี้ก็ขึ้นอยู่กับว่าจะเอาไปประยุกต์ทำอะไร
|
 |
 |
 |
 |
Date :
2011-02-10 13:58:29 |
By :
chineji |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากค่ะพี่ เมื่อวานนึกว่าพี่จะเข้าเว็บมาอีก เลยรอทั้งคืนเลย
งั้นหนูลองไปทำก่อนนะคะ ถ้ามีอะไรติดขัดอีก เดี๋ยวจะมาถามต่อนะคะ
|
 |
 |
 |
 |
Date :
2011-02-10 14:17:13 |
By :
naked13 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Ex
เพิ่มให้อีกแล้วกันนะสำหรับแก้ของเก่าให้อัพเดทลงฐานข้อมูลได้
Code (PHP)
table member {int id,int no,string name}
select name from member order by no asc
1 1 ไมเคิล
2 2 แม็ก
3 3 หมวย
4 4 หมี
<script type="text/javascript">
$(document).ready(function() {
$("#table-2").tableDnD({
onDragClass: "myDragClass",
onDrop: function(table, row) {
var rows = table.tBodies[0].rows;
var debugStr = "Row dropped was "+row.id+". New order: ";
for (var i=0; i<rows.length; i++) {
debugStr += rows[i].id+" ";
}
// $("#debugArea").html(debugStr);
//$("#debugArea").html($.tableDnD.serialize());
var order = $.tableDnD.serialize() + '&action=sortTable';
$.post("updateDB.php", order, function(theResponse){
$("#debugArea").html(theResponse);
});
},
onDragStart: function(table, row) {
$("#debugArea").html("Started dragging row "+row.id);
}
});
});
</script>
Code (updateDB.php)
#require("db.php");
$action = mysql_real_escape_string($_POST['action']);
$updateRecordsArray = $_POST['table-2'];
if ($action == "sortTable"){
$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue) {
$query = "UPDATE table_test SET no = " . $listingCounter . " WHERE id = " . $recordIDValue;
//mysql_query($query) or die('Error,update query failed');
echo $listingCounter.": ".$query."<br>";
$listingCounter = $listingCounter + 1;
}
echo '<pre>';
print_r($updateRecordsArray);
echo '</pre>';
echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
ส่วนสำหรับแบบใหม่ก็ดุตัวอย่างตามลิงค์ดังกล่าวนะที่บอกไว้ก่อนหน้านะ
|
 |
 |
 |
 |
Date :
2011-02-10 14:42:25 |
By :
chineji |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อยากจะขอบคุณซัก 1,000,000 ครั้ง แต่ ก็คงไม่พอ แต่ยังไงก็ขอบคุณมากค่ะ
|
 |
 |
 |
 |
Date :
2011-02-10 15:07:32 |
By :
naked13 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|