Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One option is to do something like:</p> <pre><code>&lt;tbody data-bind="foreach: entries"&gt; &lt;tr&gt; &lt;td&gt; &lt;!-- ko if: type === 'file' --&gt; &lt;i class="icon-file"&gt;&lt;/i&gt; &lt;a href="#" data-bind="text: name, click: $parent.showFile"&gt;&lt;/a&gt; &lt;!-- /ko --&gt; &lt;!-- ko if: type !== 'file' --&gt; &lt;i class="icon-folder"&gt;&lt;/i&gt; &lt;a href="#" data-bind="text: name, click: $parent.goToPath"&gt;&lt;/a&gt; &lt;!-- /ko --&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; </code></pre> <p>Sample here: <a href="http://jsfiddle.net/rniemeyer/9DHHh/" rel="noreferrer">http://jsfiddle.net/rniemeyer/9DHHh/</a></p> <p>Otherwise, you can simplify your view by moving some logic into your view model like:</p> <pre><code>&lt;tbody data-bind="foreach: entries"&gt; &lt;tr&gt; &lt;td&gt; &lt;i data-bind="attr: { 'class': $parent.getClass($data) }"&gt;&lt;/i&gt; &lt;a href="#" data-bind="text: name, click: $parent.getHandler($data)"&gt;&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; </code></pre> <p>Then, add methods on your view model to return the appropriate value:</p> <pre><code>var ViewModel = function() { var self = this; this.entries = [ { name: "one", type: 'file' }, { name: "two", type: 'folder' }, { name: "three", type: 'file'} ]; this.getHandler = function(entry) { console.log(entry); return entry.type === 'file' ? self.showFile : self.goToPath; }; this.getClass = function(entry) { return entry.type === 'file' ? 'icon-file' : 'icon-filder'; }; this.showFile = function(file) { alert("show file: " + file.name); }; this.goToPath = function(path) { alert("go to path: " + path.name); }; }; </code></pre> <p>Sample here: <a href="http://jsfiddle.net/rniemeyer/9DHHh/1/" rel="noreferrer">http://jsfiddle.net/rniemeyer/9DHHh/1/</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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