|
 |
|
ปัญหาเรื่องการคำนวณใน Auto fill ครับ ผมติดเรื่องการนำตัวแปลตารางมาใช้ในการคำนวณครับ T^T ช่วยบอกหน่อยครับไปต่อไม่ได้ |
|
 |
|
|
 |
 |
|
ปัญหาเรื่องการคำนวณใน Auto fill ครับ ผมติดเรื่องการนำตัวแปลตารางมาใช้ในการคำนวณครับ T^T ช่วยบอกหน่อยครับไปต่อไม่ได้

จากตารางจะเห็นว่าช่องที่มีข้อมูลคือช่องที่ดึงข้อมูลมาจากฐานข้อมูล
ส่วนช่อง จำนวนที่ใช้ จะต้องกรอกเอง เพื่อใช้ในการคำนวณ โดยคำนวนจาก ค่าอะไหล่ (จากฐานข้อมูล) * จำนวนที่ใช้(กรอกข้อมูล)
และแสดงผลรวมในช่อง ราคารวมโดยใช้ปุ่ม Sum ในการคำนวณ
มีการวลลูปของตาราง ซึ่งผมเขียนแบบนี้
Code (JavaScript)
<script language="javascript">
function fncSum()
{
document.frmMain.txtTotle<?=$i;?>.value = parseFloat(document.frmMain.txtUse<?=$i;?>.value) + parseFloat(document.frmMain.txtPrice<?=$i;?>.value);
}
</script>
ซึ่งไม่แสดงผลการคูณออกมาในช่อง ผลรวม
และผมต้องการต่อยอดการเขียนโดยมีการคำนวนค่าผลรวมทั้งหมดที่ได้จากตารางมารวม กับ ค่าช่าง และส่วนลด เพื่อแสดงผลในซ่องผลรวมอีกครั้ง
โค้ดผมมีดังนี้ครับ
Code Auto fill ในส่วนของ Java
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[name*="txtSpareID"]').change("change", function (){
var currentIndex = $(this).closest("tr")[0].rowIndex;
$.ajax({
url: "returnCustomer3.php" ,
type: "POST",
data: 'sCusID=' +$("#txtSpareID"+currentIndex).val()
})
.success(function(result) {
var obj = jQuery.parseJSON(result);
if(obj == '')
{
$("#txtSpareID"+currentIndex).val('');
$("#txtName"+currentIndex).val('');
$("#txtPrice"+currentIndex).val('');
$("#txtQuantity"+currentIndex).val('');
}
else
{
$.each(obj, function(key, inval) {
$("#txtSpareID"+currentIndex).val(inval["s_id"]);
$("#txtName"+currentIndex).val(inval["s_name"]);
$("#txtPrice"+currentIndex).val(inval["s_price"]);
$("#txtQuantity"+currentIndex)<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[name*="txtSpareID"]').change("change", function (){
var currentIndex = $(this).closest("tr")[0].rowIndex;
$.ajax({
url: "returnCustomer3.php" ,
type: "POST",
data: 'sCusID=' +$("#txtSpareID"+currentIndex).val()
})
.success(function(result) {
var obj = jQuery.parseJSON(result);
if(obj == '')
{
$("#txtSpareID"+currentIndex).val('');
$("#txtName"+currentIndex).val('');
$("#txtPrice"+currentIndex).val('');
$("#txtQuantity"+currentIndex).val('');
}
else
{
$.each(obj, function(key, inval) {
$("#txtSpareID"+currentIndex).val(inval["s_id"]);
$("#txtName"+currentIndex).val(inval["s_name"]);
$("#txtPrice"+currentIndex).val(inval["s_price"]);
$("#txtQuantity"+currentIndex).val(inval["quantity"]);
});
}
});
});
});</script>
ผมลงเขียนให้คำนวณในส่วนของ .val(inval[ โดยให้มีการคำนวณใน .val(inval[]*inval[]) ซึ้งมีการคำณรวนจริงและแสดงผล
แต่ เป็นการคำนวณตัวเลขตัวฐานข้อมูลเท่านั้น และแสดงผลอัตโนมัต ซึ่งผมต้องการให้คำนวณจาก ตัวเลข จากTxtboxอื่นๆ
ตัวตารางที่เป็น Auto fill
<table width="963" border="1">
<tr>
<th width="142"> <div align="center">รหัสอะไหล่</div></th>
<th width="422"> <div align="center">ชื่ออะไหล่ </div></th>
<th width="82"> <div align="center">ราคาอะไหล่ : ชิ้น </div></th>
<th width="89">จำนวนคงคลัง</th>
<th width="1">จำนวนที่ใช้</th>
<th width="70">คงเหลือ</th>
<th width="111"> <div align="center">ราคารวม</div></th>
</tr>
<?
for($i=1;$i<=10;$i++)
{
?>
<tr>
<td><div align="center"><input type="text" name="txtSpareID<?=$i;?>" id="txtSpareID<?=$i;?>" size="20"></div></td>
<td><input type="text" name="txtName<?=$i;?>" id="txtName<?=$i;?>" size="60"></td>
<td><div align="center"><input type="text" name="txtPrice<?=$i;?>" id="txtPrice<?=$i;?>" size="5"></div></td>
<td><div align="center"><input name="txtQuantity<?=$i;?>" type="text" id="txtQuantity<?=$i;?>" size="3"></div></td>
<td><div align="center">
<input type="text" name="txtUse<?=$i;?>" id="txtUse<?=$i;?>" size="3">
</div></td>
<td><div align="center">
<input type="text" name="txtBalance<?=$i;?>" id="txtBalance<?=$i;?>" size="3">
</div></td>
<td><div align="center">
<input type="text" name="txtTotle<?=$i;?>" id="txtTotle<?=$i;?>" size="5">
</div></td>
</tr>
<?
}
?>
</table>
รบกวนหน่อยครับ ผมติดอยู่โค้ดนี้จะครับสัปดาห์แล้วครับ T^T งานอื่นเลยไม่คืบหน้าไปด้วย
Tag : PHP, jQuery

|
|
 |
 |
 |
 |
Date :
2013-10-29 11:58:49 |
By :
slipknotbel |
View :
1516 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code หลายส่วนเหลือเกินครับ อ่านแล้วจับจุดยังไม่เจอครับ ลองสรุปเอาแบบสั้น ๆ ดูครับ
|
 |
 |
 |
 |
Date :
2013-10-31 08:38:01 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ได้แล้วครับ เดี่ยว ขอแก้ ไขอีกหน่อย แล้ว จะมาโพสให้ครับ
|
ประวัติการแก้ไข 2013-10-31 12:00:56
 |
 |
 |
 |
Date :
2013-10-31 11:31:49 |
By :
slipknotbel |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้ว ป๋ม จะลงฐานข้อมูลยังไงฮับ อย่างอื่นผ่านหมดแล้ว
-แนวคิดเบื้องต้น
แนวคิดที่ 1 : ทำ Hidden เก็บค่า id ที่ไม่ใช่รหัส อะไหล่ แล้วดึงไปฐานข้อมูลเฉพาะ id
ใน db จะตั้งชื่อตาราง คล้ายๆกันโดยมีเลขกำกับด้านหลัง เพื่อทำการเก็บค่าจาก Hidden เช่น SID1 SID2 SID...
แนวคิดที่ 2 : ทำ Hidden เก็บ รหัสอะไหล่ และสร้าง Dbเหมือนแนวคิด 1
แนะนำเพิ่มเติมด้วยนะครับ
|
 |
 |
 |
 |
Date :
2013-11-05 17:03:15 |
By :
slipknotbel |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|