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

Post a reply

Smilies
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

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

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

by tsukasaz » 27/08/2018 5:09 pm

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

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>

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

by supattra_su » 27/08/2018 3:48 pm

ทำอย่างไรค่ะ ให้ 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>

Top