knockout.js - How to access knockout $index in javascript -


i need $index in javascript function implement prev/next buttons list of values. method found seems cumbersome, perhaps there more straightforward way.

now $index in javascript, put in observable:

<div data-bind="foreach: myarray">     <div data-bind="click: function(data, event){ onclick(data, event, $index()); }"  function onclick(idata, event, index) {         theappviewmodel.choiceindex(index); 

on so found small improvement getting $index event:

<div data-bind="foreach: myarray">     <div data-bind="click: onclick"  function onclick(idata, event) {         var context = ko.contextfor(event.target);         theappviewmodel.choiceindex(context.$index()); 

from commenter came method index searching array selected value, has own observable, e.g. choice:, like:

var = thearray.indexof(theappviewmodel.choice()); 

normally, length of array on page isn't huge, , if has big objects, search on 1 of fields fun syntax like:

myarray.find(x => x.id === searchvalue); 

but wonder if isn't possible access $index more directly, without storing in own observable choiceindex, knockout docs $index observable.


here complete sample code play with:

<!doctype html> <html> <head>     <title>test knockout foreach</title>     <meta charset="utf-8" />     <meta name="viewport" content="width=device-width, initial-scale=1" />     <script src='https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js'></script>     <style>         .selected {background-color: #f0f0f0;}     </style> </head> <body>     <div data-bind="foreach: myarray" style="width: 10em;">         <div data-bind="click: function(data, event){ onclick(data, event, $index()); },                          css: { selected: $data == $root.choice() }">             <span data-bind="text: $index"></span>             <span data-bind="text: $data"></span>         </div>     </div>     <input type="button" value="prev" data-bind="click: onprev" />     <input type="button" value="next" data-bind="click: onnext" />     <p>         choice:         <span data-bind="text: choice"></span>     </p>     <p>         index:         <span data-bind="text: choiceindex"></span>     </p>     <script>         var thearray = ["apple", "pear", "banana", "coconut", "peanut"];         function onclick(idata, event, index) {             theappviewmodel.choice(idata);             theappviewmodel.choiceindex(index);             //var context = ko.contextfor(event.target);             //theappviewmodel.choiceindex(context.$index());             //theappviewmodel.choiceindex(index);         }         function onprev(idata, event) {             var = theappviewmodel.choiceindex() - 1;             if (i >= 0) {                 theappviewmodel.choice(thearray[i]);                 theappviewmodel.choiceindex(i);             }         }         function onnext(idata, event) {             //var = theappviewmodel.choiceindex() + 1;             //var dummydata = theappviewmodel.choice();             //var dummy = thearray.indexof(dummydata);             var = thearray.indexof(theappviewmodel.choice()) + 1;             if (i < thearray.length) {                 theappviewmodel.choice(thearray[i]);                 theappviewmodel.choiceindex(i);             }         }         function appviewmodel() {             var self = this;             self.myarray = ko.observablearray(thearray);             self.choice = ko.observable();             self.choiceindex = ko.observable();         }         var theappviewmodel = new appviewmodel();         window.onload = function () {             ko.applybindings(theappviewmodel);         }     </script> </body> </html> 

there no built-in binding set viewmodel value binding, it's simple create 1 so:

ko.bindinghandlers.copyindex = {     init: function (element, valueaccessor, allbindings, vm, bindingcontext) {         vm.index = bindingcontext.index;     } }; 

the use follows:

<div data-bind="foreach: myarray">     <div data-bind="copyindex"></div> </div> 

however, i'd still not recommend approach, since ties viewmodel behavior presence of specific bindings. jason spake's suggestion use myarray.indexof($data) (or ko.utils.arrayindexof(myarray, $data)) more robust.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -