如何使用laravel 7和vue更新数据

时间:2020-07-16 14:31:40

标签: javascript php laravel vue.js axios

<script>

import Vue from 'vue'
import { Form, HasError, AlertError } from 'vform'

Vue.component(HasError.name, HasError)
Vue.component(AlertError.name, AlertError)

    export default {
        data(){
            return {
                editmode: false,
                staff: {},
                form: new Form([
                    {id : ''},
                    {name : ''},
                    {email: ''},
                    {phone : ''},
                    {type : ''},
                   {password : ''}

                ])
            }
        },

        methods: {

            updateStaff(){
                this.$Progress.start();
                this.form.patch('api/user/'+this.form.id)
                .then(()=>{

                })
                .catch(()=>{
                    this.$Progress.fail();
                });
            },

            editModal(staff){
                this.editmode =true;
                this.form.reset();
                $('#addNew').modal('show');
                this.form.fill(staff);
            },
             newModal(){
                 this.editmode =false;
                this.form.reset();
                $('#addNew').modal('show');
            },



            deleteUser(id){

                swal.fire({
                        title: 'Are you sure?',
                        text: "You won't be able to revert this!",
                        icon: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        confirmButtonText: 'Yes, delete it!'
                        }).then((result) => {
                            //send request to server
                            if (result.value) {
                                    this.form.delete('api/user/'+id)
                                    .then(()=>{

                                            swal.fire(
                                            'Deleted!',
                                            'Staff has been deleted.',
                                            'success'
                                            )

                                        Refresh.$emit('RefreshResult');
                                    })
                                    .catch(()=>{
                                        swal("Failed","Something Went wrong.","warning");
                                    });
                            }


                })

            },

            loadStaff(){
                axios.get("api/user").then(({data})=>(this.staff = data.data));
            },
            createStaff(){
                this.$Progress.start();
                this.form.post('api/user')
                .then(()=>{

                    Refresh.$emit('RefreshResult');
                $('#addNew').modal('hide');

                toast.fire({
                            icon: 'success',
                            title: 'Staff created successfully'
                          })


                this.$Progress.finish();

                })
                .catch(()=>{
                     swal("Failed","Something Went wrong.","warning");
                })



            }

        },
        created() {
            this.loadStaff();
            Refresh.$on('RefreshResult',() => {
                this.loadStaff();
            });
            //setInterval(() => this.loadStaff(), 1000);
        }
    }
</script>

0 个答案:

没有答案