【问题标题】:Ember.js/rendering nested array content on handlebarsEmber.js/在把手上渲染嵌套数组内容
【发布时间】:2023-03-21 05:00:01
【问题描述】:

我有一个模型“事务”,其中声明了一个子类别数组。每当调用 transactionsController 的方法 'add_subcagtegory' 时,此数组都会填充事务类型对象。现在,当我尝试在嵌套循环(#collection)中呈现子类别时,我没有完成它。渲染数组控制器对象的外部循环(#each)工作正常。谁能告诉如何渲染 subCategories 数组?

app.js

App.transaction=Em.Object.extend({
  account:null,
  date:null,
  source:null,
  description:null,
  category:null,
  flag_for_later:null,
  amount:null,
  category_id:null,
  record_index:null,
  isSubCategory:null,
  subCategories:[]
});

App.transactionsController = Em.ArrayController.create({
  content: [],
  add_subcategory: function(param){
     var records=this.toArray();
     if (typeof(records[param.value -1].subCategories) === "undefined") {
       records[param.value -1].subCategories = new Array();
     }


      var category=App.transaction.create({
        account:"//",
        date:"//",
        source:"//",
        description:"//",
        category:" ",
        flag_for_later:" ",
        amount:null,
        category_id:records[param.value -1].subCategories.length + 1,
        isSubCategory:true
      });

     records[param.value -1].subCategories.push(category);

     App.transactionsController.set('content',[]);
     App.transactionsController.pushObjects(records);

     App.array.push(obj1);
    }
});

和模板:

<table>
    {{#each App.transactionsController}}
      <tr>
        <td>{{account}}</td>
        <td>{{date}}</td>
        <td>{{source}}</td>
        <td>{{view App.TextField class="span12" style="border:0px;"  objcount=record_index fieldname="description" value=description}}</td>
        <td>{{view App.TextField class="span12" style="border:0px;" objcount=record_index fieldname="category" value=category }}</td>
        <td><button onclick="App.transactionsController.add_subcategory(this);" value="{{unbound record_index}}">+</button></td>
        <td>{{view App.TextField class="span6" style="border:0px;" objcount=record_index fieldname="flag_for_later" value=flag_for_later }}</td>
        <td>{{amount}}</td>
      </tr>
      {{#collection contentBinding="App.transactionsController.subCategories"}}
        <b>content does,nt not render</b>
      {{/collection}}
    {{/each}}
</table>

在collection下的template中,如何访问subCategories?

http://jsfiddle.net/KbN47/29/

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    简单地将 {{collection}} 助手的内容绑定到 this.subcategories(this 是您上下文中的事务)有效吗?

    {{#collection contentBinding="this.subcategories"}}
    

    更新

    这是一个 jsfiddle:http://jsfiddle.net/Sly7/tRbZC/

    请注意,ember 版本是最新版本。您应该更新,因为 0.9.5 已经很旧了。 我没有看到&lt;select&gt; 的行为,但如果它不起作用,我想你现在拥有使它起作用的所有关键:)

    【讨论】:

    • 这很奇怪,因为您将子类别定义为事务对象中的数组...您可以发布代码的 jsfiddle 吗?
    • 好的,看起来 add_category 函数有问题。我下午去看看。您能否使用 jsfidlle 编辑您的问题,或者至少展示您如何使用此功能?
    • 这是如何使用的 &lt;td&gt;&lt;button onclick="App.transactionsController.add_subcategory(this);" value="{{unbound record_index}}"&gt;+&lt;/button&gt;&lt;/td&gt; 很快就会发布一个 jsfiddle
    • 使用 jsfidlle 更新问题,单击 (+) 按钮添加子类别,但不显示任何内容
    【解决方案2】:

    我将 ember 版本从最新版本修改为 1.0 之前版本,点击 + 即可。

    http://jsfiddle.net/y3YX9/

    【讨论】:

      猜你喜欢
      • 2015-05-15
      • 2016-07-29
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      相关资源
      最近更新 更多