index.vue 3.4 KB
Newer Older
1 2 3 4 5 6
<template>
    <div class="container w-auto m-auto bg-gray-200 mb-10">
        
        <div class="p-4">
            
            <span>添加内容</span>
7
            <textarea class="block my-2 w-full" v-model="newContent" maxLength="512"></textarea>
8
            <button class="bg-gray-300 py-2  px-4 rounded hover:bg-gray-400" @click="add"> add</button>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
            <button class="bg-gray-300 py-2  px-4 rounded hover:bg-gray-400 ml-2" @click="search"> search</button>
            


            <div class="w-full mt-4">
                <table class="table-auto w-full bg-gray-200 text-left">
                    <thead>
                        <tr class="h-10 *:border-b *:border-slate-300 *:pl-2 *:bold">
                            <th >ID</th>
                            <th >内容</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="item in list" class="*:h-12 *:border-b *:border-slate-300 *:pl-2 *:hover:bg-gray-300">
24 25 26 27 28
                            <td class="w-[80px]">{{ item._id }}</td>
                            <td>
                                <textarea class="my-2 w-full bg-gray-100 max-h-48 border-gray-300" v-model="item.content" maxLength="512"></textarea>
                            </td>
                            <td class="*:py-1  *:px-4 *:rounded-md *:mr-2 *:text-gray-100 w-[160px]">
29 30 31 32 33 34 35 36 37
                                <button  @click="deleteItem(item)" class="bg-orange-400 hover:bg-orange-500"> 删除</button>
                                <button  @click="updateItem(item)" class="bg-blue-400 hover:bg-blue-500"> 更新</button>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            

38 39 40 41 42 43 44
        </div>
    </div>
    
</template>
<script>
import Config from '../../js/config'
import RepEs from '../../js/repEs'
45 46
import { MessagePlugin } from 'tdesign-vue-next';

47

48
const esClient = new RepEs(Config.getData().data.ext)
49 50 51 52 53 54
export default {
    name: 'contentIndex',
    components: {
    },
    data() {
        return {
55
            newContent: '',
56
            list: []
57 58 59 60
        }
    },
    methods: {
        add () {
61 62 63 64 65 66 67 68 69
            
            esClient.add(this.newContent).then(res=>{
                MessagePlugin.success({ content: '添加成功', placement: 'center' })
                this.newContent = ''
                
                this.$nextTick(() => {
                    this.search()
                })
            })
70 71 72 73 74
        },
        search () {
            esClient.query(this.newContent).then(res => {
                console.info(res)
                this.list = res
75
                
76 77 78 79 80
            })
        },
        updateItem (item) {
            esClient.update(item._id, item.content).then(res => {
                MessagePlugin.success({ content: '更新成功', placement: 'center' })
81 82 83
                this.$nextTick(() => {
                    this.search()
                })            })
84 85 86

        },
        deleteItem (item) {
87
            
88 89
            esClient.delete(item._id).then(res => {
                MessagePlugin.success({ content: '删除成功', placement: 'center' })
90 91 92 93 94 95 96
                this.$nextTick(() => {
                    this.search()
                })            
            }).catch(() => {
                this.$nextTick(() => {
                    this.search()
                })
97 98 99
            })
        },

100 101 102
    }
}
</script>