<template>
<table>
<thead>
<tr>
<th>产品ID</th>
<th>产品名称</th>
<th>价格</th>
<th>库存</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in tableData" :key="index" @click.prevent="showDetails(item)">
<td>{{ item.productId }}</td>
<td>{{ item.productName }}</td>
<td>{{ item.price }}</td>
<td :style="{ color: item.stock < 10? 'red' : 'inherit' }">{{ item.stock }}</td>
</tr>
</tbody>
</table>
</template>
<script>
export default {
data() {
return {
tableData: []
};
},
created() {
// 模拟异步获取数据
setTimeout(() => {
this.tableData = [
{ productId: 1, productName: '产品1', price: 100, stock: 5 },
{ productId: 2, productName: '产品2', price: 200, stock: 15 }
];
}, 1000);
},
methods: {
showDetails(item) {
alert(`产品ID: ${item.productId}\n产品名称: ${item.productName}\n价格: ${item.price}\n库存: ${item.stock}`);
}
}
};
</script>