作业:Product模块添加,修改,删除操作

原创 2018-11-30 21:32:34 608
摘要:作业:Product模块添加,修改,删除操作关于添加操作,由于有editor中有可能会传入图片,因此会有upload上传操作方法public function upload(){ $file=Request::file('img'); if($info=$file->validate(['ext'=>'jpg,jpeg

作业:Product模块添加,修改,删除操作

关于添加操作,由于有editor中有可能会传入图片,因此会有upload上传操作方法

public function upload(){
		$file=Request::file('img');
		if($info=$file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload')){
			return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]);
		}else{
			return $file->getError();
		}
	}
	
	public function doAdd(){
		//获取提交过来的数据
		$data=Request::param();
		$title=$data['title'];
		$info=ProductModel::where('title',$title)->find();
		if($info==true){
			return ['res'=>0,'msg'=>'产品重复'];
		}
		$data['time']=time();
		$data['username']=Session::get('username');
		$product=new ProductModel();
		if($product->save($data)){
			return ['res'=>1,'msg'=>'发布成功'];
		}else{
			return ['res'=>0,'msg'=>'发布失败'];
		}

html相关添加操作

   form.on('submit(add)', function(data){
            console.log(data);
            //发异步,把数据提交给php
            $.post('{:url(\'doAdd\')}',{
            	'title':$('#title').val(),
            	'sort':$('#sort').val(),
            	'desc':$('#desc').val(),
            	'content':editor.txt.html(),
            	'once':$('#once').val(),
            	'over_night':$('#over_night').val(),
            },function(data){
            	if(data.res==1){
	            		layer.alert(data.msg, {icon: 6},function () {
	                // 获得frame索引
	                var index = parent.layer.getFrameIndex(window.name);
	                //关闭当前frame
	                parent.layer.close(index);
	            });            		
            }else{
            			layer.alert(data.msg, {icon: 6},function () {
	                // 获得frame索引
	                var index = parent.layer.getFrameIndex(window.name);
	                //关闭当前frame
	                parent.layer.close(index);
	            });            		
            }
         })

            return false;
          });

编辑处理

public function edit(){
		$proId=Request::param('id');
		$product=ProductModel::get($proId);
		$this->assign('product',$product);
		return $this->fetch();
	}
	
	public function doEdit(){
		//获取前台提交过来的数据
		$data=Request::param();
		$product=new ProductModel();
		$data['time']=time();
		$data['username']=Session::get('username');
		$info=$product->save([
			'title'=>$data['title'],
			'desc'=>$data['desc'],
			'content'=>$data['content'],
			'once'=>$data['once'],
			'over_night'=>$data['over_night'],
			'time'=>$data['time'],
			'username'=>$data['username'],
		],['id'=>$data['id']]);

	if($info){
		return ['res'=>1,'msg'=>'更新成功'];
	}else{
		return ['res'=>0,'msg'=>'更新失败'];
	}
		}
添加一个hidden id.
 <input type="hidden" id="id" name="id" value="{$product.id}">
  $.post('{:url(\'doEdit\')}', {
                'id': $('#id').val(),
                'title': $('#title').val(),
                'desc': $('#desc').val(),
                'content': editor.txt.html(),
                'once': $('#once').val(),
                'over_night': $('#over_night').val(),
            }, function (data) {
                if (data.res == 1) {
                    layer.alert(data.msg, {icon: 6}, function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                    });
                } else {
                    layer.alert(data.msg, {icon: 6}, function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                    });
                }
            })

1.PNG

2.PNG


3.PNG

4.PNG

批改老师:韦小宝批改时间:2018-12-01 09:20:58
老师总结:不错不错!写的很棒哦!项目要多写才能看到成效哦!

发布手记

热门词条