KnockoutJS - méthode slice ()
La description
L'observable KnockoutJS slice()La méthode découpe un morceau d'un tableau. La tranche ici est la même que la fonction de tranche JavaScript native. Renvoie les éléments de start-index à endindex.
Syntaxe
arrayName.slice(start-index,end-index)
Paramètres
Accepte 2 paramètres, index de début et index de fin.
Exemple
<!DOCTYPE html>
<head>
<title>KnockoutJS ObservableArray slice 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 slice() method.</p>
<p>slice(1,3) to show items starting from index 1 up to 3:
<span data-bind = "text: empArray().slice(1,3)"></span>
</p>
<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']);
}
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-slice.htm fichier.
Ouvrez ce fichier HTML dans un navigateur.