在Vue.js中,可以通过以下步骤从b-modal中的表中获取ID并显示在页面上:
<template>
<div>
<b-modal ref="myModal" @ok="handleOk">
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<!-- 其他表头 -->
</tr>
</thead>
<tbody>
<tr v-for="item in items" :key="item.id">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<!-- 其他表格内容 -->
</tr>
</tbody>
</table>
</b-modal>
<button @click="openModal">打开模态框</button>
<p>选中的ID:{{ selectedId }}</p>
</div>
</template>
selectedId
,用于存储选中的ID:data() {
return {
selectedId: null,
items: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
// 其他数据项
]
};
},
openModal
,用于打开模态框,并在模态框的确认按钮事件handleOk
中获取选中的ID:methods: {
openModal() {
this.$refs.myModal.show();
},
handleOk() {
// 获取选中的ID
const selectedRow = document.querySelector('tr.selected');
if (selectedRow) {
this.selectedId = selectedRow.querySelector('td:first-child').textContent;
}
this.$refs.myModal.hide();
}
}
tr.selected {
background-color: yellow;
}
这样,当点击"打开模态框"按钮时,会弹出一个模态框,其中包含一个表格。当用户在表格中选择一行时,该行的ID会被获取并显示在页面上的<p>
标签中。
领取专属 10元无门槛券
手把手带您无忧上云