简体   繁体   English

Vue JS - 在 for 循环中修改对象而不是实时更新

[英]Vue JS - Modifying object in for loop not updating live

So I have a loop of objects and I want to set flags on them so that I can change their "state" to "being edited" "newly created" etc and show the proper fields depending on the state.所以我有一个对象循环,我想在它们上设置标志,以便我可以将它们的“状态”更改为“正在编辑”“新创建”等,并根据状态显示正确的字段。 Here is my code:这是我的代码:

<tr v-for="application in editForm.applicationsSelected" :key="application.id">
    <!-- Name -->
    <td style="vertical-align: middle;" v-if="!application.fresh">
        {{ application.name }}
    </td>
    <td style="vertical-align: middle;" v-if="application.fresh">
        <select class="form-control" name="application_id" v-model="application.id">
            <option value="" selected disabled>Select One</option>
            <option v-for="application in applications" v-bind:value="application.id">{{ application.name }}</option>
        </select>
    </td>

    <!-- Appliction User ID -->
    <td style="vertical-align: middle;"  v-if="!application.isEditing && !application.fresh">
        {{ application.pivot.application_user_id }}
    </td>
    <td style="vertical-align: middle;"  v-if="application.isEditing || application.fresh">
        <input type="text" class="form-control" id="application_user_id" v-model="application.pivot.application_user_id" />
    </td>

    <!-- Edit User Application -->
    <td style="vertical-align: middle;" v-if="application.isEditing">
        <a class="action-link" @click="updateUserApplication(application, editForm.id)">
            Save
        </a>
    </td>
    <td style="vertical-align: middle;" v-if="application.fresh">
        <a class="action-link" @click="attachUserApplication(application, editForm.id)">
            Save
        </a>
    </td>
    <td style="vertical-align: middle;" v-if="!isEditing && !application.fresh">
        <a class="action-link" @click="application.isEditing = true">
            Edit
        </a>
    </td>
</tr>

The important part is the edit button on the bottom.重要的部分是底部的编辑按钮。 I want to set the objects "isEditing" flag to true and then show the input fields for that row.我想将对象“isEditing”标志设置为 true,然后显示该行的输入字段。 However, its simply not working.但是,它根本不起作用。 It will change the state of the application but not update the code till i close the modal and open it back up again (which forces a refresh of the application objects).它将更改应用程序的状态,但不会更新代码,直到我关闭模式并再次打开它(这会强制刷新应用程序对象)。

Also I plan on changing this to one property that takes on string "states" instead of changing multiple flags, however, for now I can't get the basic step down.此外,我计划将其更改为一个接受字符串“状态”而不是更改多个标志的属性,但是,现在我无法获得基本的降级。 Why is it that changing application.isEditing to true on click isn't switching which fields i see?为什么单击时将 application.isEditing 更改为 true 不会切换我看到的字段?

Until version 3 of Vue arrives, you need to be careful about dynamically adding properties to objects.在 Vue 版本 3 到来之前,您需要小心为对象动态添加属性。 Vue cannot detect when this happens and, as such, changes to the newly added property will not be reactively reflected in the DOM. Vue 无法检测到何时发生这种情况,因此,对新添加属性的更改不会反应性地反映在 DOM 中。

The primary ways of working around this are using Vue.set (or this.$set in a component) or adding the property before the object is added to Vue.解决此问题的主要方法是使用Vue.set (或组件中的this.$set )或在将对象添加到 Vue之前添加属性。

It appears OP solved the problem using Vue.set combined with a method event handler.看来 OP 使用 Vue.set 结合方法事件处理程序解决了这个问题。

<a class="action-link" @click="onEdit(application)">

And in the code:在代码中:

methods: {
  onEdit(application){
    this.$set(application, 'isEditing', true)
  }  
}

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

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