0

我使用 jsViews 创建了一个表/网格。每行都有一个编辑按钮,单击该按钮会选择该行并显示输入控件而不是文本值。

如果我使用它显示/隐藏输入,data-link="visible{:#parent.parent.data.selectedIndex!==#index}"那么它工作正常。

但是,我尝试了一种不同的方法{^{if #parent.parent.data.selectedIndex===#index}}...{{else}}...{{/if}}来显示/隐藏输入,当我的数据对象上的 selectedIndex 更改时,这不起作用。

我也尝试过,{^{if ~root.selectedIndex===#index}}但也没有用。有可能做到这一点{{if}}吗?我在第一种方法上尝试这个的原因是为了避免渲染很多选择框,这些选择框无论如何都会被隐藏。

我的数据对象如下所示:

app = {
    data: [...],

    selectedIndex: null,

    select: function select(index) {
        if (this.selectedIndex !== index) {
            $.observable(this).setProperty("selectedIndex", index);
        }
    }
};

我像这样链接模板:

$.templates("#myTemplate").link("#divHolder", app)
    .on("click", ".data .editButton", function() {
        app.select($.view(this).index);
    })
    .on("click", ".data .saveButton", function() {
        // save details
    })
    .on("click", ".transmittals .cancelButton", function() {
        // reset values

        app.select(null);
    });

我的模板是这样的:

<script id="myTemplate" type="text/x-jsrender">
    <table id="tblData" class="data">
        <thead>
            <tr>
                <th></th>
                <th>A</th>
                <th>B</th>
                <th>C</th>                    
            </tr>
        </thead>
        <tbody>
            {^{for data}}
            <tr class="item">
                <td>
                    {{if #parent.parent.data.selectedIndex===#index}}
            <span class="editItem">
                <button class="cancelButton">Cancel</button></span>
                    {{else}}
            <span class="viewItem">
                <button class="editButton">Edit</button></span>
                    {{/if}}                
                </td>
                <td>
                    {^{if #parent.parent.data.selectedIndex===#index}}
            <span class="editItem"><input type="text" data-link="B" /></span>
                    {{else}}
            <span class="viewItem" data-link="B"></span>
                    {{/if}}
                </td>
                <td>
                    {^{if #parent.parent.data.selectedIndex===#index}}
            <span class="editItem"><input type="text" data-link="C" /></span>
                    {{else}}
            <span class="viewItem" data-link="C"></span>
                    {{/if}}
                </td>
            </tr>
        {{/for}}
        </tbody>
    </table>
</script>
4

1 回答 1

1

当您添加 {{if}} 块时,它是一个嵌套视图,因此单击按钮不会让您获得带有索引的项目视图。您需要使用$.view(this).parent.index- 或者更简单的$.view(this).getIndex()- 自动逐步通过嵌套视图(如果有)到项目视图并获取其索引。

app.select($.view(this).getIndex());

(请参阅此处的讨论:https ://github.com/BorisMoore/jsrender/issues/173#issuecomment-11058106 )

顺便说一句,这里是您的示例的修改形式,只是为了给您一些想法。它用于<button data-link="{on ~root.select #getIndex()}">Edit</button>连接按钮上的点击处理程序并直接调用 select 方法,将索引传递给它:

<script id="myTemplate" type="text/x-jsrender">
<table id="tblData" class="data">
    <thead>
        ...
    </thead>
    <tbody>
    {^{for data}}
        <tr class="item">
        {^{if ~root.selectedIndex===#index}}
            <td><button class="cancelButton" data-link="{on ~root.select null}">Cancel</button></td>
            <td><input data-link="A" /></td>
            <td><input data-link="B" /></td>
        {{else}}
            <td><button class="editButton" data-link="{on ~root.select #getIndex()}">Edit</button></td>
            <td data-link="A"></td>
            <td data-link="B"></td>
        {{/if}}
        </tr>
    {{/for}}
    </tbody>
</table>
</script>

<div id="divHolder"></div>

<script>
var app = {
    data: [{A:"aa", B: "bb"},{A:"aa2", B: "bb2"}],
    selectedIndex: null,
    select: function(index) {
        if (this.selectedIndex !== index) {
            $.observable(this).setProperty("selectedIndex", index);
        }
    }
};

$.templates("#myTemplate").link("#divHolder", app);
</script>
于 2015-05-07T21:06:30.917 回答