首页 > 编程语言 >vue 商品sku添加,笛卡尔算法,商品添加。动态生成table,table添加值后 再生成的table 不改变table之前输入的值

vue 商品sku添加,笛卡尔算法,商品添加。动态生成table,table添加值后 再生成的table 不改变table之前输入的值

时间:2024-04-22 11:55:29浏览次数:13  
标签:const title value 生成 item 添加 dragData table


 

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>快速入门</title>
        <!-- 引入组件库 -->
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <style type="text/css">
            .Multiple-specifications {
                margin: 18px 0 0 0;
            }

            .Specification-list {
                margin: 18px 0 0 0;
            }

            .img_marsk {
                width: 80px;
                height: 80px;
                border-radius: 5px;
                background-color: rgba(0, 0, 0, 0.7);
                position: absolute;
                z-index: 999999;
                top: 0px;
                left: 0;
                display: flex;
                justify-content: center;
                align-items: center;
            }

            .checkout_icon {
                padding: 3px;
                display: inline-block;
                font-size: 16px;
                margin-right: 10px;
                ;
                text-align: center;
                position: relative;
                border-radius: 2px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <div>
                <div>
                    <!-- 多规格 -->
                    <div class="Multiple-specifications">
                        <el-card title="多规格" style="border-radius: 10px;">
                            <div style="display: flex;flex-flow: row wrap;">
                                <div v-for="(item, index) in dragData" :key="index">
                                    <div label="规格名" :rules="{required: true, message: '请输入规格名',trigger: 'blur'}">
                                        <div>
                                            <el-input v-model="item.title" placeholder="规格名" style="width:200px"
                                                @change="makenewTabel(item, 'ruleName', '')" ></el-input>
                                            <el-button @click="deletRow(index, item)">删除</el-button>
                                        </div>
                                    </div>
                                    <div label="规格值" :rules="{required: true, message: '请输入规格值',trigger: 'blur'}">
                                        <div v-for="(it, ix) in item.num" :key="ix" style="margin-left: 20px;">
                                            <div>
                                                <el-input v-model="it.value" placeholder="规格值" style="width:200px"
                                                    @change="makenewTabel(item, 'value', it)" ></el-input>
                                                <el-button @click="deletNum(ix, it, item, index)">删除</el-button>
                                            </div>
                                        </div>
                                    </div>
                                    <div @click="addSku(index)">+添加规格值</div>
                                </div>
                            </div>
                            <div @click="addOtherSku">+添加其他规格
                            </div>
                        </el-card>
                    </div>
                    <!-- 规格列表  -->
                    <div class="Specification-list">
                        <el-card title="规格列表" style="border-radius: 10px;">
                            <el-table :data="duoTableData">
                                <el-table-column v-for="(val, key) in columns2" :key="key" :prop="val.dataIndex" :label="val.title">
                                    <template slot-scope="scope">
                                        <el-input placeholder="请输入" v-model="scope.row[val.dataIndex]" @blur="emitTableData"></el-input>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-card>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入vue mete -->
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.bootcss.com/vue/2.6.11/vue.js"></script>
    <!-- import JavaScript -->
    <script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script>
        new Vue({
            el: '#app',

            data() {
                return {
                    tableData:[],
                    duoTableData: [],
                    dragData: [],
                    columns2: [],
                    columns: [],
                    specificationListdata: [],
                }
            },
            created() {
                this.initDrag()
                this.columns = [
                    {
                        title: `售价(US$})`,
                        dataIndex: 'salesPrice',
                    },
                    {
                        title: `原价(US$})`,
                        dataIndex: 'originalPrice',
                    },
                    {
                        title: '重量',
                        width: '180',
                    },
                    {
                        title: '库存数量',
                        dataIndex: 'goodsStock',
                    },
                    {
                        title: 'SKU(货号)',
                        dataIndex: 'skuNo',
                    },
                    {
                        title: '条形码',
                        dataIndex: 'barCode',
                    },
                    {
                        title: '操作',
                        dataIndex: 'action',
                    }
                ]
                    this.resetColumns()
            },
            methods: {
                // 重置表头
                /***
                 * dragData的默认数据
                 */
                initDrag() {
                    this.dragData = this.tableData.length > 0 ? JSON.parse(JSON.stringify(this.tableData)) : [{
                        title: '',
                        oldTitle: '',
                        num: [{
                            value: '',
                            oldValue: ''
                        }]
                    }]
                    // 如果是新增取消确定 // 或者单规格,就要清空表头and 表格数据
                        // 把old 也要清空
                        this.duoTableData = []
                },
                /**
                 * 得到列
                 */
                resetColumns() {
                    const rules = []
                    for (var j = 0; j < this.dragData.length; j++) {
                        const obj = {}
                        obj.title = this.dragData[j].title
                        obj.dataIndex = `newHead${obj.title}`
                        rules.push(obj)
                    }
                    const columns = [...this.columns]
                    columns.splice(1, 0, ...rules)
                    this.columns2 = columns
                },
                async deletRow(index, item) {
                    if (item.title) {
                        const ret = await this.$confirm(`确定要删除 ${item.title} 规格?`)
                        if (!ret) {
                            return false
                        }
                    }
                    this.dragData.splice(index, 1)
                    this.tableData = this.dragData
                    this.makenewTabel(item, 'deleteRule', index)
                },
                async deletNum(index, ix, item, iii) {
                    if (item.num < 1) {
                        this.$message.error('必须有一个规格值', 2)
                        return false
                    }
                    if (ix.value) {
                        let title = `确定要删除 ${item.title} 规格中的 ${ix.value}?`
                        if (!item.title) {
                            title = `确定要删除 ${ix.value} 规格值?`
                        }
                        const ret = await this.$confirm(title)
                        if (!ret) {
                            return false
                        }
                    }
                    this.dragData[iii].num.splice(index, 1)
                    this.makenewTabel(item, 'deleteRule', ix)
                },
                addSku(index) {
                    this.dragData[index].num.push({
                        value: '',
                        oldValue: ''
                    })
                },
                addOtherSku() {
                    this.dragData.push({
                        title: '',
                        oldTitle: '',
                        num: [{
                            value: '',
                            oldValue: ''
                        }]
                    })
                },
                /** 按规格得到笛卡尔乘积列表 handleList */
                getDecareList() {
                    const ret = []
                    if (this.dragData.length === 0) {
                        return ret
                    } else if (this.dragData.length === 1) {
                        for (var m = 0; m < this.dragData[0].num.length; m++) {
                            const e = {}
                            e[`newHead${this.dragData[0].title}`] = this.dragData[0].num[m].value
                            e['weightUnit'] = 'g'
                            e['goodsStock'] = 0
                            ret.push(e)
                        }
                        return ret
                    }
                    // 展开table中的数据
                    const arr1 = this.dragData.map((item) => {
                        const res = []
                        item.num.forEach(v => {
                            res.push({
                                value: v.value,
                                ruleName: item.title
                            })
                        })
                        return res
                    })
                    // 构造笛卡尔乘积
                    const r = arr1.reduce((col, row) => {
                        const res = []
                        col.forEach(c => {
                            row.forEach(r => {
                                const t = Array.isArray(c) ? [...c] : [`#${c.ruleName}#${c.value}`]
                                t.push(`#${r.ruleName}#${r.value}`)
                                res.push(t)
                            })
                        })
                        return res
                    })
                    const pattern = new RegExp('#([^#]+)#(.*)')
                    let match = []

                    for (var i = 0; i < r.length; i++) {
                        const e = {}
                        for (var k = 0; k < r[i].length; k++) {
                            match = pattern.exec(r[i][k])
                            if (match) {
                                e[`newHead${match[1]}`] = match[2]
                                e['weightUnit'] = 'g'
                                e['goodsStock'] = 0
                            }
                        }
                        ret.push(e)
                    }
                    return ret
                },
                /*** 规格生成  */
                makenewTabel(item, type, value) {
                    this.resetColumns()
                    // 深拷贝一个值
                    const copyListData = JSON.parse(`${JSON.stringify(this.duoTableData)}`)
                    // 得到笛卡尔列
                    const specificationListdata = this.getDecareList()
                    const oldDataList = {}
                    let speckey = []
                    // 原值
                    let key = ''
                    let title = ''
                    // 得到规格名用于data中旧的规格信息
                    const ruleNames = this.dragData.map(table => {
                        return `newHead${table.title}`
                    })
                    copyListData.forEach(data => {
                        speckey = []
                        this.dragData.forEach(table => {
                            // 值变化
                            title = `newHead${table.title}`
                            key = data[title]
                            if (!key) {
                                if (type === 'value') {
                                    data[title] = value.value
                                    key = value.value
                                }
                            }
                            if (type === 'value' && table === item && key === value.oldValue) {
                                data[`newHead${table.title}`] = value.value
                                key = value.value
                            } else {
                                if (type === 'ruleName' && table === item) {
                                    data[`newHead${table.title}`] = data[`newHead${table.oldTitle}`]
                                    key = data[`newHead${table.oldTitle}`]
                                }
                            }
                            speckey.push(key)
                        })
                        // 删除原来的旧列原来的旧值
                        Object.keys(data).forEach(key => {
                            if (key.startsWith('newHead') && !ruleNames.includes(key)) {
                                delete data[key]
                            }
                        })
                        oldDataList[speckey.join('-')] = data
                    })
                    // 新值
                    const temps = specificationListdata
                    temps.forEach((data, index) => {
                        speckey = []
                        this.dragData.forEach(table => {
                            speckey.push(data[`newHead${table.title}`])
                        })
                        key = speckey.join('-')
                        if (oldDataList[key]) {
                            temps[index] = oldDataList[key]
                        }
                    })
                    this.duoTableData = temps
                    if (type !== 'reset') {
                        if (type === 'value') {
                            value.oldValue = value.value
                        } else {
                            item.oldTitle = item.title
                        }
                    }
                },
                // 每次失去焦点后,把值emit
                emitTableData() {
                    const emitData = {
                        tableData: this.tableData,
                        specificationListdata: this.specificationListdata
                    }
                },
            }

        });
    </script>
</html>

 

标签:const,title,value,生成,item,添加,dragData,table
From: https://www.cnblogs.com/kaijiangyugty/p/18150363

相关文章

  • vue 商品sku,笛卡尔算法,商品添加。动态生成table,table添加值后 再生成的table 不改变t
     <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>快速入门</title><!--引入组件库--><linkrel="stylesheet"href="https://un......
  • 【Java定时任务】浅谈CronTrigger的用法和在线Cron表达式生成网址
    1.CronTrigger的作用CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表。CronTrigger,你可以指定触发的时间表如“每星期五中午”,或“每个工作日9:30时”,甚至“每5分钟一班9:00和10:00逢星期一上午,星期三......
  • Spark SQL 抽样函数 ——TABLESAMPLE 的坑点
    最近需要实现一段SparkSQL逻辑,对数据集进行抽样指定的行数。由于数据集较大,刚开始的逻辑是,取窗口函数随机排序后row_number的前n行。但运行速度较慢,所以想起了TABLESAMLE函数,支持直接取Rows,尝试后发现速度特别快,基本上几秒内就完成对亿级数据的采样。所以好奇就去查......
  • sublime text添加自定义代码片段
    效果展示建立步骤如图新建代码片段填写内容xml的标签形式。tabTrigger和vscode内的prefix一样,就是触发提示的代码。<![CDATA[内部既是我们要的代码。${1:}表示光标首先停在的位置${1:xxx}即表示默认文字为xxx,如果是${2:}则按一下tab就跳到2的位置。description表......
  • 自动生成接口文档
    自动生成接口文档​ RESTframework可以自动帮我们生成接口文档。​ 接口文档以网页的形式呈现​ 自动接口文档能生成的是继承自APIView及其子类的视图使用步骤安装依赖​ RESTframework生成接口文档需要coreapi库的支持pipinstallcoreapi设置接口文档访问路径​ 需......
  • C++ STL -- HashTable
    HashTable一般常用的unordered_set、unordered_map都是基于哈希表实现的,哈希表主要需要注意的是哈希冲突,迭代器等基础哈希映射使用哈希函数将键映射到索引的数据结构。即将原始数组索引通过哈希函数映射到一个哈希值上,从而将一个大范围索引,减小到一个小的固定范围哈希冲突......
  • 比较两个table是否相同
    两个列表是否相同functionIsTwoListValueSame(list1,list2)localcnt1=#list1localcnt2=#list2ifcnt1~=cnt2thenreturnfalseendlocaltab={}localsameCnt=0fori=1,cnt1dotab[list1[i]]=trueendfori......
  • Random 项目总结 -08 生成WORD 报告
    usingSystem.Windows.Forms;usingSystem.Xml;usingMsword=Microsoft.Office.Interop.Word;privatevoiddy_Click(objectsender,EventArgse){Formf1=dy.FindForm();intgx=f1.Location.X;intgy=f1.Location.Y;......
  • Random 项目总结 -06 定时器、 随机数,截图,生成WORD报告 (result完整)
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Xml;usingMsword=Microsoft.Office.......
  • vue3 优化ai生成的手写签名
    下面是baiduai生成的代码:在Vue3中实现手写签名功能,可以使用canvas元素来创建一个绘图区域,并监听鼠标事件来实现签名的记录。以下是一个简单的例子:vue<template><div><canvasref="signatureCanvas"@mousedown="startSigning"@mousemove="updat......