使用到的方法: 弹窗 ajax跨域请求 各种风格的重定向 构造url
需求:生成一个消毒订单,在该订单内增加订单明细。 就好像点外卖,一个订单包含了很多菜品。
效果:
关键代码:
订单模型
class DisinfectionOrder extends BaseModel {
public $check_save=1;
public function tableName() {
return '{{disinfection_order}}';
}
public function rules() {
if($this->check_save)
$a=array(
array('restaurant_id', 'required', 'message' => '{attribute} 不能为空'),
array('disinfection_id', 'required', 'message' => '{attribute} 不能为空'),
array('date', 'required', 'message' => '{attribute} 不能为空'),
array('tableware_quantity', 'required', 'message' => '{attribute} 不能为空'),
);
$a[]= array($this->safeField(), 'safe');
return $a;
}
...后面代码省略...
订单控制器
解决思路:明细记录使用一个字段记录对应的订单号。由于点击进入添加时,订单并没有保存,也没有写入数据库,此时订单id=null。所以我先进入actionCreate,new一个订单,跳过模型rules验证,保存一次,再跳转到修改的actionUpdate。
public function actionCreate() {
$modelName = $this->model;
$model = new $modelName('create');
$model->check_save=0;
$model->save();
$this->actionUpdate($model->id);
}
public function actionUpdate($id='0') {
$modelName = $this->model;
$model = $this->loadModel($id, $modelName);
$detailList=DisinfectionOrderDetail::model()->findAll('order_id='.$id);
if (!Yii::app()->request->isPostRequest) {
$data = array();
$data['model'] = $model;
$data['detailList'] = $detailList;
$this->render('update', $data);
} else {
$this->saveData($model, $_POST[$modelName]);
}
}
public function actionOpenDialog(){
$modelName='DisinfectionOrderDetail';
$detail_id=DecodeAsk('detail_id');
if($detail_id){
$model=$this->loadModel($detail_id,$modelName);
}
else{
$model = new DisinfectionOrderDetail();
$model->order_id=DecodeAsk('order_id');
}
if (!Yii::app()->request->isPostRequest) {
$data = array();
$data['model'] = $model;
$data['isClose'] = DecodeAsk('isClose');
$this->render('update_detail', $data);
}else {
$this->Save_detail($model, $_POST['DisinfectionOrderDetail']);
}
}
public function Save_detail($model, $post) {
$model->attributes = $post;
$url=Yii::app()->request->getUrl().'&isClose=1';
show_status($model->save(), '保存成功',$url, '保存失败');
}
public function actionSaveFormDate($id){
$model=$this->loadModel($id,$this->model);
$model->check_save=0;
$model->attributes = $_REQUEST[$this->model];
$model->save();
}
视图
update_detail是修改明细的弹窗子界面
update_detail.php
<div class="box">
<div class="box-title c"><h1><i class="fa fa-table"></i>单位信息</h1><span class="back"></span></div><!--box-title end-->
<div class="box-detail">
<?php $form = $this->beginWidget('CActiveForm', get_form_list()); ?>
<div class="box-detail-tab">
<ul class="c">
<li class="current">基本信息</li>
</ul>
</div><!--box-detail-tab end-->
<div class="box-detail-bd">
<div style="display:block;" class="box-detail-tab-item">
<table>
<tr class="table-title">
<td colspan="2">申请信息</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model, 'order_id'); ?></td>
<td>
<?php echo $form->textField($model, 'order_id', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'order_id', $htmlOptions = array()); ?>
</td>
</tr>
<!-- <tr>-->
<!-- <td>--><?php
<!-- <td>-->
<!-- --><?php
<!-- --><?php
<!-- </td>-->
<!-- </tr>-->
<tr>
<td><?php echo $form->labelEx($model, 'tableware_name'); ?></td>
<td>
<?php echo $form->textField($model, 'tableware_name', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'tableware_name', $htmlOptions = array()); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model, 'unit'); ?></td>
<td>
<?php echo $form->textField($model, 'unit', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'unit', $htmlOptions = array()); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model, 'cost'); ?></td>
<td>
<?php echo $form->textField($model, 'cost', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'cost', $htmlOptions = array()); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model, 'number'); ?></td>
<td>
<?php echo $form->textField($model, 'number', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'number', $htmlOptions = array()); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model, 'total_cost'); ?></td>
<td>
<?php echo $form->textField($model, 'total_cost', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'total_cost', $htmlOptions = array()); ?>
</td>
</tr>
</table>
</div>
</div><!--box-detail-tab-item end style="display:block;"-->
</div><!--box-detail-bd end-->
<div class="box-detail-submit">
<button onclick="save()" class="btn btn-blue" type="submit">保存</button>
</div>
<?php $this->endWidget(); ?>
</div><!--box-detail end-->
</div><!--box end-->
<script>
if('<?php echo $isClose==1?>'){
$.dialog.data('detailId','<?php echo $model->id;?>')
$.dialog.close();
}
$(function(){
let api = $.dialog.open.api;
api.button(
{
name: '取消'
}
);
});
</script>
update.php
<div class="box">
<div class="box-title c"><h1><i class="fa fa-table"></i>单位信息</h1><span class="back"><a class="btn"
href="javascript:;"
onclick="we.back();"><i
class="fa fa-reply"></i>返回</a></span></div><!--box-title end-->
<div class="box-detail">
<?php $form = $this->beginWidget('CActiveForm', get_form_list()); ?>
<div class="box-detail-tab">
<ul class="c">
<li class="current">基本信息</li>
</ul>
</div><!--box-detail-tab end-->
<div class="box-detail-bd">
<div style="display:block;" class="box-detail-tab-item">
<table>
<tr class="table-title">
<td colspan="2">申请信息</td>
</tr>
<tr>
<td width="30%"><?php echo $form->labelEx($model, 'restaurant_id'); ?></td>
<td width="30%">
<?php echo $form->textField($model, 'restaurant_id', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'restaurant_id', $htmlOptions = array()); ?>
</td>
</tr>
<tr>
<td width="30%"><?php echo $form->labelEx($model, 'restaurant_name'); ?></td>
<td width="30%">
<?php echo $form->textField($model, 'restaurant_name', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'restaurant_name', $htmlOptions = array()); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model, 'disinfection_id'); ?></td>
<td>
<?php echo $form->textField($model, 'disinfection_id', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'disinfection_id', $htmlOptions = array()); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model, 'disinfection_name'); ?></td>
<td>
<?php echo $form->textField($model, 'disinfection_name', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'disinfection_name', $htmlOptions = array()); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model, 'date');?></td>
<td>
<?php echo $form->textField($model, 'date', array('class' => 'Wdate','style'=>'width:180px;'));?>
<?php echo $form->error($model, 'date', $htmlOptions = array());?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model, 'state'); ?></td>
<td>
<?php echo $form->textField($model, 'state', array('class' => 'input-text')); ?>
<?php echo $form->error($model, 'state', $htmlOptions = array()); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model, 'complete_time');?></td>
<td>
<?php echo $form->textField($model, 'complete_time', array('class' => 'Wdate','style'=>'width:180px;'));?>
<?php echo $form->error($model, 'complete_time', $htmlOptions = array());?>
</td>
</tr>
<tr>
</tr>
</table>
</div>
</div><!--box-detail-tab-item end style="display:block;"-->
<div class="box-table">
<button class="btn btn-green" style="float: right;margin:5px" type="button" onclick="updateDetail();">+添加餐具</button>
<table class="list">
<thead>
<tr>
<!-- <th class="check"><input id="j-checkall" class="input-check" type="checkbox"></th>-->
<?php $model2 = DisinfectionOrderDetail::model();?>
<?php
$str='order_id,tableware_type,tableware_name,unit,cost,number,total_cost';
echo $model2->gridHead($str); ?>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
if(isset($detailList))
foreach ($detailList as $v) { ?>
<tr>
<!-- <td class="check check-item"><input class="input-check" type="checkbox"-->
<!-- value="--><?php //echo CHtml::encode($v->id); ?><!--"></td>-->
<?php echo $v->gridRow($str); ?>
<td>
<button class="btn" type="button" onclick="updateDetail(<?php echo $v->id;?>);">编辑</button>
<a class="btn" href="javascript:;" onclick="we.dele('<?php echo $v->id; ?>', deleteUrl);"
title="删除"><i class="fa fa-trash-o"></i></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div><!--box-table end-->
</div><!--box-detail-bd end-->
<div class="box-detail-submit">
<button onclick="submitType='baocun'" class="btn btn-blue" type="submit">保存</button>
<button class="btn" type="button" onclick="we.back();">取消</button>
</div>
<?php $this->endWidget(); ?>
</div><!--box-detail end-->
</div><!--box end-->
<script>
$(function() {
var $date=$('#<?php echo get_class($model);?>_date');
$date.on('click', function() {
WdatePicker( {
startDate:'%y-%M-%D %H:%m:%s',dateFmt:'yyyy-MM-dd HH:mm:ss'
}
);
}
);
}
);
$(function() {
var $date=$('#<?php echo get_class($model);?>_complete_time');
$date.on('click', function() {
WdatePicker( {
startDate:'%y-%M-%D %H:%m:%s',dateFmt:'yyyy-MM-dd HH:mm:ss'
}
);
}
);
}
);
</script>
<script>
var deleteUrl = '<?php echo $this->createUrl('DisinfectionOrderDetail/delete', array('id' => 'ID')); ?>';
function updateDetail(id=0){
saveFormDate()
url = '<?php echo $this->createUrl("OpenDialog");?>'
url += '&order_id=<?php echo $model->id;?>'
url +='&detail_id='+id
tl= id===0?'添加明细':'修改明细'
$.dialog.data('id',0)
$.dialog.open(url,{
id: 'updateDetail',
lock:true,opacity:0.3,
width:'1000px',
height:'80%',
title:tl,
close: function () {
redirect = '<?php echo str_replace('create','update',Yii::app()->request->getUrl())?>'
redirect+='&id='+'<?php echo $model->id;?>'
window.location.href = redirect;
}
});
};
function saveFormDate() {
let form=$('#active-form').serialize();
let s1='<?php echo $this->createUrl('SaveFormDate');?>'
s1=s1+'&'+form+'&id='+'<?php echo $model->id;?>'
$.ajax({
url: s1,
type: 'get',
dataType: 'json',
})
}
</script>
注意要点 1.js和php代码混写时,写成格式形如 ‘<?php echo XXX?>’ 2. let form=$(’#active-form’).serialize(); 表单序列化。可把表单作为参数传递,拼接url写成形如’&’+form。序列化第一个变量前面是没有&的。序列化结果形如 a=1&b=2&c=3 3.gridHead ,girdRow是封装的生成列表HTML的方法。不传参返回模型标签定义的全部字段,传字符串,以逗号分隔,作为参数,返回指定的字段。 4.控制器render方法前,一般构造data数组。data里的键值对,可以转化成变量,传递到views视图中直接使用。如上文detailList。 5. window.location.href 是js的重定向方法
|