我想基于vuetify示例但以打字稿实现一个简单的CRUD数据表。 验证示例:https://vuetifyjs.com/en/components/data-tables 在下面的代码中,我设法在表中显示了一些静态数据。因此,我在下面的模板和脚本代码中有一个注释部分和一个未注释的工作部分。需要翻译注释部分以启用CRUD功能。未注释部分仅描述具有静态数据的基本表。任何帮助表示高度赞赏。
<template>
<div class="person-list">
<!-- vuetify crud data need guidance for typescript -->
<!--
<div>
<v-toolbar flat color="white">
<v-toolbar-title>My CRUD</v-toolbar-title>
<v-divider class="mx-2" inset vertical></v-divider>
<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 grid-list-md>
<v-layout wrap>
<v-flex xs12 sm6 md4>
<v-text-field v-model="editedItem.name" label="Dessert name"></v-text-field>
</v-flex>
<v-flex xs12 sm6 md4>
<v-text-field v-model="editedItem.calories" label="Calories"></v-text-field>
</v-flex>
<v-flex xs12 sm6 md4>
<v-text-field v-model="editedItem.fat" label="Fat (g)"></v-text-field>
</v-flex>
<v-flex xs12 sm6 md4>
<v-text-field v-model="editedItem.carbs" label="Carbs (g)"></v-text-field>
</v-flex>
<v-flex xs12 sm6 md4>
<v-text-field v-model="editedItem.protein" label="Protein (g)"></v-text-field>
</v-flex>
</v-layout>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" flat @click="close">Cancel</v-btn>
<v-btn color="blue darken-1" flat @click="save">Save</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-toolbar>
<v-data-table :headers="headers" :items="desserts" class="elevation-1">
<template v-slot:items="props">
<td>{{ props.item.name }}</td>
<td class="text-xs-right">{{ props.item.calories }}</td>
<td class="text-xs-right">{{ props.item.fat }}</td>
<td class="text-xs-right">{{ props.item.carbs }}</td>
<td class="text-xs-right">{{ props.item.protein }}</td>
<td class="justify-center layout px-0">
<v-icon small class="mr-2" @click="editItem(props.item)">edit</v-icon>
<v-icon small @click="deleteItem(props.item)">delete</v-icon>
</td>
</template>
<template v-slot:no-data>
<v-btn color="primary" @click="initialize">Reset</v-btn>
</template>
</v-data-table>
</div>
-->
<!-- simple working table -->
<div>
<v-content>
<v-data-table v-bind:headers="headers" :items="items" class="elevation-1">
<template slot="items" slot-scope="props">
<td>{{ props.item.name }}</td>
<td class="text-xs-right">{{ props.item.calories }}</td>
<td class="text-xs-right">{{ props.item.fat }}</td>
<td class="text-xs-right">{{ props.item.carbs }}</td>
<td class="text-xs-right">{{ props.item.protein }}</td>
<td class="text-xs-right">{{ props.item.sodium }}</td>
<td class="text-xs-right">{{ props.item.calcium }}</td>
<td class="text-xs-right">{{ props.item.iron }}</td>
</template>
</v-data-table>
</v-content>
</div>
</div>
</template>
<script lang="ts">
// https://stackoverflow.com/questions/48715457/vuetifys-data-table-vue-typed-missing-props-value
// https://vuetifyjs.com/en/components/data-tables
import { Component, Vue } from "vue-property-decorator";
@Component
export default class PersonList extends Vue {
public persons: Person[] = [];
dialog = false;
desserts = [];
editedIndex = -1;
editedItem = [
{
name: "",
calories: 0,
fat: 0,
carbs: 0,
protein: 0
}
];
defaultItem = [
{
name: "",
calories: 0,
fat: 0,
carbs: 0,
protein: 0
}
];
get search() {
return "";
}
get pagination() {
return {};
}
/*
computed: {
formTitle () {
return this.editedIndex === -1 ? 'New Item' : 'Edit Item'
}
},
watch: {
dialog (val) {
val || this.close()
}
},
created () {
this.initialize()
},
editItem(item) {
this.editedIndex = this.desserts.indexOf(item);
this.editedItem = Object.assign({}, item);
this.dialog = true;
}
deleteItem(item) {
const index = this.desserts.indexOf(item);
confirm("Are you sure you want to delete this item?") &&
this.desserts.splice(index, 1);
}
close() {
this.dialog = false;
setTimeout(() => {
this.editedItem = Object.assign({}, this.defaultItem);
this.editedIndex = -1;
}, 300);
}
save() {
if (this.editedIndex > -1) {
Object.assign(this.desserts[this.editedIndex], this.editedItem);
} else {
this.desserts.push(this.editedItem);
}
this.close();
}
*/
get headers() {
return [
{
text: "Dessert (100g serving)",
align: "left",
sortable: false,
value: "name"
},
{ text: "Calories", value: "calories" },
{ text: "Fat (g)", value: "fat" },
{ text: "Carbs (g)", value: "carbs" },
{ text: "Protein (g)", value: "protein" },
{ text: "Sodium (mg)", value: "sodium" },
{ text: "Calcium (%)", value: "calcium" },
{ text: "Iron (%)", value: "iron" }
];
}
get items() {
return [
{
value: false,
name: "Frozen Yogurt",
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
sodium: 87,
calcium: "14%",
iron: "1%"
},
{
value: false,
name: "Ice cream sandwich",
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3,
sodium: 129,
calcium: "8%",
iron: "1%"
}
];
}
}
</script>
谢谢