【问题标题】:angularjs ng-repeat table with nested table element带有嵌套表元素的angularjs ng-repeat表
【发布时间】:2016-01-21 01:00:25
【问题描述】:

我有一个显示基本客户信息(姓名和电子邮件)的视图,其中有客户详细信息表。

我尝试在 tbody 部分编写 ng-repeat,但子表没有获取数据并且它只打印一次。

我也尝试使用ng-repeat-start ng-repeat-end,但我收到错误提示找不到匹配的ng-repeat-end

错误:[$compile:uterdir] 未终止的属性,找到 'ng-repeat-start' 但没有找到匹配的 'ng-repeat-end'。

我的代码:

<table class="customer">
    <thead>
    <tr>
      <th class="first">Customer Name <span class="sortable"></span></th>
      <th class="opp-detail">Email</th>
    </tr>
    </thead>
    <tbody ng-repeat="customer in customerList">
    <tr>
      <td>{{customer.Name}}</td>
      <td class="opp-detail">{{customer.EMail}}</td>

      <table class="customer-details collapsed" >
        <thead>
        <tr>
          <th class="opp-detail">Name</th>
          <th class="opp-detail">Address</th>
          <th class="opp-detail">City</th>
          <th class="opp-detail">Zip Code</th>
          <th class="opp-detail">Contact</th>
          <th class="opp-detail">Email</th>
          <th class="opp-detail">Phone</th>
        </tr>
        </thead>
        <tbody>
        <tr>
          <td class="opp-detail">{{customer.Name}}</td>
          <td class="opp-detail">{{customer.Address}}</td>
          <td class="opp-detail">{{customer.City}}</td>
          <td class="opp-detail">{{customer.PostCode}}</td>
          <td class="opp-detail">{{customer.Contact}}</td>
          <td class="opp-detail">{{customer.EMail}}</td>
          <td class="opp-detail">{{customer.PhoneNo}}</td>
        </tr>
        </tbody>
      </table>
  </tr>
  </tbody>
</table>

任何想法应该如何解决?


测试数据:

[{ _id: 569fa2ab011d2ddc3065b802,
Name: 'Gicom',
SearchName: 'GICOM',
Address: 'some address',
Address2: '',
City: '',
Contact: '',
PhoneNo: '123456',
FaxNo: '',
PostCode: '',
Country: '',
EMail: 'mail@test.com',
MonitoringSource: 'US',
__v: 0 },
{ _id: 569fa2ab011d2ddc3065b803,
Name: 'Mitsui',
SearchName: 'MITSUI',
Address: '',
Address2: '',
City: '',
Contact: '',
PhoneNo: '',
FaxNo: '',
PostCode: '',
Country: '',
EMail: '',
MonitoringSource: 'US',
__v: 0 },
{ _id: 569fa2ab011d2ddc3065b804,
Name: 'Icon',
SearchName: 'ICON',
Address: 'customer address',
Address2: '',
City: 'some city',
Contact: '',
PhoneNo: '789456',
FaxNo: '',
PostCode: '',
Country: '',
EMail: 'mymail@test.com',
MonitoringSource: 'US',
__v: 0 }]

【问题讨论】:

  • 你的customerList 对象是什么样的?
  • 我认为您应该为此使用嵌套表。
  • 1.从 tbody 中删除 ng-repeat 并将其放在 tr 上。 2. 检查数据库中有多少条记录。 3. 检查您是否在后端查询中使用“LIMIT 1”来获取记录。如果是,请将其删除。最后,分享customerList 对象。
  • 我在帖子中添加了 customerList 对象数据
  • @AntiHeadshot 你能解释一下你的意思吗?

标签: javascript angularjs


【解决方案1】:

只需稍作调整即可正常工作。我为你清理了一点。在下面尝试我的解决方案,如果您有任何问题,请告诉我:

解决方案(针对嵌套表进行了修订):

<table class="customer table table-bordered">
    <thead>
    <tr>
        <th class="first">Customer Name <span class="sortable"></span></th>
        <th class="opp-detail">Email</th>
    </tr>
    </thead>
    <tbody ng-repeat="customer in customerList">
    <tr>
        <td>{{customer.Name}}</td>
        <td class="opp-detail">{{customer.EMail}}</td>
    </tr>
    <tr class="collapsed">
        <td colspan="2">

            <table class="customer-details collapsed table table-bordered">
                <thead>
                <tr>
                    <th class="opp-detail">Name</th>
                    <th class="opp-detail">Address</th>
                    <th class="opp-detail">City</th>
                    <th class="opp-detail">Zip Code</th>
                    <th class="opp-detail">Contact</th>
                    <th class="opp-detail">Email</th>
                    <th class="opp-detail">Phone</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td class="opp-detail">{{customer.Name}}</td>
                    <td class="opp-detail">{{customer.Address}}</td>
                    <td class="opp-detail">{{customer.City}}</td>
                    <td class="opp-detail">{{customer.PostCode}}</td>
                    <td class="opp-detail">{{customer.Contact}}</td>
                    <td class="opp-detail">{{customer.EMail}}</td>
                    <td class="opp-detail">{{customer.PhoneNo}}</td>
                </tr>
                </tbody>
            </table>

        </td>
    </tr>
    </tbody>
</table>

注意 1:如果您使用的是 Bootstrap 并且想清楚地看到表格数据,只需将“表格”类与现有的“客户”类一起应用,您就会看到差异。

注意 2:您的代码的问题在于表的结构。您没有正确放置子表。我进行了调整,以便子表将出现在主父表的正下方,在它自己的单独行中,并且还将“折叠”类移动到 tr 标签而不是嵌套表。这将帮助您根据需要切换行。我用数据测试了这个修改后的解决方案,它在我身上运行良好。试试看,让我知道结果如何。

【讨论】:

  • 感谢您的回答,但我需要嵌套表(包含客户姓名的父表、电子邮件和包含有关客户的更多详细信息的子表)。
  • @intentarr 更新了我的嵌套表解决方案。
  • 像魅力一样工作!谢谢你,@Devner :)
  • @intentarr 很高兴为您提供帮助 :)
猜你喜欢
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-13
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
相关资源
最近更新 更多