欢迎来到代码驿站!

vue

当前位置:首页 > 网页前端 > vue

详解vue+nodejs获取多个表数据的方法

时间:2022-10-11 12:12:43|栏目:vue|点击:

读取两个表的数据

将用户及图像联系在一起

效果

前端实现

修改关联的时候,前端向后端传入array[number],后端存为字符串

这时在前端获取数据时,需要循环处理为数字数组

<template>
    <div>
        <el-table :data="state.tableData" border style="width: 100%">
            <el-table-column prop="name" label="Name" width="180" />
            <el-table-column prop="relation" label="Relation" width="180" ></el-table-column>
            <el-table-column prop="path" label="Path">
                <template #default="scope">
                    <div v-for="(item, index) in scope.row.path" :key="index" style="display:inline-block">
                        <img
                            :src="'http://localhost:3000//' + item.path"
                            style="width: 100px;height:100px;margin:0 10px"
                        />
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="Operations" width="120">
                <template #default="scope">
                    <el-button
                        type="text"
                        size="small"
                        @click.prevent="edit(scope.row)"
                    >
                        <el-icon>
                            <edit />
                        </el-icon>
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog v-model="state.dialogVisible" width="80%">
            <el-transfer
                v-model="state.rightValue"
                style="text-align: left; display: inline-block"
                filterable
                :titles="['Source', 'Target']"
                :button-texts="['To left', 'To right']"
                :format="{
                noChecked: '${total}',
                hasChecked: '${checked}/${total}',
                }"
                :data="state.data"
            >
                <template #default="{ option }">
                    <span>{{ option.key }} - {{ option.label }}</span>
                </template>
            </el-transfer>
            <p>
                <el-button
                    type="primary"
                    size="medium"
                    @click.prevent="commit"
                >提交
                </el-button>
            </p>
        </el-dialog>
    </div>
</template>
<script lang="ts">
import {defineComponent, reactive} from 'vue'
import {relationlist,uploadorder,editrelation} from '../utils/api'
import {
    ElMessage, ElDialog
} from 'element-plus';
import { Edit } from '@element-plus/icons';
export default defineComponent({
    name : 'relation',
    components:{
        Edit, ElMessage, ElDialog
    },
    setup() {
        const state = reactive({
            tableData:[],
            dialogVisible:false,
            data:[],
            rightValue:[],
            editdata:{}
        })
        const init = function(){
            relationlist().then((res)=>{
                if (res.code === 200) {
                    res.list.forEach((ele)=>{
                        if(ele.relation){
                            ele.relation = ele.relation.split(',')
                            for (let i = 0; i < ele.relation.length; i++) {
                                ele.relation[i] = Number(ele.relation[i]);
                            }
                        }
                    })
                    state.tableData = res.list
                }
            })
        }
        const init1 = function(){
             uploadorder().then(res => {
                if (res.code === 200) {
                    let data = []
                    res.list.forEach(ele => {
                        data.push({
                            key:ele.id,
                            label:ele.name
                        })
                    });
                    state.data = data
                }
            })
        }
        const edit = function(row){
            state.editdata = row;
            state.dialogVisible = true;
            state.rightValue = row.relation||[];
        }
        const commit = function(){
            let data = {
                rightvalue:state.rightValue,
                ...state.editdata
            }
            editrelation(data).then((res)=>{
                if(res.code === 200){
                    ElMessage.success(res.msg)
                    state.dialogVisible = false
                    init() 
                }else{
                    ElMessage.error(res.msg)
                }
            })
        }
        init()
        init1()
        return {
            state,
            edit,
            commit
        }
    }
})
</script>

后端实现

//获取关联列表
router.get('/relationlist',async (req,res,next)=>{
  const result = await db.select(`SELECT * FROM user`)
  for (let i = 0; i < result.length; i++) {
    let ele = result[i];
    let uploadres = await db.select(`SELECT path FROM upload where id in (${ele.relation})`)
    ele.path = uploadres 
  }
  res.send(Success(result));
})
//修改关联列表
router.post('/editrelation',async (req,res,next)=>{
  let {id,rightvalue} = req['body']
  console.log(rightvalue);
  if(!id || !rightvalue){
    res.send(MError('请选择后再提交'))
    return
  }
  const result = await db.update('user', { relation:rightvalue}, ` WHERE id = ${id}`);
  if(result){
    res.send(Success(result))
    return
  }else{  
    res.send(MError('修改失败,请再次尝试'))
    return
  }
})

总结

上一篇:Vue列表页渲染优化详解

栏    目:vue

下一篇:没有了

本文标题:详解vue+nodejs获取多个表数据的方法

本文地址:http://www.codeinn.net/misctech/216093.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有