Vue.js列表绑定不起作用

时间:2018-05-30 03:14:37

标签: asp.net vue.js

抱歉我的英文。 数据绑定不起作用。 所有数据都正确序列化和显示,但如果我尝试更改某些值 - 没有任何反应。 Klik()方法正常工作,条件正常工作。 请帮忙。

HTML代码

<div id="app">
    <div class="areaInfo " v-for="area in mainObjects" v-on:click="klik(area)">
        <div class="trDiv areaData">
            <div class="tdDiv" v-for="(prop, key) in area" v-if="key != 'ChildData'">
                {{key}}
                <template v-if="key.includes('Start') || key.includes('End') ">
                    {{ ConvertJsonDateString(prop) }}
                </template>
                <template v-else-if="!key.includes('Id')">
                    {{ prop }}
                </template>
            </div>
            <div class="tdDiv" > {{area.childSeen}}</div>
        </div>
    </div>
</div>

脚本:

var mainObjects = @(Html.Raw(result.Content));

for (var i = 0; i < mainObjects.length; i++) {
    mainObjects[i].childSeen = false;
    for (var j = 0; j < mainObjects[i].ChildData.length; j++) {
        mainObjects[i].ChildData[j].childSeen = false;
    }
}

 console.log(mainObjects);

var app = new Vue({
    el: "#app",
    data: mainObjects,
    methods: {
        klik: function (region) {
            console.log(region.childSeen)
            if (region.childSeen == false) {
                console.log('wasFalse');
                return region.childSeen = true;
            }
            return region.childSeen = false;
        }

    },
});

模型示例:

public class Test 
{
   public string FirstName {get;set;}
   public string LastName {get;set;}
   public List<Rebenok> ChildData {get;set;}
}

public class Rebenok
{
   public string FirstName {get;set;}
   public string LastName {get;set;}
   public List<Diagnosis> Diagnoses {get;set;}
}

public class Diagnosis
{
   public string Name {get;set;}
   public string Description {get;set;}
}

1 个答案:

答案 0 :(得分:0)

mainObjects引用未更改。您需要深层复制才能使Vue被动

<div id="app">
    <div class="areaInfo " v-for="(area, index) in mainObjects" v-on:click="klik(index)">
        <div class="trDiv areaData">
            <div class="tdDiv" v-for="(prop, key) in area" v-if="key != 'ChildData'">
                {{key}}
                <template v-if="key.includes('Start') || key.includes('End') ">
                    {{ ConvertJsonDateString(prop) }}
                </template>
                <template v-else-if="!key.includes('Id')">
                    {{ prop }}
                </template>
            </div>
            <div class="tdDiv" > {{area.childSeen}}</div>
        </div>
    </div>
</div>

var app = new Vue({
    el: "#app",
    data () {
      return {
        mainObjects
      }
    },
    methods: {
        klik: function (index) {
            const mainObjects = JSON.parse(JSON.stringify(mainObjects)) // deep copy
            const region = mainObjects[index]
            console.log(region.childSeen)
            if (region.childSeen == false) {
                console.log('wasFalse');
                region.childSeen = true;
            }
            region.childSeen = false;
            this.mainObjects = mainObjects // assign again
        }

    },
});