Vue?3?表單與后端數(shù)據(jù)交互之查詢并回顯數(shù)據(jù)步驟流程
前言
對(duì)應(yīng)的回顯數(shù)據(jù)異常,可回頭看這篇:在使用uni-data-select插件時(shí)通過(guò)接口獲取值賦給localdata失敗的問(wèn)題解決方案
以下是實(shí)戰(zhàn)中抽離的Demo
核心邏輯和步驟流程概述:
- 前端添加“查詢”按鈕:在表單中輸入箱號(hào),點(diǎn)擊“查詢”按鈕后,通過(guò)調(diào)用后端接口獲取數(shù)據(jù)
- 后端處理查詢請(qǐng)求:后端根據(jù)傳入的查詢數(shù)據(jù)庫(kù),返回查詢到的數(shù)據(jù)
- 前端回顯查詢結(jié)果:前端接收到查詢結(jié)果后,將數(shù)據(jù)填充到表單中,用戶可以在表單中查看或編輯數(shù)據(jù)
1. 前端
前端界面包含一個(gè)輸入框(用于輸入箱號(hào))和一個(gè)“查詢箱號(hào)”按鈕。點(diǎn)擊按鈕時(shí),會(huì)通過(guò)后端接口查詢箱號(hào)相關(guān)數(shù)據(jù),并將查詢結(jié)果回顯到表單中
<template> <Dialog :title="dialogTitle" v-model="dialogVisible" width="70%" max-height="30%"> <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" v-loading="formLoading" > <el-row> <!-- 輸入箱號(hào) --> <el-col :span="4"> <el-form-item label="集裝箱箱號(hào)" prop="containerNumber"> <el-input v-model="formData.containerNumber" placeholder="請(qǐng)輸入箱號(hào)" clearable /> </el-form-item> </el-col> <!-- 輸入箱屬 --> <el-col :span="4"> <el-form-item label="箱屬" prop="containerOwner"> <el-input v-model="formData.containerOwner" placeholder="箱屬" clearable /> </el-form-item> </el-col> <!-- 查詢按鈕 --> <el-col :span="4"> <el-button type="primary" @click="searchBoxData">查詢箱號(hào)</el-button> </el-col> </el-row> <!-- 其他表單項(xiàng) --> </el-form> </Dialog> </template> <script setup> import { ref } from 'vue' import GoodsLogApi from '@/api/GoodsLogApi' const formData = ref({ containerNumber: '', containerOwner: '', // 其他字段 }) const formRules = ref({ // 表單驗(yàn)證規(guī)則 }) const formLoading = ref(false) const dialogVisible = ref(false) const dialogTitle = ref('') // 打開(kāi)彈窗方法 const open = async (type, id = null) => { dialogVisible.value = true dialogTitle.value = type === 'create' ? '創(chuàng)建' : '編輯' resetForm() if (id) { formLoading.value = true try { formData.value = await GoodsLogApi.getGoodsLog(id) } finally { formLoading.value = false } } } // 重置表單數(shù)據(jù) const resetForm = () => { formData.value = { containerNumber: '', containerOwner: '', // 其他字段 } } // 查詢箱號(hào)數(shù)據(jù) const searchBoxData = async () => { if (!formData.value.containerNumber) { return this.$message.warning('請(qǐng)輸入箱號(hào)') } formLoading.value = true try { // 向后端請(qǐng)求箱號(hào)數(shù)據(jù) const boxData = await GoodsLogApi.queryBoxData(formData.value.containerNumber) if (boxData) { // 將查詢結(jié)果填充到表單 formData.value.containerOwner = boxData.containerOwner || '' // 例如填充箱屬 // 根據(jù)返回的其他字段繼續(xù)填充表單數(shù)據(jù) formData.value.containerNumber = boxData.cntr || '' // 例如填充箱號(hào) } else { this.$message.warning('未找到相關(guān)箱號(hào)數(shù)據(jù)') } } catch (error) { this.$message.error('查詢失敗') } finally { formLoading.value = false } } </script>
前端調(diào)用后端查詢箱號(hào)數(shù)據(jù)接口,并返回查詢結(jié)果。
// api/GoodsLogApi.js import request from '@/utils/request' export default { // 根據(jù)箱號(hào)查詢數(shù)據(jù) queryBoxData(containerNumber) { return request({ url: `/api/goods-log/query-box`, method: 'get', params: { containerNumber } }) } }
2. 后端
后端接口接收箱號(hào)作為請(qǐng)求參數(shù),查詢數(shù)據(jù)庫(kù)相關(guān)數(shù)據(jù),并將數(shù)據(jù)返回給前端
@RestController @RequestMapping("/api/goods-log") public class GoodsLogController { @Autowired private GoodsLogService goodsLogService; // 查詢箱號(hào)數(shù)據(jù) @GetMapping("/query-box") public ResponseEntity<Map<String, Object>> queryBoxData(@RequestParam String containerNumber) { Map<String, Object> boxData = goodsLogService.queryBoxData(containerNumber); if (boxData == null) { return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); // 如果未找到數(shù)據(jù) } return ResponseEntity.ok(boxData); // 返回查詢結(jié)果 } }
以及
在服務(wù)層通過(guò) SQL 查詢箱號(hào)數(shù)據(jù),并返回查詢到的結(jié)果
@Service public class GoodsLogService { @Autowired private JdbcTemplate jdbcTemplate; // 使用 JdbcTemplate 執(zhí)行 SQL 查詢 public Map<String, Object> queryBoxData(String containerNumber) { String sql = "SELECT * " + "FROM xxx WHERE CNTR = ?"; // 執(zhí)行查詢并返回結(jié)果 List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, containerNumber); // 如果找到數(shù)據(jù),返回第一個(gè)結(jié)果 if (result != null && !result.isEmpty()) { return result.get(0); } return null; // 如果沒(méi)有找到數(shù)據(jù),返回 null } }
3. 總結(jié)
- 前端輸入箱號(hào):用戶在表單中的 containerNumber 輸入框中輸入箱號(hào)
- 點(diǎn)擊查詢按鈕:用戶點(diǎn)擊“查詢箱號(hào)”按鈕,觸發(fā) searchBoxData 方法
- 請(qǐng)求后端接口:前端通過(guò) GoodsLogApi.queryBoxData 向后端請(qǐng)求箱號(hào)數(shù)據(jù)
- 后端查詢數(shù)據(jù)庫(kù):后端通過(guò) SQL 查詢數(shù)據(jù)庫(kù),獲取箱號(hào)的相關(guān)數(shù)據(jù)
- 回顯數(shù)據(jù):如果查詢到數(shù)據(jù),后端將數(shù)據(jù)返回給前端,前端將數(shù)據(jù)回顯到表單中的各個(gè)字段(例如 containerOwner、containerNumber 等)
實(shí)戰(zhàn)截圖如下:
到此這篇關(guān)于Vue 3 表單與后端數(shù)據(jù)交互:查詢并回顯數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Vue 查詢并回顯數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Avue實(shí)現(xiàn)動(dòng)態(tài)查詢與數(shù)據(jù)展示的示例代碼
- vue中輕量級(jí)模糊查詢fuse.js使用方法步驟
- vue中el表單的簡(jiǎn)單查詢方法
- 5種vue模糊查詢的方法總結(jié)
- vue+element?ui表格添加多個(gè)搜索條件篩選功能(前端查詢)
- vue中wangEditor的使用及回顯數(shù)據(jù)獲取焦點(diǎn)的方法
- Vue前端如何實(shí)現(xiàn)與后端進(jìn)行數(shù)據(jù)交互
- vue.js前后端數(shù)據(jù)交互之提交數(shù)據(jù)操作詳解
- 詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource
相關(guān)文章
vue實(shí)現(xiàn)動(dòng)態(tài)監(jiān)測(cè)元素高度
這篇文章主要介紹了vue實(shí)現(xiàn)動(dòng)態(tài)監(jiān)測(cè)元素高度方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue2之簡(jiǎn)易的pc端短信驗(yàn)證碼的問(wèn)題及處理方法
這篇文章主要介紹了vue2之簡(jiǎn)易的pc端短信驗(yàn)證碼的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Vue使用v-viewer插件實(shí)現(xiàn)圖片預(yù)覽和縮放和旋轉(zhuǎn)等功能(推薦)
v-viewer是一個(gè)基于viewerjs封裝的vue圖片預(yù)覽組件,有預(yù)覽縮放拉伸旋轉(zhuǎn)切換拖拽等功能,支持配置化,這篇文章主要介紹了Vue使用v-viewer插件實(shí)現(xiàn)圖片預(yù)覽和縮放和旋轉(zhuǎn)等功能,需要的朋友可以參考下2023-02-02基于Vite2.x的Vue 3.x項(xiàng)目的搭建實(shí)現(xiàn)
這篇文章主要介紹了基于Vite2.x的Vue 3.x項(xiàng)目的搭建實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Vue3+Vite如何解決“找不到模塊“@/components/xxx.vue”或其相應(yīng)的類(lèi)型聲明ts(2307)”
在Vue項(xiàng)目中使用Vite時(shí),可能因版本差異而需修改tsconfig.app.json而非tsconfig.json以解決編譯錯(cuò)誤,本文分享個(gè)人解決經(jīng)驗(yàn),供參考2024-10-10vue實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞的兩種方式
這篇文章主要介紹了vue頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞的兩種方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09Vue使用el-tree 懶加載進(jìn)行增刪改查功能的實(shí)現(xiàn)
這篇文章主要介紹了Vue使用el-tree 懶加載進(jìn)行增刪改查,以懶加載的形式展示,目錄根據(jù)需求需要有新增 編輯 刪除 操作以及操作后的刷新樹(shù)結(jié)構(gòu),具體實(shí)現(xiàn)代碼跟隨小編一起看看吧2021-08-08