ทำอย่างไรค่ะ ให้ code นี้มัน run แบบ realtime โดยไม่ต้องกดปุ่มอ่ะค่ะ

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderators: mindphp, ผู้ดูแลกระดาน

supattra_su
PHP Full Member
PHP Full Member
Posts: 39
Joined: 18/05/2018 10:21 pm

ทำอย่างไรค่ะ ให้ code นี้มัน run แบบ realtime โดยไม่ต้องกดปุ่มอ่ะค่ะ

Post by supattra_su »

ทำอย่างไรค่ะ ให้ code นี้มัน run แบบ realtime โดยไม่ต้องกดปุ่มอ่ะค่ะ

Code: Select all

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<script>
//console.log(myArr)
function SigmalRUpdateData(){
  
//var myjson = '{"tType":"STOCK","aoStock":[{"FTPgpCode":"1000002","FTPgpName":"กลุ่มที่2","FCXdtNet":150.0,"FCXdtQty":2.0}]}';
//var myjson = '{"tType":"STOCK","aoStock":[{"FTPgpCode":"1000003","FTPgpName":"แว่นตา2","FCXdtNet":200.00,"FCXdtQty":1.0}]}';

var myjson = '{"tType":"STOCK","aoStock":[{"FTPgpCode":"1000003","FTPgpName":"แว่นตา2","FCXdtNet":200.00,"FCXdtQty":1.0},{"FTPgpCode":"1000002","FTPgpName":"กลุ่มที่2","FCXdtNet":150.0,"FCXdtQty":2.0}]}';

        // console.log(myjson)
        var myArr = $.parseJSON(myjson);
 
        for(var i = 0; i<myArr['aoStock'].length;i++){
        
            // console.log(myArr['aoStock'][i].FTPgpName+'\r\n');
            tPgpName = myArr['aoStock'][i].FTPgpName;
            tGpgCode = myArr['aoStock'][i].FTPgpCode;
            saleQty = myArr['aoStock'][i].FCXdtQty;
            saleAmout = myArr['aoStock'][i].FCXdtNet;
            

            nCountPgpRow = $('table tr td:contains("'+tPgpName+'")').length
      
            if(nCountPgpRow == 0){
                $('#otbSaleGroup').append($('<tr>')
                                       .append($('<td>')
                                         .text(tPgpName)
                                         .attr('id','otdStkGpg'+tGpgCode)
                                        )
                                       .append($('<td>')
                                          .text(saleQty)
                                          .attr('id','otdStkQty'+tGpgCode) 
                                       )
                                       .append($('<td>')
                                            .text(saleAmout) 
                                            .attr('id','otdStkAmount'+tGpgCode) 
                                       )
                      );
            }else{
                    //Update จำนวน
 
                    nLastQty = parseInt($('#otdStkQty'+tGpgCode).text());
                    nIncommingQty = parseInt(saleQty);
                    nNewQty = nLastQty + nIncommingQty;
                    $('#otdStkQty'+tGpgCode).text(nNewQty);

                    //Update ราคา
 
                    nLastAmt = parseFloat($('#otdStkAmount'+tGpgCode).text());
                    nIncommingAmt = parseFloat(saleAmout);
                    nNewAmt = nLastAmt + nIncommingAmt;
                    $('#otdStkAmount'+tGpgCode).text(nNewAmt);
            }
        }
    }
</script>

<button onclick="SigmalRUpdateData()">SignalR</button>


<table class="table table-bordered" id="otbSaleGroup">
    <thead>
      <tr>
        <th>Group</th>
        <th>Qty</th>
        <th>Amount</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td id="otdStkGpg1000001">แว่นตา</td>
        <td id="otdStkQty1000001">1</td>
        <td id="otdStkAmount1000001">120</td>
      </tr>
    </tbody>
  </table>
User avatar
tsukasaz
PHP VIP Members
PHP VIP Members
Posts: 13128
Joined: 18/04/2012 9:39 am

Re: ทำอย่างไรค่ะ ให้ code นี้มัน run แบบ realtime โดยไม่ต้องกดปุ่มอ่ะค่ะ

Post by tsukasaz »

ลองแบบนี้ครับ

Code: Select all

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<script>
//console.log(myArr);

jQuery(function($) {
  
//var myjson = '{"tType":"STOCK","aoStock":[{"FTPgpCode":"1000002","FTPgpName":"กลุ่มที่2","FCXdtNet":150.0,"FCXdtQty":2.0}]}';
//var myjson = '{"tType":"STOCK","aoStock":[{"FTPgpCode":"1000003","FTPgpName":"แว่นตา2","FCXdtNet":200.00,"FCXdtQty":1.0}]}';

var myjson = '{"tType":"STOCK","aoStock":[{"FTPgpCode":"1000003","FTPgpName":"แว่นตา2","FCXdtNet":200.00,"FCXdtQty":1.0},{"FTPgpCode":"1000002","FTPgpName":"กลุ่มที่2","FCXdtNet":150.0,"FCXdtQty":2.0}]}';

        // console.log(myjson)
        var myArr = $.parseJSON(myjson);
 
        for(var i = 0; i<myArr['aoStock'].length;i++){
        
            // console.log(myArr['aoStock'][i].FTPgpName+'\r\n');
            tPgpName = myArr['aoStock'][i].FTPgpName;
            tGpgCode = myArr['aoStock'][i].FTPgpCode;
            saleQty = myArr['aoStock'][i].FCXdtQty;
            saleAmout = myArr['aoStock'][i].FCXdtNet;
            

            nCountPgpRow = $('table tr td:contains("'+tPgpName+'")').length
      
            if(nCountPgpRow == 0){
                $('#otbSaleGroup').append($('<tr>')
                                       .append($('<td>')
                                         .text(tPgpName)
                                         .attr('id','otdStkGpg'+tGpgCode)
                                        )
                                       .append($('<td>')
                                          .text(saleQty)
                                          .attr('id','otdStkQty'+tGpgCode) 
                                       )
                                       .append($('<td>')
                                            .text(saleAmout) 
                                            .attr('id','otdStkAmount'+tGpgCode) 
                                       )
                      );
            }else{
                    //Update จำนวน
 
                    nLastQty = parseInt($('#otdStkQty'+tGpgCode).text());
                    nIncommingQty = parseInt(saleQty);
                    nNewQty = nLastQty + nIncommingQty;
                    $('#otdStkQty'+tGpgCode).text(nNewQty);

                    //Update ราคา
 
                    nLastAmt = parseFloat($('#otdStkAmount'+tGpgCode).text());
                    nIncommingAmt = parseFloat(saleAmout);
                    nNewAmt = nLastAmt + nIncommingAmt;
                    $('#otdStkAmount'+tGpgCode).text(nNewAmt);
            }
        }
    });
</script>



<table class="table table-bordered" id="otbSaleGroup">
    <thead>
      <tr>
        <th>Group</th>
        <th>Qty</th>
        <th>Amount</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td id="otdStkGpg1000001">แว่นตา</td>
        <td id="otdStkQty1000001">1</td>
        <td id="otdStkAmount1000001">120</td>
      </tr>
    </tbody>
  </table>
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Programming - PHP”

Who is online

Users browsing this forum: No registered users and 17 guests