Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would advise that you use knockout mapping to add custom functionality to your data items that you can invoke from your markup. (I have created a working <a href="http://jsbin.com/eziwog/1/edit" rel="nofollow">JSBin</a> for you ) </p> <p>Your initial data remains the same and your javascript becomes:</p> <pre><code>var DataItem = function(data) { var self = this; ko.mapping.fromJS(data, {}, self); // add custom functionality to each data item here // here an alert is added that can be called via 'selectItem' in markup self.selectItem = function(){ alert("item with id " + self.OrderId() + " clicked"); }; }; var viewModelMapping = { 'DataItems': { create: function(options) { return new DataItem(options.data); } } }; var ViewModel = function(data) { var self = this; ko.mapping.fromJS(data, viewModelMapping, self); }; var vm = new ViewModel(initialData); $(document).ready(function () { ko.applyBindings(vm); }); </code></pre> <p>And your HTML table becomes (<em>removed styling for clarity</em>):</p> <pre><code>&lt;table&gt; &lt;tbody data-bind="foreach: DataItems"&gt; &lt;tr&gt; &lt;td"&gt; &lt;input type="button" value="Select" data-bind="click: selectItem" /&gt; &lt;span data-bind="text: OrderId"&gt;&lt;/span&gt; &lt;/td&gt; &lt;td data-bind="text: ItemHtml"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>You can read more about customizing mapped objects in the <strong>Customizing object construction using “create”</strong> and <strong>Customizing object updating using “update”</strong> sections <a href="http://knockoutjs.com/documentation/plugins-mapping.html" rel="nofollow">here</a></p>
    singulars
    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.
 

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