2017-09-08 7 views
1

J'ai rencontré un problème en essayant de mettre à jour la valeur d'un tableau à l'intérieur de mon. J'ai une recherche sur Google depuis les 4 dernières heures, mais pas de chance J'ai le code follwing:Polymer - app-storage: Supprimer l'entrée et mettre à jour le stockage

<!-- 
@license 
Copyright (c) 2016 The Polymer Project Authors. All rights reserved. 
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt 
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt 
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt 
Code distributed by Google as part of the polymer project is also 
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt 
--> 

<link rel="import" href="../bower_components/polymer/polymer-element.html"> 
<link rel="import" href="shared-styles.html"> 
<link rel="import" href="../bower_components/vaadin-grid/vaadin-grid.html"> 
<link rel="import" href="../bower_components/vaadin-date-picker/vaadin-date-picker.html"> 
<link rel="import" href="../bower_components/paper-input/paper-input.html"> 
<link rel="import" href="../bower_components/paper-button/paper-button.html"> 
<link rel="import" href="../bower_components/app-storage/app-localstorage/app-localstorage-document.html"> 






<dom-module id="my-view1"> 
    <template> 
    <style include="shared-styles"> 
     :host { 
     display: block; 

     padding: 10px; 
     } 
     .form { 
     display: flex; 
     flex-direction: column; 
     } 
     .form paper-input { 
     flex: 1; 
     margin-right: 10px; 
     } 
     .form vaadin-date-picker { 
     flex: 1; 
     margin-top: 10px; 

     } 
     .form paper-button { 
     margin-top: 10px; 
     align-self: flex-end; 
     } 

    </style> 
    <div class="card"> 
     <div class="form"> 
     <paper-input label="Sum" value="{{todo.task}}" auto-validate placeholder="Suma" required=true pattern="[0-9]*" error-message="Numbers only"></paper-input> 
     <vaadin-date-picker label="Date" value="{{todo.due}}"></vaadin-date-picker> 
     <paper-button raised on-tap="_addToDo">Add</paper-button> 
     </div> 
<br> 
     <vaadin-grid id="grid" items={{todos}}> 

     <vaadin-grid-column width="calc(50% - 100px)"> 
      <template class="header">Sum</template> 
      <template>{{item.task}}</template> 
     </vaadin-grid-column> 

    <vaadin-grid-column width="calc(50% - 100px)"> 
     <template class="header">Date</template> 
     <template>{{item.due}}</template> 
    </vaadin-grid-column> 

    <vaadin-grid-column> 
     <template> 
      <div style="display: flex; justify-content: flex-end;"> 
      <paper-button raised on-tap="_remove">remove</paper-button> 

      </div> 
     </template> 
     </vaadin-grid-column> 

    </vaadin-grid> 
    </div> 
<app-localstorage-document key="todos" data="{{todos}}"> 
</app-localstorage-document> 
    </template> 



    <script> 
    class MyView1 extends Polymer.Element { 
     static get is() { return 'my-view1'; } 

     static get properties() { 
     return { 
      todo: { 
      type: Object, 
      value:() => { return {} } 
      }, 
      todos: { 
      type: Array, 
      value:() => [] 
      } 
     }; 
     } 

     _addToDo() { 
     this.push('todos', this.todo); 
     this.todo = {}; 
     }; 

     _remove(e) { 
     var index = this.todos.indexOf(e.model.item); 
      this.todos.splice(index, 1); 
      this.$.grid.clearCache(); 
     }; 


    } 


    window.customElements.define(MyView1.is, MyView1); 

    </script> 
</dom-module> 

Je suis en train de mettre à jour localStorage après le retrait de l'un des éléments. Je ne sais pas comment "valider la mise à jour". Est-il possible de mettre à jour le localStorage après avoir supprimé l'élément? Merci!

+2

Pouvez-vous essayer 'this.splice ('todos', index, 1)' au lieu de 'this.todos.splice (index, 1);' et voir si cela fonctionne? – Ofisora

+0

Oui, cela a fonctionné. Merci beaucoup! Comment puis-je choisir votre commentaire comme réponse? – unkn0wnx

+0

Vous pouvez vérifier ma réponse maintenant. – Ofisora

Répondre

2

Si vous manipulez un tableau en utilisant les méthodes natives (comme Array.prototype.splice), vous devez informer Polymer après coup. Ainsi, vous pouvez notifier en utilisant notifySplices. En savoir plus sur notifySplices. Ou, utilisez les méthodes Polymer pour les mutations de tableau.

Chaque élément polymère a les méthodes de mutation de tableau suivantes disponibles:

push(path, item1, [..., itemN]) 
pop(path) 
unshift(path, item1, [..., 
itemN]) 
shift(path) 
splice(path, index, removeCount, [item1, ..., itemN]) 

Vous n'êtes pas obligé d'informer si vous utilisez la méthode de mutation de la matrice polymère. Donc, utilisez this.splice('todos', index, 1) au lieu de this.todos.splice(index, 1);, cela notifiera les modifications apportées dans le tableau todos qui reflétera également les changements dans le localstorage.

+1

Merci! C'est la bonne réponse. – unkn0wnx