2016-09-15 5 views
1

私は基本的な請求システムを持っています。このシステムでは、基本価格を変更すると、フォーム内のctnなどの単位を記入する項目(jquery select 2)を選択できます。請求書を保存する前に、この価格を変更することができます。jQueryがプログラムで完了したときに変更が発生しないようにする

この(保存された)請求書を(同じページとフォームで)編集する必要がある場合は、PHP GETでIDを取得してから、アイテムとその数量を取得するためにajaxで保存された請求書をフェッチします、価格などの詳細はフォームに記入する必要があります。

問題は、(ajaxの後で)選択ボックス(jquery select 2)内の項目をプログラムで選択すると、(前述のように)変更機能がトリガされ、代わりに基本価格などが取得されます。私が見てそれを変更するために計画的に記入したいものの

//Fetch Items 
fetchItems() 

function fetchItems() { 
    $.ajax({ 
    url: 'https://api.myjson.com/bins/4z7se', 
    dataType: "json", 
    success: function(items) { 
     // Create list of items in select tag 
     var toAppend = ''; 
     $.each(items, function(i, o) { 
     toAppend += '<option value="' + o.itemId + '">' + o.newName + '</option>'; 
     }); 

     $('#item').append(toAppend); 
     $('#item').select2({ 
     placeholder: "Select an item", 
     escapeMarkup: function(markup) { 
      return markup; 
     } 
     }) 
    }, 
    error: function(xhr, ajaxOptions, thrownError) { 
     alert("ERROR:" + xhr.responseText + " - " + thrownError); 
    } 
    }); 
} 



// On item select, get item details and fill in the table 
// --->>> Select first item for this demo to get the details filled (usually it requests the values from mysql for the selected item) 
$('#item').on('change', function() { 
    $.ajax({ 
    url: 'https://api.myjson.com/bins/vjfi', 
    dataType: "json", 
    success: function(data) { 
     // Print item details in respective elements 
     $('#itemId').val(data[0]['itemId']) 
     $('#size').text(data[0]['size']) 
     $('#units_per_ctn').text(data[0]['units_per_ctn']) 
     $('#ctn_price').val(data[0]['ctn_price']) 
     $('#qty').val('') // Clear qty from previous 
     $('#total_price').text('') // Clear total price from previous 
    }, 
    error: function(xhr, ajaxOptions, thrownError) { 
     alert("ERROR:" + xhr.responseText + " - " + thrownError); 
    } 
    }); 
}) 


// For update purpose, details to be filled in the form 
$('#edit').click(function() { 
    //var editId = '<?PHP if(isset($_GET['edit '])) { echo $_GET['edit'];}?>'; 
    var editId = 1 // Lets assume it for this demo 
    if (editId > 0) { 
    // Get transfer details for this id 
    $.ajax({ 
     url: 'https://api.myjson.com/bins/1iwha', 
     dataType: 'json', 
     success: function(data) { 
     $('table #item').select2().val(data[0]['item_id']).trigger('change') 

     // Here is the proble, can't put these values in the input because as soon as select2 change is triggered it fills its own values to the inputs. But I want the following values (returned from mysql in json) to be filled in. 
     $('table #ctn_price').val(data[0]['ctn_price']) 
     $('table #qty').val(data[0]['qty']) 
     $('table #totap_price').val(data[0]['total_price']) 
     }, 
     error: function(xhr, ajaxOptions, thrownError) { 
     alert("ERROR:" + xhr.responseText + " - " + thrownError); 
     } 
    }) 
    } 
}) 

Here is the Fiddle

+1

なぜ、 '.trigger( '変更')を'削除しないで! – eisbehr

+0

新しい請求書を作成するときに必要です – sohal07

+0

これは請求書を追加および編集するための同じページです – sohal07

答えて

1

あなたはtriggerHandlerを使用することができますし、あなたのevent listener$.ajaxオブジェクトを返します。その後、.done()を連鎖させて、ajaxリクエストが完了した後で作業を進めることができます。

// return ajax object 
$('#item').on('change', function() { 
    return $.ajax({ 
     /* ... */ 
    }); 
}); 

// chain actions 
$('#item').select2().val(val).triggerHandler('change').done(function() { 
    // set your values 
}); 

Working example

//Fetch Items 
 
fetchItems() 
 

 
function fetchItems() { 
 
    $.ajax({ 
 
    url: 'https://api.myjson.com/bins/4z7se', 
 
    dataType: "json", 
 
    success: function(items) { 
 
     // Create list of items in select tag 
 
     var toAppend = ''; 
 
     $.each(items, function(i, o) { 
 
     toAppend += '<option value="' + o.itemId + '">' + o.newName + '</option>'; 
 
     }); 
 

 
     $('#item').append(toAppend); 
 
     $('#item').select2({ 
 
     placeholder: "Select an item", 
 
     escapeMarkup: function(markup) { 
 
      return markup; 
 
     } 
 
     }) 
 
    }, 
 
    error: function(xhr, ajaxOptions, thrownError) { 
 
     alert("ERROR:" + xhr.responseText + " - " + thrownError); 
 
    } 
 
    }); 
 
} 
 

 

 

 
// On item select, get item details and fill in the table 
 
// --->>> Select first item for this demo to get the details filled (usually it requests the values from mysql for the selected item) 
 
$('#item').on('change', function() { 
 
return $.ajax({ 
 
    url: 'https://api.myjson.com/bins/vjfi', 
 
    dataType: "json", 
 
    success: function(data) { 
 
     // Print item details in respective elements 
 
     $('#itemId').val(data[0]['itemId']) 
 
     $('#size').text(data[0]['size']) 
 
     $('#units_per_ctn').text(data[0]['units_per_ctn']) 
 
     $('#ctn_price').val(data[0]['ctn_price']) 
 
     $('#qty').val('') // Clear qty from previous 
 
     $('#total_price').text('') // Clear total price from previous 
 
    }, 
 
    error: function(xhr, ajaxOptions, thrownError) { 
 
     alert("ERROR:" + xhr.responseText + " - " + thrownError); 
 
    } 
 
    }); 
 
}); 
 

 
// For update purpose, details to be filled in the form 
 
$('#edit').click(function() { 
 
    //var editId = '<?PHP if(isset($_GET['edit '])) { echo $_GET['edit'];}?>'; 
 
    var editId = 1 // Lets assume it for this demo 
 
    if (editId > 0) { 
 
    // Get transfer details for this id 
 
    $.ajax({ 
 
     url: 'https://api.myjson.com/bins/1iwha', 
 
     dataType: 'json', 
 
     success: function(data) { 
 
     $('table #item').select2().val(data[0]['item_id']).triggerHandler("change").done(function() { 
 
      $('table #ctn_price').val(data[0]['ctn_price']) 
 
      $('table #qty').val(data[0]['qty']) 
 
      $('table #totap_price').val(data[0]['total_price']) 
 
     }); 
 
     }, 
 
     error: function(xhr, ajaxOptions, thrownError) { 
 
     alert("ERROR:" + xhr.responseText + " - " + thrownError); 
 
     } 
 
    }) 
 
    } 
 
})
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script> 
 
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js"></script> 
 
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css"> 
 
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css"> 
 
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script> 
 

 
<table class="table table-bordered jambo_table"> 
 
    <thead> 
 
    <th>Size</th> 
 
    <th>Item Name</th> 
 
    <th>Units/CTN</th> 
 
    <th>CTN Price 
 
     <br><small>(inc GST)</small></th> 
 
    <th>Qty</th> 
 
    <th>Total Price</th> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td id="size"></td> 
 
     <td> 
 
     <select class="form-control" id="item" name="item"> 
 
      <option disabled selected>Select an item</option> 
 
     </select> 
 
     </td> 
 
     <td id="units_per_ctn"></td> 
 
     <td> 
 
     <input type="text" class="form-control" id="ctn_price" name="ctn_price" size="10"> 
 
     </td> 
 
     <td> 
 
     <input type="text" class="form-control" id="qty" name="qty" size="10"> 
 
     </td> 
 
     <td id="total_price"></td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 

 
<div> 
 
    Normally item id is received with php get, and the details are fetched with ajax to be filled in this form, (using button for the demo only)<br> 
 
    <button id="edit">Edit Record 1</button> 
 
</div>

+0

がありません。私はローカル環境でこのコードを使用してこのエラーが発生しています: 'stock-transfer.php?edit = 1:645 Uncaught TypeError:プロパティの読み取りができません 'done' of undefined' – sohal07

+0

'triggerHandler'または' return $ .ajax({... '}をイベントリスナーの中に書くのを忘れてしまった@ sohal07 – eisbehr

+0

はい、ありがとうございました。ありがとうございました – sohal07

関連する問題