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.pswp.cn/pingmian/81073.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/81073.shtml
英文地址,請注明出處:http://en.pswp.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