简体   繁体   English

v-for table 动态 vuejs3

[英]v-for table dynamically vuejs3

I´m doin a axios.get() to route laravel, that this route it´s ok, return all my data in json, and i´m tryiying to fill my table.我正在使用axios.get()来路由 laravel,这条路由没问题,以 json 格式返回我的所有数据,我正在尝试填满我的表格。 I have a array that it´s filled with axios get .我有一个数组,里面装满了axios get

if i do console.log() with my array, have data.如果我用我的数组做 console.log(),有数据。 but if i do v-for with this array... i can´t show data.但是如果我用这个数组做 v-for ......我不能显示数据。

<template>
  <div class="tabla offset-md-1">
        <table class="table table-hover table-striped">
            <thead>
                <th v-for="column in columns">
                    {{ column.label }}
                </th>
            </thead>
            <tbody>
                <tr v-for="data in treatment" v-bind:value="index">
                    <td>{{ data.id }}</td>
                    <td>{{ data.nombre_tratamiento }}</td>
                    <td>{{ data.compania.nombre }}</td>
                    <td>{{ data.precio_sesion }}</td>
                    <td><a href="#" class="btn btn-info">Edit</a></td>
                    <td><a href="#" class="btn btn-info">Remove</a></td>
                </tr>
            </tbody>
        </table>
        <div class="alert alert-success d-none" role="alert" id="correcto"></div>
        <div class="alert alert-danger d-none" role="alert" id="error"></div>
    </div>
</template>

<script>
    export default {
        created: function () {
            axios.get('/treatmentCompany/getAllTreatmentCompany')
            .then((response) => {
                this.treatment = response.data;
            })
            .catch(error => console.log(error))
        },
        methods: {
            remove(event) {
                if(confirm("Do you really want to delete?")){

                    axios.post('/treatmentCompany/destroy', {treatment: event}).then(
                        (data) => {
                            this.treatment = data
                            window.location.replace(response.data);
                        }
                    ).catch(error => console.log(error))
                }
            },
        },
        data(){
            return {
                searchTerm: '',
                treatment: [],
                
                columns: [
                    {
                        label: 'ID',
                        field: 'id',
                    },
                    {
                        label: 'Name',
                        field: 'nombre_tratamiento',
                    },
                    {
                        label: 'Price',
                        field: 'precio_sesion',
                    },
                    {
                        label: 'Edit',
                        field: 'edit'
                    },
                    {
                        label: 'Remove',
                        field: 'remove'
                    },
                ],                
            };
        },
    };
</script>

this it´sa result from my query这是我查询的结果

[{"id":5,"nombre":"aaa","apellidos":"ggggaas","tlf1":222,"tlf2":2222,"created_at":"2022-06-08T06:44:09.000000Z","updated_at":"2022-06-14T11:12:12.000000Z"},{"id":6,"nombre":"ddd","apellidos":"ddd","tlf1":2222,"tlf2":2222,"created_at":"2022-06-13T15:02:28.000000Z","updated_at":"2022-06-13T15:02:28.000000Z"}]

i´m new with vuejs 3, with vuejs 2 i can to do this.我是 vuejs 3 的新手,使用 vuejs 2 我可以做到这一点。

Thanks for read me and your answers感谢您阅读我和您的答案

You used a wrong syntax for for-loop , simple it's like this in Vue 2 and 3:你为for-loop使用了错误的语法,在 Vue 2 和 3 中很简单:

<div v-for="(item, i) in items" :key="i" >
{{item}}
</div>

You should use i as the array index for key prop, then Vue will be able to update the DOM if the array being changed.您应该使用i作为key prop 的数组索引,然后如果数组被更改,Vue 将能够更新 DOM。

And your case would be like this:你的情况是这样的:

<template>
  <div class="tabla offset-md-1">
    <table class="table table-hover table-striped">
      <thead>
        <th v-for="(column, i) in columns" :key="i">
          {{ column.label }}
        </th>
      </thead>
      <tbody>
        <tr v-for="(data, i) in treatment" :key="i">
          <td>{{ data.id }}</td>
          <td>{{ data.nombre_tratamiento }}</td>
          <td>{{ data.compania.nombre }}</td>
          <td>{{ data.precio_sesion }}</td>
          <td><a href="#" class="btn btn-info">Edit</a></td>
          <td><a href="#" class="btn btn-info">Remove</a></td>
        </tr>
      </tbody>
    </table>
    <div class="alert alert-success d-none" role="alert" id="correcto"></div>
    <div class="alert alert-danger d-none" role="alert" id="error"></div>
  </div>
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM