CakePHP 3.6:根据下拉控件的选择更新控件值

时间:2018-10-18 13:25:19

标签: php cakephp onchange

我有下表:

00FF0000

与关系:

ServiceTypesTable.php:

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]

CustomerServiceTypesTable.php:

$this->hasMany('CustomerServiceTypes', [
        'foreignKey' => 'service_type_id'
    ]);

$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')); } 下拉值字段中添加特定服务的值:

  

Service_1(建议价格:100)

     

Service_2(建议价格:150)

     

.....

但是我要实现的是,当用户在下拉字段中进行选择时,用建议的价格更新services字段。是否有可能实现该服务器端?不使用javascript?因为我对javascript的了解非常有限。如果不能,您可以根据我的问题提供一个可行的例子吗?

1 个答案:

答案 0 :(得分:1)

进行以下更改:

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'));