hjalmers / angular-generic-table

A generic table for Angular 2+. Generic table uses standard markup for tables ie. table, tr and td elements etc. and has support for expanding rows, global search, filters, sorting, pagination, export to CSV, column clicks, custom column rendering, custom export values.
https://hjalmers.github.io/angular-generic-table/
MIT License
105 stars 55 forks source link

how to use for nested json? #103

Open buck999 opened 7 years ago

buck999 commented 7 years ago

like this.

data = [
    {
      UUID: 'NP1',
      data: {
        nome: 'lore',
        sobrenome: 'episum',
        cpf: 123123123,
        rg: 12323455,
        addresses: [
          'AD52', 'AD55', 'AD59'
        ],
        purchaseHistory: {
          firstBuyStore: 'BS1',
          firstSeller: 'SL1'
        }
      }
    },
    {
      UUID: 'NP2',
      data: {
        nome: 'klasjdkl',
        sobrenome: 'fadsas',
        cpf: 32314,
        rg: 765756,
        purchaseHistory: {
          firstBuyStore: 'BS2',
          firstSeller: 'SL2'
        }
      }
    }
  ];
hjalmers commented 7 years ago

Generic table is meant to be used with flat objects so the easiest option would probably be flatten the objects or to write a simple map function to create new row objects containing the properties you want to display in the table, e. g.

data.map(row => {
    return {
        UUID: row.UUID,
        nome: row.data.nome
        ...
    }
})

Another option you could try, although I'm not sure it works as I haven't tried it myself, would be to use the value function of gtFields to tell the table where to fetch the "real" data. e. g.

settings: [{
        objectKey: 'nome'
      }, {
        objectKey: 'data',
        visible: false
}]

fields: [{
        name: 'Nome',
        objectKey: 'nome',
        value:(row)=>{return row.data.nome}
      }, {
        name: '',
        objectKey: 'data'
}]

please note that you still need to specify all the columns you want to use under settings and fields (you could write a function to add them dynamically too).

Feel free to try above and let me know if it works for you. Haven't really thought to much about nested data objects but if this is something more people would like to use I might consider adding it to make it even easier.

radiumrasheed commented 5 years ago

the value seems to work, but yeah we'll love support for nested json