KnockoutJS - Méthode reverse ()

La description

L'observable KnockoutJS reverse() La méthode inverse l'ordre du tableau.

Syntaxe

arrayName.reverse()

Paramètres

N'accepte aucun paramètre.

Exemple

<!DOCTYPE html>
   <head>
      <title>KnockoutJS ObservableArray reverse method</title>
      <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js"
         type = "text/javascript"></script>
   </head>

   <body>
      <p>Example to demonstrate reverse() method.</p>
      <button data-bind = "click: revEmp">Reverse Array</button>
      <p>Array of employees: <span data-bind = "text: empArray()" ></span></p>

      <script>
         function EmployeeModel() {
            this.empName = ko.observable("");
            this.chosenItem = ko.observableArray("");
            this.empArray = ko.observableArray(['Scott','James','Jordan','Lee',
               'RoseMary','Kathie']);

            this.revEmp = function() {
               this.empArray.reverse();  // reverse order
            }
         }
         
         var em = new EmployeeModel();
         ko.applyBindings(em);
      </script>
      
   </body>
</html>

Production

Exécutons les étapes suivantes pour voir comment fonctionne le code ci-dessus -

  • Enregistrez le code ci-dessus dans array-reverse.htm fichier.

  • Ouvrez ce fichier HTML dans un navigateur.

  • Cliquez sur le bouton Inverser le tableau et observez que l'ordre du tableau est modifié.