v-for表动态vuejs3

发布于 2025-02-10 09:01:53 字数 3395 浏览 1 评论 0原文

我要doin a axios.get()将laravel路由,这是可以的,可以返回我的所有数据,然后我正在尝试填充我的表格。我有一个装有Axios Get的数组。

如果我在数组中执行console.log(),请使用数据。但是,如果我对此数组进行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>

这是由我的查询产生的,

[{"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"}]

我是新的vuejs 3,而我可以做到这一点。

感谢您阅读我和您的答案

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. I have a array that it´s filled with axios get.

if i do console.log() with my array, have data. but if i do v-for with this array... i can´t show data.

<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´s a 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.

Thanks for read me and your answers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2025-02-17 09:01:53

您使用了错误的语法,以 for-loop ,简单地在vue 2和vue 2和3:

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

您应该使用i作为 prop的数组索引,然后Vue如果更改数组,Vue将能够更新DOM。

您的情况就是这样:

<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>

You used a wrong syntax for for-loop, simple it's like this in Vue 2 and 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.

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文