Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout js template not binding to ko.computed
    primarykey
    data
    text
    <p>I have pretty much spent most of the day playing around in complete frustration in an effort to get the attached code to work correctly. The code is more or less taken from <a href="http://jsfiddle.net/rniemeyer/5Xr2X/" rel="nofollow">http://jsfiddle.net/rniemeyer/5Xr2X/</a>. My version displays the first page, IE, the first 10 rows. Clicking next fires the pagedRows function but will not for the life of me display the next page of data. Heeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelp. </p> <p>Attached is the code</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;th&gt;Id&lt;/th&gt; &lt;th&gt;Type&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;/tr&gt; &lt;tbody data-bind='template: { name: "fieldTemplate", foreach: pagedRows }'&gt;&lt;/tbody&gt; &lt;/table&gt; &lt;a href="#" data-bind="click: previous, visible: pageIndex() &gt; 0"&gt;Previous&lt;/a&gt; &lt;a href="#" data-bind="click: next, visible: pageIndex() &lt; maxPageIndex()"&gt;Next&lt;/a&gt; &lt;script type="text/html" id="fieldTemplate"&gt; &lt;tr&gt; &lt;td&gt;&lt;span data-bind="text:FieldId" /&gt;&lt;/td&gt; &lt;td&gt;&lt;span data-bind="text:Type" /&gt;&lt;/td&gt; &lt;td&gt;&lt;span data-bind="text:Description" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/script&gt; &lt;script&gt; function ViewModel(jsondata) { var self = this; self.fields = ko.observableArray(jsondata); self.pageIndex = ko.observable(0); self.pageSize = ko.observable(10); self.pagedRows = ko.computed(function () { var size = self.pageSize(); var start = self.pageIndex() * size; return self.fields.slice(start, start + size); }); self.maxPageIndex = ko.computed(function () { return Math.ceil(self.fields().length / self.pageSize()) - 1; }); self.next = function () { self.pageIndex(self.pageIndex + 1); } self.previous = function () { self.pageIndex(self.pageIndex - 1); } } $(function () { var data = []; for (var i = 0; i &lt; 100; i++) { data.push({ FieldId: i, Type: "Type" + i, Name: "Name" + i, Description: "Description" + i }) } ko.applyBindings(new ViewModel(data)); }); &lt;/script&gt; </code></pre> <p>Seems insanely straight forward, but I am just not getting it. any help would be greatly appreciated.</p> <p>thanks</p> <p>Mike</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload