uniapp-商城-55-后台 新增商品(分类、验证和弹窗属性)

1、概述

        在前面 ,我们将商品页面的布局给完成了,这里来对表单的标签输入进行校验,看看这里的校验还是不是也需要兼容微信小程序,还有没有前面遇到的自定义正则进行校验的情况。

        另外这里还需要完成商品属性的添加,就是前面在布局中提到的弹窗。弹窗这里涉及的内容还很多,后面我们还会进一步来分析和研究。

2、表单校验

包含图片、名称、价格、类别、属性、描述等等,这里我们看到应该必须要有图片、名称、价格、类别这个几个元素

    <uni-forms ref="goodsForm" :model="goodsFormData" :rules="goodsRules" :label-width="100" label-align="right">

2.1 这里就没有删除rules ,因为我们这里没有自定义的校验规则。

2.2  必须填写的属性,页面的froms-item 中要加入 required这个属性值,js规则部分,要通过name定义其他规则,也加入required,没有就不管了,如这里的图。

2.3 要进行规则的校验,必须填写name 便于进行规则的指定

没有name,没有办法指定,见上图

2.4 data的规则数据定义。绑定到标签的 name

如这里的name 和 price  就是名称和价格的规则。写道我们表单的rules里面(goodsRules)

特别要注意书写的格式。

3、分类 使用的下拉框  云端数据获取 就这个uni-data-select 可以获取云端数据,所以用这个组件较为方便。

  需要安装,并使用云端数据获取

			<uni-forms-item label="产品分类" required name="category_id"><!-- 云端数据  下拉框获取  field 就是获取的内容  一定要写成 value text 这是官方定义的  value选中的值,text显示的文本,也就是value后台用,text前台用 --><!-- 利用这个组件就实现了后端数据库的读取 --><uni-data-select collection="green-mall-categories" field="_id as value, name as text"v-model="goodsFormData.category_id"></uni-data-select></uni-forms-item>

4、商品属性 采用的cell的单元格(uview中组件,可以被点击)

4.1 cell

该组件就是cell单元格。一般用于一组列表的情况,比如个人中心页,设置页等,默认有一个边框,且整个可以被点击 。

title是组件显示的名称; 

isLink  就是显示一个右侧的箭头;

border:不用显示默认的边框; 注意使用加v-band(也就是加 :)

<u-cell :title="skuTitle" isLink :border="false" @click="clickSelect"></u-cell>

然后我们给这个点击加一个事件。需要-可以读5小节代码。

            //点击选择属性
            clickSelect() {
                this.$refs.attrWrapPop.open();
                if (this.skuArr.length) return;
                this.getSkuData();

            },

4.2 在这个组件下面,可以将填写的属性展示出来。

具体可以阅读,5小节的代码。

                <view class="skuList">
                    <view class="item" v-for="item in goodsFormData.sku_select" @click="clickSelect">
                        <view class="left">{{item.skuName}}:</view>
                        <view class="right">{{skuChildName(item.children)}}</view>
                    </view>
                </view>

4.3 弹出窗 使用的uni-popup

前面的章节,也对这个组件进行过研究。

/https://uniapp.dcloud.net.cn/component/uniui/uni-popup.html

在 47 48章节 。

4.3.1 第一点击cell 从底部弹出窗

注意:type要加

		<uni-popup ref="attrWrapPop" type="bottom"><!-- 底部弹出 type ref 是一个名字属性,便于被调用 给 clickSelect 在 clickSelect 函数中调用了该接口--><view class="attrWrapper"><view class="head"><view class="title">商品属性</view><view class="addAttr" @click="clickAddAttr()">+ 添加属性</view></view><view class="body"><view class="item" v-for="(item,index) in skuArr"><view class="top"><checkbox :checked="item.checked" @click="changeCheckbox(index)"></checkbox><view class="font">{{item.skuName}}</view></view><view class="btnGroup" v-if="item.checked"><view class="btn" :class="child.checked?'active':''" v-for="(child,cIdx) in item.children"@click="clickChlidBtn(index,cIdx)">{{child.name}}</view><view class="btn" @click="clickAddAttr(index)"><u-icon name="plus"></u-icon></view></view></view></view><view class="foot"><button type="primary" @click="clickConfirmSelect">确认选择</button></view></view><view class="safe-area-bottom"></view></uni-popup>

4.3.2 就是展示可以选手属性元素 布局如下

这一部分就是显示可以选的属性值

1、属性 可以有多个,如颜色、规格、口味等,就是一个 循环 实现

        <view class="item" v-for="(item,index) in skuArr">

2、每一个属性 可以罗列多个可选项 进行选择,并可以手动添加 可选项

        2.1 分成两部分 一个top(显示属性 ) 前面有一个可选框checkbox,后面是属性名字

        <checkbox :checked="item.checked" @click="changeCheckbox(index)"></checkbox>

        <view class="font">{{item.skuName}}</view>

        2.2 一个就是可以选的属性组排列,最后跟一个图标 +号

 <view class="top">
          <checkbox :checked="item.checked" @click="changeCheckbox(index)"></checkbox>
          <view class="font">{{item.skuName}}</view>
  </view>


<view class="btnGroup" v-if="item.checked">
           <view class="btn" :class="child.checked?'active':''" v-for="(child,cIdx) in item.children"
                                @click="clickChlidBtn(index,cIdx)">{{child.name}}</view>
           <view class="btn" @click="clickAddAttr(index)">
                   <u-icon name="plus"></u-icon>
           </view>
 </view>

3、点击添加,又有点击事件--->弹出窗

                 <view class="btn" @click="clickAddAttr(index)">
                        <u-icon name="plus"></u-icon>
                 </view>

4、具体的css样式见5的代码

5、代码

<template><view class="goodsView"><!-- 添加商品 --><uni-forms ref="goodsForm" :model="goodsFormData" :rules="goodsRules" :label-width="100" label-align="right"><uni-forms-item label="商品图片" required="true"><uni-file-picker v-model="goodsFormData.thumb" fileMediatype="image" mode="grid" ></uni-file-picker></uni-forms-item><uni-forms-item label="商品名称" required name="name" ><!-- trim 去空格 --><uni-easyinput v-model="goodsFormData.name" placeholder="请输入商品名称" trim="both"></uni-easyinput></uni-forms-item><uni-forms-item label="产品分类" required name="category_id"><!-- 云端数据  下拉框获取  field 就是获取的内容  一定要写成 value text 这是官方定义的  value选中的值,text显示的文本,也就是value后台用,text前台用 --><!-- 利用这个组件就实现了后端数据库的读取 --><uni-data-select collection="green-mall-categories" field="_id as value, name as text"v-model="goodsFormData.category_id"></uni-data-select></uni-forms-item><uni-forms-item label="商品价格" required name="price"><!-- trim 去空格 --><uni-easyinput type="number" v-model="goodsFormData.price" placeholder="请输入商品价格"trim="both"></uni-easyinput></uni-forms-item><uni-forms-item label="商品原价"><!-- trim 去空格 --><uni-easyinput type="number" v-model="goodsFormData.before_price" placeholder="请输入原价"trim="both"></uni-easyinput></uni-forms-item><uni-forms-item label="商品属性" ><u-cell :title="skuTitle" isLink :border="false" @click="clickSelect"></u-cell><view class="skuList"><view class="item" v-for="item in goodsFormData.sku_select" @click="clickSelect"><view class="left">{{item.skuName}}:</view><view class="right">{{skuChildName(item.children)}}</view></view></view></uni-forms-item><uni-forms-item label="商品描述"><!-- type 是类型  textarea 就是大框 --><uni-easyinput type="textarea" placeholder="请输入详细的描述信息"v-model="goodsFormData.description"></uni-easyinput></uni-forms-item><view class="btnView"><button type="primary" @click="onSubmit">确认提交</button></view></uni-forms><uni-popup ref="attrWrapPop" type="bottom"><view class="attrWrapper"><view class="head"><view class="title">商品属性</view><view class="addAttr" @click="clickAddAttr()">+ 添加属性</view></view><view class="body"><view class="item" v-for="(item,index) in skuArr"><view class="top"><checkbox :checked="item.checked" @click="changeCheckbox(index)"></checkbox><view class="font">{{item.skuName}}</view></view><view class="btnGroup" v-if="item.checked"><view class="btn" :class="child.checked?'active':''" v-for="(child,cIdx) in item.children"@click="clickChlidBtn(index,cIdx)">{{child.name}}</view><view class="btn" @click="clickAddAttr(index)"><u-icon name="plus"></u-icon></view></view></view></view><view class="foot"><button type="primary" @click="clickConfirmSelect">确认选择</button></view></view><view class="safe-area-bottom"></view></uni-popup><uni-popup ref="addAttrPop"><uni-popup-dialog mode="input" title="新增" placeholder="请输入新增的内容"@confirm="dialogConfirm"></uni-popup-dialog></uni-popup></view>
</template><script>const skuCloudObj = uniCloud.importObject("kt-mall-sku", {"customUI": true});const goodsCloudObj = uniCloud.importObject("kt-mall-goods", {"customUI": true})export default {data() {return {goodsFormData: {thumb: [],name: "",category_id: null,price: null,before_price: null,description: "",sku_select: []},addAttrType: "parent", //parent代表父,child代表子goodsRules: {name: {rules: [{required: true,errorMessage: "请输入产品名称"}]},price: {rules: [{required: true,errorMessage: "请输入产品价格"}]},category_id: {rules: [{required: true,errorMessage: "请输入产品分类"}]}},skuArr: []};},onLoad() {},computed: {skuTitle() {if (this.goodsFormData.sku_select.length) {let arr = this.goodsFormData.sku_select.map(item => {return item.skuName})return arr.join("/")} else {return "点击添加属性"}}},methods: {//属性返回子元素的名称skuChildName(arr) {let nsArr = arr.map(item => {return item.name})return nsArr.join("/")},//点击确认选择clickConfirmSelect() {let arr = this.skuArr.filter(item => {let state = item.children.some(child => child.checked)return item.checked && state}).map(item => {let children = item.children.filter(child => {return child.checked})return {...item,children}})this.goodsFormData.sku_select = arrthis.$refs.attrWrapPop.close();},//获取sku列表async getSkuData() {let res = await skuCloudObj.get();this.skuArr = res.dataconsole.log(res);},//点击添加属性clickAddAttr(index = null) {if (index == null) {this.addAttrType = "parent"this.attrIndex = null} else {this.addAttrType = "child"this.attrIndex = index}this.$refs.addAttrPop.open();},//添加属性弹窗的确认按钮async dialogConfirm(e) {if (!e) return;if (this.addAttrType == "parent") {let obj = {skuName: e,checked: true,children: []}let res = await skuCloudObj.add(obj)obj._id = res.id;this.skuArr.push(obj)} else if (this.addAttrType == "child") {let obj = {name: e,checked: true}let id = this.skuArr[this.attrIndex]._id;let res = await skuCloudObj.updateChild(id, obj)this.skuArr[this.attrIndex].children.push(obj)}},//点击属性的复选框changeCheckbox(index) {this.skuArr[index].checked = !this.skuArr[index].checked},//点击属性值的子元素clickChlidBtn(index, cIdx) {this.skuArr[index].children[cIdx].checked = !this.skuArr[index].children[cIdx].checked},//点击选择属性clickSelect() {this.$refs.attrWrapPop.open();if (this.skuArr.length) return;this.getSkuData();},//点击提交表单onSubmit() {this.$refs.goodsForm.validate().then(res => {this.toDataBase();}).catch(err => {console.log(err);})},//上传到云数据库async toDataBase() {//这里缺少一个更新的按钮,需要在list中去实现this.goodsFormData.thumb = this.goodsFormData.thumb.map(item => {return {url: item.url,name: item.name,extname: item.extname}})let res = await goodsCloudObj.add(this.goodsFormData)uni.showToast({title: "新增商品成功"})setTimeout(() => {uni.navigateBack()}, 1500)}}}
</script><style lang="scss" scoped>.goodsView {padding: 30rpx;.skuList {.item {padding: 30rpx;background: $page-bg-color;margin: 15rpx 0;@include flex-box-set(start);}}}.attrWrapper {padding: 30rpx;background: #fff;border-radius: 20rpx 20rpx 0 0;.head {@include flex-box();font-size: 34rpx;margin-bottom: 30rpx;.title {font-weight: bold;}.addAttr {color: $brand-theme-color-aux;}}.body {.item {border-top: 1px solid $border-color-light;&:last-child {border-bottom: 1px solid $border-color-light;}.top {padding: 30rpx 0;@include flex-box-set(start);.font {padding-left: 10rpx;font-weight: bold;}}.btnGroup {padding: 10rpx 0 30rpx;@include flex-box-set(start);flex-wrap: wrap;.btn {padding: 0rpx 25rpx;height: 60rpx;border: 1rpx solid $border-color-light;margin-right: 20rpx;border-radius: 10rpx;color: $text-font-color-2;margin-bottom: 20rpx;@include flex-box-set();&.active {border-color: $brand-theme-color;color: $brand-theme-color;background: rgba(236, 87, 79, 0.1);}}}}}.foot {padding: 50rpx 200rpx;}}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/81073.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PyInstaller 打包后 Excel 转 CSV 报错解决方案:“excel file format cannot be determined“

一、问题背景 在使用 Python 开发 Excel 转 CSV 工具时,直接运行脚本(python script.py)可以正常工作,但通过 PyInstaller 打包成可执行文件后,出现以下报错: excel file format cannot be determined, you must specify an engine manually 该问题通常发生在使用pandas…

【HTML 全栈进阶】从语义化到现代 Web 开发实战

目录 &#x1f31f; 前言&#x1f3d7;️ 技术背景与价值&#x1fa79; 当前技术痛点&#x1f6e0;️ 解决方案概述&#x1f465; 目标读者说明 &#x1f9e0; 一、技术原理剖析&#x1f4ca; 核心概念图解&#x1f4a1; 核心作用讲解&#x1f527; 关键技术模块说明⚖️ 技术选…

小结:网页性能优化

网页性能优化是提升用户体验、减少加载时间和提高资源利用率的关键。以下是针对网页生命周期和事件处理的性能优化技巧&#xff0c;结合代码示例&#xff0c;重点覆盖加载、渲染、事件处理和资源管理等方面。 1. 优化加载阶段 减少关键资源请求&#xff1a; 合并CSS/JS文件&a…

【AI学习】AI大模型技术发展研究月报的生成提示词

AI大模型技术发展研究月报生成提示词 请输出AI大模型技术发展研究月报&#xff0c;要求如下&#xff1a; —————————— 任务目标 在今天&#xff08;{{today}}&#xff09;往前连续 30 天内&#xff0c;检索已正式公开发表的、与AI大模型&#xff08;参数量 ≥10B&am…

AI 实践探索:辅助生成测试用例

背景 目前我们的测试用例主要依赖人工生成和维护&#xff0c;AI时代的来临&#xff0c;我们也在思考“AI如何赋能业务”&#xff0c;提出了如下命题&#xff1a; “探索通过AI辅助生成测试用例&#xff0c;完成从需求到测试用例生成的穿刺”。 目标 找全测试路径辅助生成测…

C#实现访问远程硬盘(附源码)

在现实场景中&#xff0c;我们经常用到远程桌面功能&#xff0c;而在某些场景下&#xff0c;我们需要使用类似的远程硬盘功能&#xff0c;这样能非常方便地操作对方电脑磁盘的目录、以及传送文件。那么&#xff0c;这样的远程硬盘功能要怎么实现了&#xff1f; 这次我们将给出…

02.Golang 切片(slice)源码分析(一、定义与基础操作实现)

Golang 切片&#xff08;slice&#xff09;源码分析&#xff08;一、定义与基础操作实现&#xff09; 注意当前go版本代码为1.23 一、定义 slice 的底层数据是数组&#xff0c;slice 是对数组的封装&#xff0c;它描述一个数组的片段。两者都可以通过下标来访问单个元素。 数…

记参加一次数学建模

题目请到全国大学生数学建模竞赛下载查看。 注&#xff1a;过程更新了很多文件&#xff0c;所有这里贴上的有些内容不是最新的&#xff08;而是草稿&#xff09;。 注&#xff1a;我们队伍并没有获奖&#xff0c;文章内容仅供一乐。 从这次比赛&#xff0c;给出以下赛前建议 …

virtualbox虚拟机中的ubuntu 20.04.6安装新的linux内核5.4.293 | 并增加一个系统调用 | 证书问题如何解决

参考文章&#xff1a;linux添加系统调用【简单易懂】【含32位系统】【含64位系统】_64位 32位 系统调用-CSDN博客 安装新内核 1. 在火狐下载你需要的版本的linux内核压缩包 这里我因为在windows上面下载过&#xff0c;配置过共享文件夹&#xff0c;所以直接复制粘贴通过共享文…

[Java实战]Spring Boot 3 整合 Ehcache 3(十九)

[Java实战]Spring Boot 3 整合 Ehcache 3&#xff08;十九&#xff09; 引言 在微服务和高并发场景下&#xff0c;缓存是提升系统性能的关键技术之一。Ehcache 作为 Java 生态中成熟的内存缓存框架&#xff0c;其 3.x 版本在性能、功能和易用性上均有显著提升。本文将详细介绍…

LlamaIndex 第九篇 Indexing索引

索引概述 数据加载完成后&#xff0c;您将获得一个文档对象(Document)列表&#xff08;或节点(Node)列表&#xff09;。接下来需要为这些对象构建索引(Index)&#xff0c;以便开始执行查询。 索引&#xff08;Index&#xff09; 是一种数据结构&#xff0c;能够让我们快速检索…

【问题排查】easyexcel日志打印Empty row!

问题原因 日志打印​​I/O 操作开销​&#xff08;如 Log4j 的 FileAppender&#xff09;会阻塞业务线程&#xff0c;直到日志写入完成&#xff0c;导致接口响应变慢 问题描述 在线上环境&#xff0c;客户反馈导入一个不到1MB的excel文件&#xff0c;耗时将近5分钟。 问题排…

代码随想录第51天|岛屿数量(深搜)、岛屿数量(广搜)、岛屿的最大面积

1.岛屿数量&#xff08;深搜&#xff09; ---》模板题 版本一写法&#xff1a;下一个节点是否能合法已经判断完了&#xff0c;传进dfs函数的就是合法节点。 #include <iostream> #include <vector> using namespace std;int dir[4][2] {0, 1, 1, 0, -1, 0, 0, -…

Made with Unity | 从影视到游戏:《鱿鱼游戏》IP 的边界拓展

优质IP的跨媒体开发潜力不可限量。以现象级剧集《鱿鱼游戏》为例&#xff0c;Netflix旗下游戏工作室Boss Fight在第二季开播前夕推出的手游《Squid Game: Unleashed》&#xff0c;一经发布便横扫全球107个国家和地区的App Store免费游戏榜首。 这款多人派对大逃杀游戏完美还原…

allure 报告更改标题和语言为中文

在网上看到好多谈到更改allure 的标题设置都很麻烦&#xff0c;去更改JSON文件 其实可以有更简单的办法&#xff0c;就是在生成报表时增加参数 使用allure --help 查看&#xff1a; --lang, --report-language 设置报告的语言&#xff0c;默认是应用 The report language. …

HGDB索引膨胀的检查与处理思路

文章目录 环境文档用途详细信息 环境 系统平台&#xff1a;Linux x86-64 Red Hat Enterprise Linux 7 版本&#xff1a;4.5.8 文档用途 本文档主要介绍HGDB索引膨胀的定义、产生的原因、如何检查以及遇到索引膨胀如何处理&#xff08;包括预防和解决&#xff09; 详细信息 …

【Python CGI编程】

Python CGI&#xff08;通用网关接口&#xff09;编程是早期Web开发中实现动态网页的技术方案。以下是系统化指南&#xff0c;包含核心概念、实现步骤及安全实践&#xff1a; 一、CGI 基础概念 1. 工作原理 浏览器请求 → Web服务器&#xff08;如Apache&#xff09; → 执行…

数据库故障排查指南:从入门到精通

1. 常见数据库故障类型 1.1 连接故障 数据库连接超时连接池耗尽网络连接中断认证失败1.2 性能故障 查询执行缓慢内存使用过高CPU使用率异常磁盘I/O瓶颈1.3 数据故障 数据不一致数据丢失数据损坏事务失败2. 故障排查流程 2.1 初步诊断 -- 检查数据库状态SHOW STATUS;SHOW PRO…

conda创建环境常用命令(个人用)

创建环境 conda create --name your_project_name创建环境 ---- 指定环境python版本 conda create --name your_project_name python3.x环境列表 conda env list激活环境 conda activate your_project_name退出环境 conda deactivate环境列表 #使用conda命令 conda list …

PCL 绘制二次曲面

文章目录 一、简介二、实现代码三、实现效果一、简介 这里基于二次曲面的公式: z = a 0 + a 1 x + a 2 y + a