根据字符串列表绑定到模板中 [英] Binding to model in template based on a list of strings

查看:123
本文介绍了根据字符串列表绑定到模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中,我有一个这样的字符串列表:

In my controller I have a list of strings like this:

columns : ['name', 'description']

我想根据这个列表过滤模板中显示的内容:

I want to filter what is displayed in the template based on this list:

        {{#each m in controller.model}}
            {{#each col in controller.columns}}
               <td>{{m[col]}}</td>
            {{/each}}
        {{/each}}

课程 {{m [col]}} 不是有效的代码,但你会得到想法。我可以如何实现这一点?

Of course {{m[col]}} is not valid code but you get the idea. How might I achieve this?

这段代码是通用的,所以我没有办法讲述列数组之前的内容。

This code is generic so I have no way of telling what the columns array contains before hand.

推荐答案

您可以创建一个绑定的帮助器,如下所示:

You can create a bound helper as follows:

Ember.Handlebars.registerBoundHelper('dd', function(rowData, col) {
  return rowData[col];
});

然后,您可以在模板中使用它,如下所示:

Then, you can use it in your template as follows:

{{#each item in model}}
  <tr>
    {{#each col in columns}}
      <td> {{ dd item col }} </td>
    {{/each}}
  </tr>
{{/each}}

jsbin上的工作示例 here

Working example on jsbin here

这篇关于根据字符串列表绑定到模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆