我用Vue.js,Vuetify(应用程序的客户端)和Express(应用程序的服务器端)创建了一个节点应用程序,并将数据存储在SQL Server数据库(关系数据库)中。
一切正常,并且在我的开发机器上正在运行,并且客户端,服务器和数据库之间的连接正在工作。
我只是不知道如何在我使用的表中显示“查找数据/关系数据”。
我数据库的3个表:
所以我想显示FKAssetMake和FKAssetModel的AssetMake和AssetModel的 名称 字段 INSTEAD
这是浏览器中我的表格的屏幕截图:
如您所见,将显示FK值,但不显示名称。
要显示的查找表和数据:
Chrome中的数据对象:
我的Vue模板:
<template>
<div id="app">
<v-app id="inspire">
<v-card>
<v-card-title>
Asset (Vehicle/Vessel)
<v-spacer></v-spacer>
<v-text-field
v-model="search"
label="Search"
append-icon="mdi-card-search-outline"
single-line
hide-details
></v-text-field>
</v-card-title>
<v-data-table dense
:headers="headers"
:items="assets"
:search="search"
sort-by="Name"
class="elevation-1"
>
<template v-slot:top>
<v-toolbar flat color="white">
<v-toolbar-title>{{ tabelHeader }}</v-toolbar-title>
<v-spacer></v-spacer>
<v-dialog v-model="dialog" max-width="500px">
<template v-slot:activator="{ on }">
<v-btn color="primary" dark class="mb-2" v-on="on">New Item</v-btn>
</template>
<v-card>
<v-card-title>
<span class="headline">{{ formTitle }}</span>
</v-card-title>
<v-card-text>
<v-container>
<v-row>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.Name" label="Name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.FKMake" label="Make"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.FKModel" label="Model"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.Registration" label="Registration"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.Description" label="Description"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.Year" label="Year"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.Capacity" label="Capacity"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.isVessel" label="Is Vessel"></v-text-field>
</v-col>
</v-row>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" text @click="close">Cancel</v-btn>
<v-btn color="blue darken-1" text @click="save">Save</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-toolbar>
</template>
<template v-slot:item.Action="{ item }">
<v-icon
small
class="mr-2"
@click="editItem(item)"
>
edit
</v-icon>
<v-icon
small
@click="deleteItem(item)"
>
delete
</v-icon>
</template>
</v-data-table>
</v-card>
</v-app>
</div>
</template>
脚本部分:
<script>
import appConfig from '../config/appConfig.js'
const baseURL = "http://" + appConfig.server.ip + ":" + appConfig.server.port
export default {
data: () => ({
search: '',
dialog: false,
tabelHeader: '',
assets: [],
headers: [
{ text: 'Name', value: 'Name'},
{ text: 'Make', value: 'FKMake'},
{ text: 'Model', value: 'FKModel'},
{ text: 'Registration', value: 'Registration'},
{ text: 'Description', value: 'Description' },
{ text: 'Year', value: 'Year' },
{ text: 'Capacity', value: 'Capacity' },
{ text: 'Vessel', value: 'IsVessel' },
{ text: 'Actions', value: 'Action', sortable: false }
],
editedIndex: -1,
editedItem: {
Name: '',
FKMake: -1,
FKModel: -1,
Registration: null,
Year: null,
Capacity: null,
Description: '',
IsVessel: null
},
defaultItem: {
Name: '',
FKMake: -1,
FKModel: -1,
Registration: null,
Year: null,
Capacity: null,
Description: '',
IsVessel: null
}
}),
watch: {
dialog (val) {
val || this.close()
},
},
methods:{
editItem (item) {
this.editedIndex = this.assets.indexOf(item)
this.editedItem = Object.assign({}, item)
this.dialog = true
},
deleteItem (item) {
const index = this.assets.indexOf(item)
var isDelete = confirm('Are you sure you want to delete this item?')
if (isDelete) {
this.deleteAssetMake(item, index)
}
},
close () {
this.dialog = false
setTimeout(() => {
this.editedItem = Object.assign({}, this.defaultItem)
this.editedIndex = -1
}, 300)
},
save () {
if (this.editedIndex > -1) {
Object.assign(this.assets[this.editedIndex], this.editedItem)
this.updateAssetMake(this.editedItem)
}
else {
this.assets.push(this.editedItem)
this.addAssetMake(this.editedItem)
}
this.close()
},
deleteAssetMake (asset, index) {
fetch(baseURL + '/api/asset/' + asset.ID, {
method: 'DELETE'
})
.then(() => {
this.assets.splice(index, 1)
})
},
updateAssetMake (asset) {
fetch(baseURL + '/api/asset/' + asset.ID, {
body: JSON.stringify(asset),
method: 'PUT',
headers: {
'Content-Type': 'application/json'
}
})
.then(() => {
})
},
addAssetMake (asset) {
fetch(baseURL + '/api/asset/', {
body: JSON.stringify(asset),
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
}
},
mounted () {
fetch(baseURL + '/api/asset')
.then(response => response.json())
.then((data) => {
this.assets = data.resultMessage
console.log(data.resultMessage)
})
//.catch(() => console.log('Can’t access response. Blocked by browser?'))
},
computed: {
formTitle () {
return this.editedIndex === -1 ? 'New Item' : 'Edit Item'
},
}
}
</script>
如何显示所引用表的“名称”字段而不是外键。
通过“ Web类型”数据库设计是否有“更好”的方法来完成任务?
答案 0 :(得分:1)
@chans的回答使我处在正确的轨道上。
由于用户将不会在线编辑表中的数据,因此可以更改查询以显示所需的数据(AssetMake.Name和AsseteModel.Name)。
我正在使用带有表单的对话框来进行资产的编辑和添加。然后,我使用 v-select 来显示查找数据,如下所示:
<v-col cols="12" sm="6" md="4">
<v-select v-model="editedItem.FKModel" :items="assetModels" item-text="Name" item-value="ID" label="Model" autocomplete></v-select>
</v-col>
assetModels 包含所有资产模型,并且是在装入事件时从数据库中获取的。
感谢所有参加的人。
答案 1 :(得分:0)
确保api响应对象应具有AssetMake和AssetModel属性,然后对html和css进行以下更改。然后它将按预期工作
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.AssetMake" label="Make"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field v-model="editedItem.AssetModel" label="Model"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
{ text: 'Name', value: 'Name'},
{ text: 'Make', value: 'AssetMake'},
{ text: 'Model', value: 'AssetModel'},
{ text: 'Registration', value: 'Registration'},
{ text: 'Description', value: 'Description' },
{ text: 'Year', value: 'Year' },
{ text: 'Capacity', value: 'Capacity' },
{ text: 'Vessel', value: 'IsVessel' },
{ text: 'Actions', value: 'Action', sortable: false }
],
editedIndex: -1,
editedItem: {
Name: '',
AssetMake: '',
AssetModel: '',
Registration: null,
Year: null,
Capacity: null,
Description: '',
IsVessel: null
},
defaultItem: {
Name: '',
AssetMake: '',
AssetModel: '',
Registration: null,
Year: null,
Capacity: null,
Description: '',
IsVessel: null
}