Skip to content

Instantly share code, notes, and snippets.

@wisaruthk
Last active October 23, 2018 07:40
Show Gist options
  • Save wisaruthk/6608014eec52dcfac3471f5fd047e780 to your computer and use it in GitHub Desktop.
Save wisaruthk/6608014eec52dcfac3471f5fd047e780 to your computer and use it in GitHub Desktop.
การใช้ <script> ใน yii2 view
/**
* ปกติการ ใช้ registerJs("") ของ yii2 จะทำให้การเขียน script ค่อนข้างยาก
* ดังนั้นจึงเขียน javascript ในลักษณะนี้จะจัดการได้ง่ายกว่า ไม่ต้องคอยกังวลกับปัญหา "", ''
*/
<script type="text/javascript">
var obj = {
init:function(){
$('#po-product_uom_qty').on('change',function(e){
obj.calculateAmount();
});
$('#po-price_unit').on('change',function(e){
obj.calculateAmount();
});
},
calculateAmount:function(){
var qty = $("#po-product_uom_qty").val();
var price_unit = $("#po-price_unit").val();
var amount = qty*price_unit;
$("#po-amount").val(amount);
}
}
</script>
<?php
$this->registerJs("
$(document).ready(function(){
obj.init();
});
",View::POS_READY);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment