jq模拟简易购物车

<div class="dwfbgs">
	<div class="fbgsBox">
		<el-form ref="form" :model="form" label-width="80px" style="text-align: center;">
		  <el-form-item>
			  <el-input
			    type="textarea"
			    :rows="4"
				maxlength="50"
			    placeholder="请输入公告标题"
			    v-model="textarea">
			  </el-input>
		  </el-form-item>
		  <el-form-item>
			  <div class="tabs">
			  	<el-tabs v-model="activeName">
					<!--  @tab-click="handleClick" -->
			  	    <el-tab-pane label="曳引与强制驱动电梯" name="first"></el-tab-pane>
			  	    <el-tab-pane label="液压驱动电梯" name="second"></el-tab-pane>
			  	    <el-tab-pane label="杂物电梯" name="third"></el-tab-pane>
			  	    <el-tab-pane label="自动扶梯与自动人行道" name="1"></el-tab-pane>
			  	  </el-tabs>
			  </div>
			  <el-button type="primary"style="margin-right: -68%;margin-bottom: 1%;" @click="handleUsersAdd">新增</el-button>
			  <el-table 
			      :data="tableData"
			      border
			      style="width: 80%;margin-left:8%;margin-bottom: 1%;">
			      <el-table-column
			        prop="index"
			        label="序号"
			  	  class-name="column-bg-color-editable"
			  	  width="150"
			  	  >
			  	  <!-- show-overflow-tooltip -->
			      </el-table-column>
			      <el-table-column
			        prop="username"
			        label="项目名称"
			        width="360">
			      </el-table-column>
			      <el-table-column
			        prop="password"
			        label="维保周期(天)" 
			  	  width="260">
			      </el-table-column>
			  	<el-table-column
			  	  prop="email"
			  	  label="备注" 
			  	  width="360">
			  	</el-table-column>
				<el-table-column prop="" label="操作">
					<template slot-scope="scope">
					          <el-button @click="handleEdit(scope.$index)">编辑</el-button>
					        </template>
				</el-table-column>
			    </el-table>
		  </el-form-item>
		  <div style="display: flex;margin-left: 13%;margin-top: 3%;">
		  	<h1>计算公式:</h1>
		  	<el-button type="success" style="margin-left: 5%;">困人数救援平均到场时间</el-button>
		  	<el-button type="success" style="margin-left: 5%;">百台电梯困人数</el-button>
		  	<el-button type="success" style="margin-left: 5%;">百台检验不合格项数</el-button>
		  	<el-button type="success" style="margin-left: 5%;">按时维保率</el-button>
		  	<el-button type="success" style="margin-left: 5%;">物联网覆盖率</el-button>
		  </div>
		  <el-form-item>
			  <el-table
			        :data="tableData2"
			        border
			        style="width: 80%; margin-left: 8%;margin-top: 2%;" >
					<!-- :span-method="objectSpanMethod" -->
			        <el-table-column
			          prop="data"
			          label="序号">
			        </el-table-column>
			        <el-table-column
			          prop="address"
			          label="公示内容">
			        </el-table-column>
			        <el-table-column
			          prop="amount2"
			          label="承诺内容">
					  
			        </el-table-column>
			        
			      </el-table>
		  </el-form-item>
		  <div style="text-align: center;margin-top: 5%;">
		    <el-button type="primary">保存</el-button>
		  			<el-button type="primary">发布</el-button>
		    <el-button>返回</el-button>
		  </div>
		</el-form>
		<el-dialog title="添加用户" :visible.sync="dialogFormVisible">
		      <el-form :model="form">
				  <el-form-item label="序号" label-width="100px">
				    <el-input v-model="form.index" auto-complete="off"></el-input>
				  </el-form-item>
		        <el-form-item label="项目名称" label-width="100px">
		          <el-input v-model="form.username" auto-complete="off"></el-input>
		        </el-form-item>
		        <el-form-item label="维保周期(天)" label-width="100px">
		          <el-input v-model="form.password" auto-complete="off"></el-input>
		        </el-form-item>
		        <el-form-item label="备注" label-width="100px">
		          <el-input v-model="form.email" auto-complete="off"></el-input>
		        </el-form-item>
		        
		      </el-form>
		      <div slot="footer" class="dialog-footer">
		        <el-button @click="dialogFormVisible = false">取 消</el-button>
		        <el-button type="primary" @click="handleSure">确 定</el-button>
		      </div>
		</el-dialog>
		
	</div>
</div>


define([
    'vue',
	'jquery',
	// '../../api/dwfbgs',
    'text!./dwfbgs.html',
    'css!./dwfbgs.css',
], function(vue,jquery,tempLate) {
    'use strict';
    var dwfbgs = vue.extend({
        template: tempLate,
        data() {
            return {
				dialogFormVisible:false,
				 tableData: [],
				 tableData2:[{
				          data: '1',
				          name: '王小虎',
				          address: '困人数救援平均到场时间'
				        },
						{
						         data: '2',
						         name: '王小虎',
						         address: '百台电梯困人数'
						       },
						{
						         data: '3',
						         name: '王小虎',
						         address: '百台检验不合格项数'
						       },
						{
						         data: '4',
						         name: '王小虎',
						         address: '按时维保率'
						       },
						{
								data: '4',
								name: '王小虎',
								address: '物联网覆盖率'
						      },
						{
								data: '单位名称',

						      },
						],
						  textarea: '',
				form: {
					index:'',
					username:'',
					password:'',
					email:''
				},
				activeName: 'first',
        		isedit:false,
				editindex:''
        	}
        },
		created() {
			
			
		},
        mounted() {
        },
        methods: {
			
			handleSure() {
				let form = JSON.parse(JSON.stringify(this.form))
				if(this.tableData.length){
					let status = this.tableData.some(item=>{
						return item.index == form.index
						
					})
					console.log(status)
					if(status){
						if(this.isedit){
							this.tableData[this.editindex] = form
						}else{
						this.$message({type:"error",message:"序号不可重复"})
						}
					}else{
							this.tableData.unshift(form)
					}
				}else{
					this.tableData.unshift(form)
				}
				this.clear()
			},	
			clear(){
				this.dialogFormVisible = false
				this.form.index = ''
				this.form.username = ''
				this.form.password = ''
				this.form.email = ''
			},
			handleEdit (index) {
				console.log(index)
				this.editindex = index
				this.isedit = true
				console.log(index)
				this.dialogFormVisible = true
				this.form =JSON.parse(JSON.stringify(this.tableData[index])) 
			},
			handleUsersAdd () {
				this.isedit = false
			      this.form = {}
			      this.dialogFormVisible = true
			    },
        }
    });
    return dwfbgs;
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设你有一个商品列表,每个商品都有名称、价格和库存数量。你可以使用 jq 编写一个简单的购物车程序来处理用户的购买请求。以下是一个可能的实现: ```bash # 商品列表,格式为 JSON 数组 products='[ {"name": "apple", "price": 2.5, "quantity": 10}, {"name": "banana", "price": 1.5, "quantity": 5}, {"name": "orange", "price": 3, "quantity": 8}, {"name": "pear", "price": 2, "quantity": 12} ]' # 定义一个空的购物车,格式为 JSON 对象 cart='{"items": []}' # 处理用户输入,假设用户输入的是商品名称和购买数量,格式为 "name:quantity" input="apple:3" # 解析用户输入,获取商品名称和购买数量 name=$(echo "$input" | cut -d':' -f1) quantity=$(echo "$input" | cut -d':' -f2) # 查找商品列表中对应的商品 product=$(echo "$products" | jq ".[] | select(.name == \"$name\")") # 检查商品是否存在以及库存是否足够 if [ -z "$product" ]; then echo "Product not found" elif [ "$(echo "$product" | jq '.quantity')" -lt "$quantity" ]; then echo "Not enough stock" else # 将商品添加到购物车中 item=$(echo "$product" | jq ".quantity = $quantity") cart=$(echo "$cart" | jq ".items += [$item]") echo "Item added to cart" fi # 打印购物车内容 echo "$cart" | jq ``` 这个程序使用 jq 命令来解析和操作 JSON 数据。首先定义了商品列表和购物车的初始值,然后处理用户输入,并在商品列表中查找对应的商品。如果商品存在且库存足够,将商品添加到购物车中。最后打印购物车内容。你可以根据需要扩展这个程序,例如支持删除购物车中的商品、计算购物车总价等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值