CakePHP 3.6:根据下拉控件的选择更新控件值
CakePHP 3.6: Update control value based on the selection of a dropdown control
我有以下表格:
customers[id, name, surname, phone, text, balance, created]
service_types[id, title, price, length, is_subscription, created, payment]
customer_service_types[id, customer_id, service_type_id, price, created]
与关系:
ServiceTypesTable.php:
$this->hasMany('CustomerServiceTypes', [
'foreignKey' => 'service_type_id'
]);
客户ServiceTypesTable.php:
$this->belongsTo('Customers', [
'foreignKey' => 'customer_id',
'joinType' => 'INNER'
]);
$this->belongsTo('ServiceTypes', [
'foreignKey' => 'service_type_id',
'joinType' => 'INNER'
]);
在 CustomerServiceTypes\add.ctp
中,我有一个带有 services
的下拉菜单和一个价格字段:
echo $this->Form->control('customer_id', ['options' => $customers,'label' => 'Customer']);
echo $this->Form->control('service_type_id', ['options' => $serviceTypes, 'label' => 'Service']);
echo $this->Form->control('price', ['label' => 'Price']);
在CustomerServiceTypesController.php
中:
public function add($customerid = null)
{
$customerServiceType = $this->CustomerServiceTypes->newEntity();
if ($this->request->is('post')) {
$customerServiceType = $this->CustomerServiceTypes->patchEntity($customerServiceType, $this->request->getData());
if ($this->CustomerServiceTypes->save($customerServiceType)) {
//debug($this->request->getData("customer_id"),true);
$this->Flash->success(__('Success'));
return $this->redirect(['controller' => 'customers', 'action' => 'edit', $customerid]);
}
$this->Flash->error(__('Fail'));
}
$customers = $this->CustomerServiceTypes->Customers->find('list', ['limit' => 200])->where(['Customers.id =' => $customerid]);
$serviceTypes = $this->CustomerServiceTypes->ServiceTypes->find('list', [
'valueField' => function ($row) {
return $row['title'] . ' (Suggested price: ' . $row['price'] . ')';
}
], ['limit' => 200]);
$this->set(compact('customerServiceType', 'customers', 'serviceTypes'));
}
在 services
下拉值字段中添加特定服务的值:
Service_1 (Suggested price: 100)
Service_2 (Suggested price: 150)
.....
但我想要实现的是当用户在下拉字段中进行选择时,用建议价格更新 price
字段。是否有可能实现该服务器端?不使用javascript?因为我对javascript的了解非常有限。如果不能,您能否根据我的问题提供一个工作示例?
进行以下更改:
add.ctp
<div ng-app="" ng-init='servicePrices = <?php echo json_encode($servicePrices); ?>;' >
<?php
echo $this->Form->create();
echo $this->Form->control('customer_id', ['options' => $customers,'label' => 'Customer']);
echo $this->Form->control('service_type_id', [
'options' => $serviceTypes, 'label' => 'Service',
'ng-model'=>'service_type_id'
]);
echo $this->Form->control('price', [
'label' => 'Price',
'ng-model'=>'servicePrices[service_type_id]'
]);
echo $this->Form->submit('submit');
?>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js" ></script>
CustomerServiceTypesController.php
// add this
$servicePrices = $this->CustomerServiceTypes
->ServiceTypes
->find()
->limit(200)
->combine('id','price');
$this->set(compact('customerServiceType', 'customers', 'serviceTypes','servicePrices'));
我有以下表格:
customers[id, name, surname, phone, text, balance, created]
service_types[id, title, price, length, is_subscription, created, payment]
customer_service_types[id, customer_id, service_type_id, price, created]
与关系:
ServiceTypesTable.php:
$this->hasMany('CustomerServiceTypes', [
'foreignKey' => 'service_type_id'
]);
客户ServiceTypesTable.php:
$this->belongsTo('Customers', [
'foreignKey' => 'customer_id',
'joinType' => 'INNER'
]);
$this->belongsTo('ServiceTypes', [
'foreignKey' => 'service_type_id',
'joinType' => 'INNER'
]);
在 CustomerServiceTypes\add.ctp
中,我有一个带有 services
的下拉菜单和一个价格字段:
echo $this->Form->control('customer_id', ['options' => $customers,'label' => 'Customer']);
echo $this->Form->control('service_type_id', ['options' => $serviceTypes, 'label' => 'Service']);
echo $this->Form->control('price', ['label' => 'Price']);
在CustomerServiceTypesController.php
中:
public function add($customerid = null)
{
$customerServiceType = $this->CustomerServiceTypes->newEntity();
if ($this->request->is('post')) {
$customerServiceType = $this->CustomerServiceTypes->patchEntity($customerServiceType, $this->request->getData());
if ($this->CustomerServiceTypes->save($customerServiceType)) {
//debug($this->request->getData("customer_id"),true);
$this->Flash->success(__('Success'));
return $this->redirect(['controller' => 'customers', 'action' => 'edit', $customerid]);
}
$this->Flash->error(__('Fail'));
}
$customers = $this->CustomerServiceTypes->Customers->find('list', ['limit' => 200])->where(['Customers.id =' => $customerid]);
$serviceTypes = $this->CustomerServiceTypes->ServiceTypes->find('list', [
'valueField' => function ($row) {
return $row['title'] . ' (Suggested price: ' . $row['price'] . ')';
}
], ['limit' => 200]);
$this->set(compact('customerServiceType', 'customers', 'serviceTypes'));
}
在 services
下拉值字段中添加特定服务的值:
Service_1 (Suggested price: 100)
Service_2 (Suggested price: 150)
.....
但我想要实现的是当用户在下拉字段中进行选择时,用建议价格更新 price
字段。是否有可能实现该服务器端?不使用javascript?因为我对javascript的了解非常有限。如果不能,您能否根据我的问题提供一个工作示例?
进行以下更改:
add.ctp
<div ng-app="" ng-init='servicePrices = <?php echo json_encode($servicePrices); ?>;' >
<?php
echo $this->Form->create();
echo $this->Form->control('customer_id', ['options' => $customers,'label' => 'Customer']);
echo $this->Form->control('service_type_id', [
'options' => $serviceTypes, 'label' => 'Service',
'ng-model'=>'service_type_id'
]);
echo $this->Form->control('price', [
'label' => 'Price',
'ng-model'=>'servicePrices[service_type_id]'
]);
echo $this->Form->submit('submit');
?>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js" ></script>
CustomerServiceTypesController.php
// add this
$servicePrices = $this->CustomerServiceTypes
->ServiceTypes
->find()
->limit(200)
->combine('id','price');
$this->set(compact('customerServiceType', 'customers', 'serviceTypes','servicePrices'));