Knockoutjs-obs-array-removeall

提供:Dev Guides
移動先:案内検索

KnockoutJS-removeAll()メソッド

説明

KnockoutJS Observable * removeAll()*メソッドは、すべてのアイテムを削除し、それらを配列として返します。

構文

arrayName.removeAll()

パラメーター

パラメーターを受け入れません。

<!DOCTYPE html>
   <head>
      <title>KnockoutJS ObservableArray removeAll 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 removeAll() method.</p>
      <button data-bind = "click: removeallEmp">Remove All Emps</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.removeallEmp = function() {
               this.empArray.removeAll();
            }
         }

         var em = new EmployeeModel();
         ko.applyBindings(em);
      </script>

   </body>
</html>

出力

上記のコードがどのように機能するかを確認するために、次の手順を実行してみましょう-

  • 上記のコードを array-removeall ファイルに保存します。
  • このHTMLファイルをブラウザーで開きます。
  • [すべてのEmpsを削除]ボタンをクリックします。