KnockoutJS foreach 循环中的 if 语句

发布于 2025-01-03 05:51:59 字数 750 浏览 1 评论 0原文

这里我有这段代码:

<tbody data-bind="foreach: entries">
    <tr>
        <td><i class="icon-file"></i> <a href="#" data-bind="text: name, click: $parent.goToPath"></a></td>
        </tr>
</tbody>

我想要这样的东西(它是伪代码):

<tbody data-bind="foreach: entries">
    <tr>
        <td><i class="{{ if type == 'file' }} icon-file {{/if}}{{else}} icon-folder {{/else}}"></i> <a href="#" data-bind="text: name, click: {{ if type == 'file' }} $parent.showFile {{/if}}{{else}} $parent.goToPath {{/else}}"></a></td>
    </tr>
</tbody>

Is it possible to write like this on KnockoutJS?

Here I have this code:

<tbody data-bind="foreach: entries">
    <tr>
        <td><i class="icon-file"></i> <a href="#" data-bind="text: name, click: $parent.goToPath"></a></td>
        </tr>
</tbody>

I would like to have something like this (it's pseudocode):

<tbody data-bind="foreach: entries">
    <tr>
        <td><i class="{{ if type == 'file' }} icon-file {{/if}}{{else}} icon-folder {{/else}}"></i> <a href="#" data-bind="text: name, click: {{ if type == 'file' }} $parent.showFile {{/if}}{{else}} $parent.goToPath {{/else}}"></a></td>
    </tr>
</tbody>

Is it possible to write something like this on KnockoutJS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最好是你 2025-01-10 05:51:59

一种选择是执行以下操作:

<tbody data-bind="foreach: entries">
    <tr>
        <td>
            <!-- ko if: type === 'file' -->
                <i class="icon-file"></i>
                <a href="#" data-bind="text: name, click: $parent.showFile"></a>
            <!-- /ko -->
            <!-- ko if: type !== 'file' -->
                <i class="icon-folder"></i>
                <a href="#" data-bind="text: name, click: $parent.goToPath"></a>
            <!-- /ko -->
        </td>
    </tr>
</tbody>

此处示例:http://jsfiddle.net/rniemeyer/9DHHh/

否则,您可以通过将一些逻辑移动到视图模型中来简化视图,例如:

<tbody data-bind="foreach: entries">
    <tr>
        <td>
            <i data-bind="attr: { 'class': $parent.getClass($data) }"></i>
            <a href="#" data-bind="text: name, click: $parent.getHandler($data)"></a>
        </td>
    </tr>
</tbody>

然后,在视图模型上添加方法以返回适当的值:

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);
    };
};

示例如下:http://jsfiddle.net/rniemeyer/9DHHh/1/

One option is to do something like:

<tbody data-bind="foreach: entries">
    <tr>
        <td>
            <!-- ko if: type === 'file' -->
                <i class="icon-file"></i>
                <a href="#" data-bind="text: name, click: $parent.showFile"></a>
            <!-- /ko -->
            <!-- ko if: type !== 'file' -->
                <i class="icon-folder"></i>
                <a href="#" data-bind="text: name, click: $parent.goToPath"></a>
            <!-- /ko -->
        </td>
    </tr>
</tbody>

Sample here: http://jsfiddle.net/rniemeyer/9DHHh/

Otherwise, you can simplify your view by moving some logic into your view model like:

<tbody data-bind="foreach: entries">
    <tr>
        <td>
            <i data-bind="attr: { 'class': $parent.getClass($data) }"></i>
            <a href="#" data-bind="text: name, click: $parent.getHandler($data)"></a>
        </td>
    </tr>
</tbody>

Then, add methods on your view model to return the appropriate value:

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);
    };
};

Sample here: http://jsfiddle.net/rniemeyer/9DHHh/1/

我喜欢麦丽素 2025-01-10 05:51:59

您可以使用基于注释标签的无容器控制流语法:

<tbody data-bind="foreach: entries">
    <tr>
        <!-- ko if: type === "file" -->
            <td><i class="icon-file"></i> <a href="#" data-bind="text: name, click: $parent.showFile"></a></td>
        <!-- /ko -->
        <!-- ko if: type !== "file" -->
            <td><i class="icon-folder"></i> <a href="#" data-bind="text: name, click: $parent.goToPath"></a></td>
        <!-- /ko -->
    </tr>
</tbody>

You can use the containerless control flow syntax, which is based on comment tags:

<tbody data-bind="foreach: entries">
    <tr>
        <!-- ko if: type === "file" -->
            <td><i class="icon-file"></i> <a href="#" data-bind="text: name, click: $parent.showFile"></a></td>
        <!-- /ko -->
        <!-- ko if: type !== "file" -->
            <td><i class="icon-folder"></i> <a href="#" data-bind="text: name, click: $parent.goToPath"></a></td>
        <!-- /ko -->
    </tr>
</tbody>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文