首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Angular-Nativesript ListView中输出对象列表及其嵌套对象列表?

如何在Angular-Nativesript ListView中输出对象列表及其嵌套对象列表?
EN

Stack Overflow用户
提问于 2019-04-07 14:31:27
回答 2查看 621关注 0票数 0

日安,

我在angular-nativescript共享代码项目中遇到了问题。模型和组件运行良好,但输出不是我所期望的,我认为问题出在我的观点(Homecomponent.tns.html)。

我有一个角度模型包含一个对象(产品)的列表中的一个对象(ProductGroup)这里是结构

产品组:

代码语言:javascript
复制
export class ProductGroup {

    public groupName : string;
    public products : ProductItem[];
}

ProductItem:

代码语言:javascript
复制
export class ProductItem {
    public itemDescription : string;
    public remarks : string;
}

我已经在我的组件ngOnInit中预先填充了它

代码语言:javascript
复制
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css'],
})
export class HomeComponent implements OnInit {

  public productGroups : ProductGroup[];
 ngOnInit() {
      ///initiate group
      this.productGroups = [];
      ///insert some items for testing
      this.productGroups.push
    (
      {
        groupName : "Cars",
        products : [
          { itemDescription : "Car", remarks : "" },
          { itemDescription : "Car1",remarks : "" },
          { itemDescription : "Car2",remarks : "" }
        ]
       },
       { groupName : "Trucks",products : [
          { itemDescription : "Truck", remarks : "" },
          { itemDescription : "Truck1",remarks : "" },
          { itemDescription : "Truck2", remarks : ""}]
       }       
    );     
     //trying to check if it is properly populated
    console.log(this.productGroups);
   }
}

我已经console.log了产品组,它看起来是填充的,应该工作得很好。

但是我尝试输出包含组名标题的列表视图中的所有productItem,但它只像这样输出

代码语言:javascript
复制
_________________________________________________
Cars
Car
_________________________________________________
Trucks
Truck
_________________________________________________

这是我的components.tns.html

代码语言:javascript
复制
<ActionBar>
    <ActionItem title="Test"></ActionItem>
</ActionBar>
<StackLayout>




<GridLayout   columns="*" >
    <ListView [items] = "productGroups">
      <ng-template let-group="item" >
        <StackLayout>
          <Label [text]="group.groupName"></Label>
             <StackLayout>
               <ListView  [items] = "group.products">
                 <ng-template let-products="item">
                     <Label [text]="products.itemDescription"></Label>                             
                  </ng-template>                                                        
               </ListView>   
              </StackLayout>
         </StackLayout>
       </ng-template>                          
    </ListView>
</GridLayout>
</StackLayout>

我希望我的问题对你们有意义。我只是希望得到这样的输出

代码语言:javascript
复制
_________________________________________________
Cars
Car
Car1
Car2
_________________________________________________
Trucks
Truck
Truck1
Truck2
_________________________________________________

希望有人能帮我解决这个问题,提前感谢你的帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-07 18:48:59

试试nativescript-accordion插件。如果希望默认情况下展开所有项,请创建一个包含所有索引的数组,并将其应用于selectedIndexes属性。

代码语言:javascript
复制
tns plugin add nativescript-accordion
票数 0
EN

Stack Overflow用户

发布于 2019-04-07 15:09:48

我已经为你创建了一个样例游乐场,here。它在ios上运行得很好。

代码语言:javascript
复制
<GridLayout>
    <ListView class="list-group" [items]="productGroups" (itemTap)="onItemTap($event)"
        style="height:1250px">
        <ng-template let-group="item">
            <StackLayout >
                <Label [text]="group.groupName"></Label>
                <StackLayout>
                    <ListView [items]="group.products">
                        <ng-template let-products="item">
                            <Label [text]="products.itemDescription"></Label>
                        </ng-template>
                    </ListView>
                </StackLayout>
            </StackLayout>
        </ng-template>
    </ListView>
</GridLayout>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55556251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档