有意思的!

初始化需要ajax获取数据!

搜索商品需要ajax获取数据!

提交数据需要ajax传递数据!

有了 vue-resource ,操作挺方便的。

这是html

<form class="form-horizontal addForm"
id="edit_form"> <div class="form-group">
<label for="goods_id" class="col-lg-2 control-label">商品ID</label>
<div class="col-lg-3"> <!-- 通过div控制长度 -->
<input type="text" class="form-control" id="goods_id" placeholder="请输入商品id进行搜索">
</div> <label for="goods_type" class="col-lg-2 control-label">类型</label>
<div class="col-lg-2">
<select id="goods_type" class="form-control">
<option value="1">线上</option>
<option value="2">门店</option>
</select>
</div> <label class="control-label col-lg-1 del-btn">
<button type="button" class="btn btn-sm btn-primary" @click="search">搜索</button>
</label>
</div> <div class="form-group col-lg-12">
<label class="control-label col-lg-4"></label>
<label class="col-lg-5 text-left">相关商品</label>
<label class="control-label col-lg-1"></label>
</div> <div class="form-group" v-for="(item, i) in items">
<label class="col-lg-1 control-label">ID:{{item.goods_id}}</label>
<label class="col-lg-1 control-label">类型:
<span v-if="item.type == 1">
线上
</span>
<sapn v-else>
门店
</sapn>
</label> <label class="col-lg-2 control-label">名称:{{item.name}}</label>
<label class="col-lg-1 control-label">图片:</label>
<div class="col-lg-2">
<img :src="item.img_url" width="120px;">
</div> <label for="is_heavy" class="col-lg-1 control-label">是否头部:</label>
<div class="col-lg-1">
<input type="checkbox" class="" id="is_heavy" v-model="item.is_heavy">
</div> <label class="control-label col-lg-1 del-btn">
<button type="button" class="btn btn-xs btn-danger del-img" @click="remove_item(i)">删除</button>
</label>
</div> <input type="hidden" name="id" id="id" value="{$id}" />
<input type="hidden" name="page" id="page" value="{$page}" />
<div class="form-group">
<div class="col-lg-2 col-lg-offset-3">
<button type="button" class="btn btn-primary col-lg-8" @click="submit">保存</button>
</div> <div class="col-lg-2">
<button type="button" class="btn btn-danger col-lg-8" @click="cancel">取消</button>
</div>
<div class="clear"></div>
</div>
</form>

这是js

<script>
// 定义全局锁
var lock_flag = false;
var page = $('#page').val(); var app = new Vue({
el: '#edit_form',
data: {
items: []
},
mounted() {
// 获取数据并进行初始化操作
var id = GetQueryString('id');
this.$http.post('step_3_init',{
id:id,
},{emulateJSON:true}).then(function(res){
if (res.body.errno !== 0) {
layer.msg(res.body.errdesc);
return false;
} res.body.data.forEach(function (item,index) {
res.body.data[index]['is_heavy'] = parseInt(res.body.data[index]['is_heavy']);
}); this.items = res.body.data;
},function(res){
console.log(res.status);
});
},
methods: {
remove_item:function(index) {
this.items.splice(index,1);
},
cancel:function() {
window.location.href='index?page=' + page;
},
search:function() {
var goods_id = parseInt($('#goods_id').val());
var goods_type = $('#goods_type').val(); if (!/^[0-9]*[1-9][0-9]*$/.test(goods_id)) {
layer.msg('请输入商品id');
return;
} //发送 post 请求
this.$http.post('get_search_goods',{
goods_id:goods_id,
goods_type:goods_type
},{emulateJSON:true}).then(function(res){
if (res.body.errno !== 0) {
layer.msg(res.body.errdesc);
return false;
}
// 检查相同的数据是否存在
var exist = 0;
this.items.forEach(function (item,index) {
if (item.id == res.body.data.id && item.type == res.body.data.type) {
exist ++;
}
});
if (exist > 0) {
layer.msg('商品数据已存在,请勿重复添加');
return;
} this.items.push(res.body.data);
},function(res){
console.log(res.status);
});
},
submit:function() {
if (!lock_flag) {
lock_flag = true;
var post_data = {};
post_data.id = $("#id").val();
post_data.items = this.items; //发送 post 请求
this.$http.post('step_3_save',post_data,{emulateJSON:true}).then(function(res){
if(res.body.errno == 0){
layer.msg("已保存");
console.log(JSON.stringify(res));
setTimeout(function() {
window.location.href = 'index?page=' + page;
},2000);
}else{
// 解锁
lock_flag = false;
layer.msg(res.body.errdesc);
}
},function(res){
lock_flag = false;
console.log(res.status);
});
} }
}
}); </script>

这是php

    // 相关的商品
public function step_3() {
$id = (int)$_GET['id'];
$this->assign('id',$id); $page = (int)$_GET['page'];
$this->assign('page', $page); $mill_dishes_model = M('mill_dishes');
$result = $mill_dishes_model->where(array('id'=>$id))->find();
if(!$result){
$this->success('数据不存在','index?page='.$page);
return false;
} // 获取配料数据
$this->display();
} // 初始化
public function step_3_init() {
vendor('Func.Json');
$json = new Json();
$id = (int)$_POST['id']; // 获取配料数据
$mill_dishes_cross_model = M('mill_dishes_cross');
$mill_dishes_cross_list = $mill_dishes_cross_model->where(['dishes_id'=>$id,'is_del'=>0])->field('id,goods_id,type,is_heavy')->select(); foreach ($mill_dishes_cross_list as $k=>&$v) {
// 获取商品名称图片
if ($v['type'] == 1) {
$goods_model = M('goods');
$info = $goods_model->where(['id'=>$v['goods_id']])->find();
$v['name'] = $info['name'];
$v['img_url'] = C('CDN_HOST') . $info['img_list'];
} else {
$store_goods_model = M('store_goods');
$info = $store_goods_model->where(['id'=>$v['goods_id']])->find();
$v['name'] = $info['name'];
$v['img_url'] = C('CDN_HOST') . $info['main_image'];
}
} $json->setErr(0, '操作成功');
$json->setAttr('data', $mill_dishes_cross_list);
$json->send();
} // 相关的商品保存
public function step_3_save() {
vendor('Func.Json');
$json = new Json();
// json传入的数据
$id = (int)$_POST['id'];
$items = $_POST['items']; $mill_dishes_cross_model = M('mill_dishes_cross');
$mill_dishes_cross_model->where(['dishes_id'=>$id])->save(['is_del'=>1]);
foreach ($items as $k=>$item) {
// 进行添加修改操作
$info = $mill_dishes_cross_model->where(['id'=>$item['id']])->find();
if ($info) { // 修改
$op_data = [
'goods_id' => $item['goods_id'],
'type' => $item['type'],
'is_heavy' => $item['is_heavy'] == 'true' ? 1 : 0,
'is_del' => 0
];
$mill_dishes_cross_model->where(['id'=>$item['id']])->save($op_data);
} else { // 添加
$op_data = [
'dishes_id' => $id,
'goods_id' => $item['goods_id'],
'type' => $item['type'],
'is_heavy' => $item['is_heavy'] == 'true' ? 1 : 0,
'add_time' => time()
];
$mill_dishes_cross_model->add($op_data);
}
} $json->setErr(0, '操作成功');
$json->setAttr('id', $id);
$json->send();
} // 搜索商品
public function get_search_goods() {
vendor('Func.Json');
$json = new Json();
$goods_id = $_POST['goods_id'];
if (!$goods_id){
$json->setErr('10001','缺少参数goods_id');
$json->Send();
} $goods_type = $_POST['goods_type'];
if (!$goods_type){
$json->setErr('10002','缺少参数goods_type');
$json->Send();
} $out_data = [];
if ($goods_type == 1) { // 获取线上商品
$goods_model = M('goods');
$info = $goods_model->where(['id'=>$goods_id,'is_sold'=>1,'status'=>1])->find();
if (!$info) {
$json->setErr('10003','商品不存在或已下架');
$json->Send();
} $out_data = [
'id' => 0,
'goods_id' => $info['id'],
'name' => $info['name'],
'img_url' =>C('CDN_HOST') . $info['img_list'],
'type' => 1,
'is_heavy' => 0
]; } else { // 获取门店商品
$store_goods_model = M('store_goods');
$info = $store_goods_model->where(['id'=>$goods_id,'is_sale'=>1,'is_del'=>0])->find();
if (!$info) {
$json->setErr('10003','商品不存在或已下架');
$json->Send();
} $out_data = [
'id' => 0,
'goods_id' => $info['id'],
'name' => $info['name'],
'img_url' =>C('CDN_HOST') . $info['main_image'],
'type' => 2,
'is_heavy' => 0
];
} $json->setErr(0, '操作成功');
$json->setAttr('data', $out_data);
$json->send();
}

VUE 结合 vue-resource 进行ajax操作的更多相关文章

  1. Vue中应用CORS实现AJAX跨域,及它在 form data 和 request payload 的小坑处理

    基本概念部分(一):理解CORS 说道Vue的跨域AJAX,我想先梳理一遍CORS跨域,"跨域资源共享"(Cross-origin resource sharing),它是一个W3 ...

  2. vue分页全选和单选操作

    <!DOCTYPE html> <html> <head> <title>演示Vue</title> <style> ul,li ...

  3. vue实践---vue结合 promise 封装原生ajax

    有时候不想使用axios这样的外部依赖,想自己封装ajax,这里有两种方法 方法一,在单个页面内使用 封装的代码如下: beforeCreate () { this.$http = (() => ...

  4. Vue(七)发送Ajax请求

    发送AJAX请求 1. 简介 vue本身不支持发送AJAX请求,需要使用vue-resource.axios等插件实现 axios是一个基于Promise的HTTP请求客户端,用来发送请求,也是vue ...

  5. Vue中使用axios发送ajax请求

    作为前后端交互的重要技巧--发送ajax请求,在Vue中我们使用axio来完成这一需求: 首先是下载axios的依赖, npm install --save axios vue-axios 然后在ma ...

  6. vue入门 vue与react和Angular的关系和区别

    一.为什么学习vue.js vue.js兼具angular.js和react的优点,并且剔除了他们的缺点 官网:http://cn.vuejs.org/ 手册:http://cn.vuejs.org/ ...

  7. Vue (二) --- Vue对象提供的属性功能

    --------------------------------------------不是井里没有水,而是你挖的不够深. 3. Vue对象提供的属性功能 3.1 过滤器 过滤器,就是vue允许开发者 ...

  8. 02 Vue之vue对象属性功能&axios数据请求实现

    1.过滤器的声明和使用 过滤器,就是vue允许开发者自定义的文本格式化函数,可以使用在两个地方:输出内容和操作数据中. 定义过滤器的方式有两种. 1 使用Vue.filter()进行全局定义 2 在v ...

  9. vue学习:vue+webpack的快速使用指南(新手向)

    一.vue有两种使用方式: 1.下载vue.js <script src="vue.js"></script> 2.使用npm npm install vu ...

  10. Vue -- vue-cli(vue脚手架) npm run build打包优化

    这段时间公司新项目立项,开发组选用 Vue2.0 进行开发.当然也就一并用到 vue cli 进行自动化构建.结果在基础版本开发完成后,用 npm run build 命令打包上线时,发现以下几个问题 ...

随机推荐

  1. OracleUNDO

    UNDO作用 数据的回滚 一致性读 表的闪回(事务,查询的闪回....) 失败会话的恢复 数据的回滚 SQL> rollback; 回滚的过程就是从回滚段里拿到刚刚执行的这条语句产生的回滚,然后 ...

  2. The OpenCV Coding Style Guide

    https://github.com/opencv/opencv/wiki/Coding_Style_Guide

  3. 如果"一切是IO"“一切是file”是成立的,那么上述的想法也一定可以实现吧 awk对apache日志分析 ---

    定时执行 自动化处理 直接入库 再去读取这个file入库: root@VM---ubuntu:/var/log/apache2# awk '{print $1 "\t" $7}' ...

  4. IO流入门-第二章-FileOutputStream

    FileOutputStreamj基本用法和方法示例 /* java.io.OutputStream java.io.FileOutputStream 文件字节输出流 将计算机内存中的数据写入到硬盘文 ...

  5. Java基础 - 面向对象 - 构造方法

    在类中除了成员方法之外,还存在一种特殊类型的方法,那就是构造方法.构造方法是一个与类同名的方法,对象的创建就是通过构造方法完成的.每当类实例化一个对象时,类都会自动调用构造方法. 构造方法的特点: 构 ...

  6. 第18章—后端分页(Mybatis)

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...

  7. 8.Query Documents-官方文档摘录

    总结 1 先插入数据 db.inventory.insertMany([ { item: "journal", qty: 25, size: { h: 14, w: 21, uom ...

  8. MariaDB日志

    1.查询日志:一般来说不开开启(会产生额外压力,并且不一定有价值),query log 记录查询操作:可以记录到文件(file)中也可记录到表(table)中 general_log=ON|OFF g ...

  9. java二叉排序树

    二叉排序树又称二叉查找树.它或者是一颗空树,或者是具有如下性质的二叉树: 1.如果左子树不空,那么左子树上的所有节点均小于它的根节点的值: 2.如果右子树不空,那么右子树上的所有节点均大于它的根节点的 ...

  10. php debug函数

    $debug=$_GET['debug'];//是说获取url中debug变量$debug=empty($debug)?'':$debug;//如果变量不为空,赋值为$debug,为空的话赋值 ''$ ...