您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關vue+nodejs獲取多個表數據的方法是什么,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
將用戶及圖像聯系在一起
修改關聯的時候,前端向后端傳入array[number],后端存為字符串
這時在前端獲取數據時,需要循環處理為數字數組
<template> <div> <el-table :data="state.tableData" border > <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" > <img :src="'http://localhost:3000//' + item.path" /> </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" 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+nodejs獲取多個表數據的方法是什么了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。