一、表单调取数据
<div>
<?php
$js = <<<JS
$(document).on('change',function(){ //js监听是否数据发生改变
var number = $("#w0 option:selected").text();//监听select改变,获取下拉选项值
var url =['index'];
$.ajax({
async:false,
type : 'post',
cache:false,
url:url,
data:{id:number}, //传值到控制器,获取相应数据
success : function(data){
var obj = JSON.parse(data); //解析从控制器传来的数据(此时是数组)
$("#contractdetail-annualbudgetid").val(obj[0]['annualBudgetID']);//解析后的数据相应值放到对应ID的文本框中
$("#contractdetail-purchasecatalogueid").val(obj[0]['purchaseCatalogueID']);
$("#contractdetail-description").val(obj[0]['description']);
$("#contractdetail-specification").val(obj[0]['specification']);
$("#contractdetail-quantity").val(obj[0]['quantity']);
$("#contractdetail-unit").val(obj[0]['unit']);
$("#contractdetail-price").val(obj[0]['price']);
$("#contractdetail-amount").val(obj[0]['amount']);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
},
});
});
JS;
$this->registerJs($js);
?>
</div>
二、控制器响应返回值
if (Yii::$app->request->isPost) {
$request = Yii::$app->request;
$purchaseDetailID= $request->post('id');
$purchaseData = PurchaseDetail::find()
->where([
'purchaseDetailID' => $purchaseDetailID
])
->asArray()
->all();
$data = json_encode($purchaseData,true);
return $data;
}