2017-05-22 60 views
0

我在我的代碼中使用了聚合物紙樹元素。它使用靜態數據完美地顯示樹,但是當我嘗試動態添加項時,它不起作用。我在這裏附上截圖。 enter image description here如何動態添加項目到聚合物紙樹?

//數組

this.itemArray = {"name": "Project", 
               "icon": "theaters", 
               "open": true, 
               "children": [{ 
                "name": "Quotes", 
                "icon": "theaters", 
                "open": true, 
                "children": [{ 
                 "name": "Breaking Bad", 
                 "icon": "theaters" 
                }] 
               }, { 
                "name": "Templates", 
                "icon": "theaters", 
                "open": true, 
                "children": [{ 
                 "name": "Breaking Bad", 
                 "icon": "theaters" 
                }, { 
                 "name": "Game of Thrones", 
                 "icon": "theaters" 
                }] 
               }] 
             } 

    //Js code which accepts value from a text box and adds it to the array. 


this.$.additem.addEventListener("click", function(){ 

enter code herevar data = self.$.txtData.value; 
                      self.itemArray.children[1].children.push(
              {'name':data, 
              'icon':'theaters'}); 

              self.addData(self.itemArray,data); 
              self.$.txtData.value = " "; 


            }); 

// addData function 



addData:function(itemsarr){ 
         console.log("calling items array function",this.tree); 
         var tempArr = []; 
         tempArr = itemsarr; 
         if(this.tree){ 
         this.$.temp.removeChild(this.tree); 
         //tempArr = itemsarr; //= this.itemArray2; 
         } 

         this.tree = document.createElement('paper-tree-node'); 
         this.tree.setAttribute('id','treemenu'); 
         this.$.temp.appendChild(this.tree); 
         this.tree.data = tempArr; 
         console.log(tempArr); 

     } 
+1

你試過聚合物陣列功能嗎? https://www.polymer-project.org/1.0/docs/devguide/model-data#array-mutation –

+0

是的......數組出來是正確的,但它不能正確應用在樹上。只有第一個動態項目才能正確添加文本和圖標,但之後只顯示文件夾圖標。不知道是什麼問題。 – ashwini

+1

你能編輯你的問題並添加一些代碼嗎?所以我們可以看到你有什麼 –

回答