javascript - 按 ionic 3 中的数据分组列表

标签 javascript angular ionic-framework ionic3

我有 table

id   customer_name  country  pincode

1    Jhon             Mexico  12209 

2    Ryan             Mexico  12209 

3    Max              Sweden  12209 

4    Steve            Germany  12209 

我想要这样的输出:

Mexico  
        Jhon, Ryan

Sweden 
         Max

Germany 

         Steve

首页.ts

show(){
    db.executeSql('SELECT *  from  tbl_data where coutry=?',[data.rows.item(i).country])

            .then((data) => {
              if (data.rows.length > 0) {
                alert(data.rows.length);
                for (let j = 0; j < data.rows.length; j++) {
                  this.userdata.push({
                  country:data.rows.item(j).country,
                   customername:data.rows.item(j).customername,
                 })
                 }}}

我如何在 ionic v3 中按这样的数据分组

如何使用 Ionic v3 实现这一目标?有什么想法吗?

最佳答案

据我了解,您正在尝试按国家/地区对其进行分组,以便您可以在 TS 中实现以下内容,

var country = new Set(this.displayItems.map(item => item.country)); 
this.result = [];
country.forEach(getCountry => 
  this.result.push({
    country_name: getCountry, 
    values: this.displayItems.filter(i => i.country === getCountry)
  }
  ))

这里的 this.displayItems 是您的服务数据。

HTML 模板可以是这样的,

<ion-grid>
  <ion-row>
    <ion-col>
      Country
    </ion-col>
    <ion-col>
      Customer Name
    </ion-col>
  </ion-row>
  <hr>

  <ion-row *ngFor="let item of result">
    <ion-col>
      {{ item.country_name }}
    </ion-col>
    <ion-col>
      <span *ngFor="let value of item.values">
        {{ value.customer_name }} ,
      </span>
    </ion-col>
  </ion-row>

</ion-grid>

你可以在这里以 Ionic 方式看到你的预期输出,https://stackblitz.com/edit/ionic-25rq2n ..

关于javascript - 按 ionic 3 中的数据分组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57798397/

相关文章:

javascript - 为什么 CSS 选择器返回一个元素数组?

javascript - 仅当我单击图标而不是整行时才发出警报

forms - Angular 4 表单,禁用字段始终有效

performance - 提高 Angular2 输入字段的性能

angular - 如何替换 Ionic 中的当前页面?

javascript - GeckoFX:脚本耗时太长对话框

javascript - 如何使用 VueJS 正确地将数据传递给同级组件?

Angular ng2-idle 订阅多个页面

javascript - 显示来自 Promise Firebase 的数据

html - 将输入类型文本放在图像的中心