2017-12-24 28 views
0

계산 된 데이터를 사용하여 추가 함수로 트리를 만들려고합니다. 나는 vuejs 공식 홈페이지에서 tree-view 예제를 사용하고 생성 된 계산 된 함수와 결합했지만이를 구현하는 데는 아무런 운이 없었다. 나는 4 일 동안이 문제를 해결하려고 노력했지만 아직 운이 없으므로 여기서 도움을 청한다.vue.js - 원시 json에서 중첩 배열을 사용할 때 순환 구성 요소가 업데이트되지 않습니다.

목록 끝에있는 "+"를 클릭하면 addChild 함수에 대한 호출이 트리거되고 데이터가 성공적으로 추가됩니다. 데이터가 추가되지만 재귀 구성 요소는 반응이 없습니다.

https://jsfiddle.net/znedj1ao/9/

var data2 = [{ 
 
    "id": 1, 
 
    "name":"games", 
 
    "parentid": null 
 
    }, 
 
    { 
 
    "id": 2, 
 
    "name": "movies", 
 
    "parentid": null 
 
    }, 
 
    { 
 
    \t "name": "iron-man", 
 
    "id": 3, 
 
    "parentid": 2 
 
    }, 
 
    { 
 
    "id": 4, 
 
    "name": "iron-woman", 
 
    "parentid": 3 
 
    } 
 
] 
 

 
// define the item component 
 
Vue.component('item', { 
 
    template: '#item-template', 
 
    props: { 
 
    model: Object 
 
    }, 
 
    data: function() { 
 
    return { 
 
     open: false 
 
    } 
 
    }, 
 
    computed: { 
 
    isFolder: function() { 
 
     return this.model.children && 
 
     this.model.children.length 
 
    } 
 
    }, 
 
    methods: { 
 
    toggle: function() { 
 
     if (this.isFolder) { 
 
     this.open = !this.open 
 
     } 
 
    }, 
 
    changeType: function() { 
 
     if (!this.isFolder) { 
 
     Vue.set(this.model, 'children', []) 
 
     this.addChild() 
 
     this.open = true 
 
     } 
 
    }, 
 
    addChild: function() { 
 
     this.model.children.push({ 
 
     name: 'My Tres', 
 
    \t \t \t \t children: [ 
 
    \t \t \t { name: 'hello' }, 
 
    \t \t \t { name: 'wat' } 
 
     ] 
 
     }) 
 

 
    } 
 
    } 
 
}) 
 

 
// boot up the demo 
 
var demo = new Vue({ 
 
    el: '#demo', 
 
    data: { 
 
    treeData2: data2 
 
    }, 
 
    computed: { 
 
    nestedInformation: function() {  
 
      var a= this.nestInformation(data2); 
 
      return a; 
 
     } 
 
    
 
    }, 
 
    methods: 
 
    { 
 
     nestInformation: function(arr, parent){ 
 
      var out = [] 
 
    for(var i in arr) { 
 
     if(arr[i].parentid == parent) { 
 
      var children = this.nestInformation(arr, arr[i].id) 
 

 
      if(children.length) { 
 
       arr[i].children = children 
 
      } 
 
      out.push(arr[i]) 
 
     } 
 
    } 
 
    return out 
 
    } 
 
    } 
 
})
<!-- item template --> 
 
<script type="text/x-template" id="item-template"> 
 
    <li> 
 
    <div 
 
     :class="{bold: isFolder}" 
 
     @click="toggle" 
 
     @dblclick="changeType"> 
 
     {{model.name}} 
 
     <span v-if="isFolder">[{{open ? '-' : '+'}}]</span> 
 
    </div> 
 
    <ul v-show="open" v-if="isFolder"> 
 
     <item 
 
     class="item" 
 
     v-for="model in model.children" 
 
     :model="model"> 
 
     </item> 
 
     <li class="add" @click="addChild">+</li> 
 
    </ul> 
 
    </li> 
 
</script> 
 

 
<p>(You can double click on an item to turn it into a folder.)</p> 
 

 
<!-- the demo root element --> 
 
<ul id="demo"> 
 
    <item 
 
    class="item" 
 
    :model="nestedInformation[1]"> 
 
    </item> 
 
</ul>

+0

나는 Vue 전문가는 아니지만 밀어 넣기를 사용하여 배열에 요소를 추가한다고해서 추가 된 속성이 반응하지 않는 것으로 보입니다. 당신은'$ set' 또는'Vue.set()'을 시도 할 수 있습니다. 그러나 나는 정말로 확신하지 않는다. –

+0

나는 이미 작동하지 않는 것을 시도했다. https : //jsfiddle.net/3p0j5sgy/1368/에서 확인할 수 있습니다. 유일한 차이점은 이미 부모 - 자식 구조에있는 데이터를 전달하고 있다는 것입니다. – Johji

+0

내 문제는 내 데이터가 원시이며 전달하기 전에 먼저 트리 구조로 변환해야합니다. – Johji

답변

2

Vue.js 문서 있음은 comment 위 말한다 Abdullah Khanlinked에 : 인해 현대 자바 스크립트의 한계로 다시

, 뷰는 할 수 없습니다 속성 추가 또는 삭제 감지.

그러나, 재산 또한 정확히무엇 당신이 당신의 nestInformation 방법으로하고 있습니다 :

if(children.length) { 
    arr[i].children = children 
} 

결과는 각 개체의 children 속성이 반응하지, 그렇게 할 때이 배열이라는 것이다 addChild에 푸시되면 UI에서 다시 렌더링이 트리거되지 않습니다.

배열은 children 배열을 만들 때 Vue.set을 사용하여 반응성 속성이됩니다. 내가 갈래의 한

if (children.length) { 
    Vue.set(arr[i], 'children', children); 
} 

및 참조 할 수 있도록 fiddle을 수정 한 다음 nestInformation 방법의 관련 코드는 다음에 업데이트해야합니다.