最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vuetify - How do I make the header of v-data-table dynamic - Stack Overflow

programmeradmin2浏览0评论

Suppose I have a dynamic array of object which suppose to look like this :

[{id: 1, item: 'apple', taste:'good', item2: 'papaya', taste2: 'bad'}
 {id: 2, item: 'melon', taste: 'bad'},
 {id: 3, item: 'orange', taste: 'good', item2: 'banana', taste2: 'bad', item3: 'grape', taste3:'good'} .......]

How do I make the v-data-table to make the header look like this

ID | ITEM | TASTE | ITEM2 | TASTE2 | ITEM3 | TASTE3 .....

Suppose I have a dynamic array of object which suppose to look like this :

[{id: 1, item: 'apple', taste:'good', item2: 'papaya', taste2: 'bad'}
 {id: 2, item: 'melon', taste: 'bad'},
 {id: 3, item: 'orange', taste: 'good', item2: 'banana', taste2: 'bad', item3: 'grape', taste3:'good'} .......]

How do I make the v-data-table to make the header look like this

ID | ITEM | TASTE | ITEM2 | TASTE2 | ITEM3 | TASTE3 .....
Share Improve this question edited Mar 29, 2020 at 13:29 Boussadjra Brahim 1 asked Mar 29, 2020 at 12:43 deymbwoideymbwoi 1353 silver badges11 bronze badges 2
  • are you working with vuetify? – Boussadjra Brahim Commented Mar 29, 2020 at 12:45
  • @BoussadjraBrahim yes – deymbwoi Commented Mar 29, 2020 at 12:50
Add a ment  | 

2 Answers 2

Reset to default 4

The headers value should be a puted property which has the following code :

puted: {
        headers() {
          //set having unique values
          let s = new Set();

          this.items.forEach(item => {
            for (f in item) {
              //adding an existing value doesn't override the old one
              s.add(f)
            }
          });
              //respect the headers format required by Vuetify which 
              // should has at least two fields (text, value)
          return Array.from(s).map(a => {
            return {
              text: a.toUpperCase(),
              value: a
            }
          });

        }

      }

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data() {
    return {
      items: [{
          id: 1,
          item: 'apple',
          taste: 'good',
          item2: 'papaya',
          taste2: 'bad'
        }, {
          id: 2,
          item: 'melon',
          taste: 'bad'
        },
        {
          id: 3,
          item: 'orange',
          taste: 'good',
          item2: 'banana',
          taste2: 'bad',
          item3: 'grape',
          taste3: 'good'
        }
      ]
    }
  },
  puted: {
    headers() {
      let s = new Set();

      this.items.forEach(item => {
        for (f in item) {
          s.add(f)
        }
      });

      return Array.from(s).map(a => {
        return {
          text: a.toUpperCase(),
          value: a
        }
      });

    }

  }
})
<link href="https://fonts.googleapis./css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vuetify.js"></script>

<div id="app">
  <v-app>
    <v-content>
      <v-container>
        <v-data-table :headers="headers" :items="items"></v-data-table>
      </v-container>
    </v-content>
  </v-app>
</div>

Using .reduce(), Set() and Object.keys() bination you can get the header as expected.

Try the following:

const data = [
  {id: 1, item: 'apple', taste:'good', item2: 'papaya', taste2: 'bad'},
  {id: 2, item: 'melon', taste: 'bad'},
  {id: 3, item: 'orange', taste: 'good', item2: 'banana', taste2: 'bad', item3: 'grape', taste3:'good'}
];
 
const uniqueKeys = Array.from(data.reduce((a, c) => {
  Object.keys(c).forEach(e => a.add(e));
  return a;
}, new Set()));

console.log(uniqueKeys);

发布评论

评论列表(0)

  1. 暂无评论