2

I'm using last version of vue-js and element-ui whose documentation can be found here.

Context

I want to display a table containing some information about interested and attending people for a given date and room

Issue

I do not manage to display the attending and interested for each room and for each date.

The main problem is how to loop through each room

https://jsfiddle.net/k7Lzv38b/

Data

tableData: [{
        date: 'Jan 2017',
        rooms: [{
          name: 'Room A',
          attending: 10,
          interested: 5
        }, {
          name: 'Room B',
          attending: 10,
          interested: 0
        }]
      }, {
        date: 'Feb 2017',
        rooms: [{
          name: 'Room A',
          attending: 10,
          interested: 5
        }, {
          name: 'Room B',
          attending: 10,
          interested: 0
        }]
      }]

3 Answers 3

4

var Main = {
  data() {
    return {
      tableData: [{
        date: 'Jan 2017',
        rooms: [{
          name: 'Room A',
          attending: 10,
          interested: 5
        }, {
          name: 'Room B',
          attending: 5,
          interested: 10
        }]
      }, {
        date: 'Feb 2017',
        rooms: [{
          name: 'Room A',
          attending: 0,
          interested: 5
        }, {
          name: 'Room B',
          attending: 5,
          interested: 15
        }]
      }, {
        date: 'Mar 2017',
        rooms: [{
          name: 'Room B',
          attending: 5
        }]
      }]
    }
  },
  computed: {
    rooms() {
      let rooms = {}
      this.tableData.forEach(row => {
        row.rooms.forEach(room => {
          rooms[room.name] = 1
        })
      })
      return Object.keys(rooms)
    }
  },
  methods: {
    cellFormatter(row, col) {
      let key = JSON.parse(col.property)
      let d = row.rooms.find(r => r.name === key.room)
      if (d && d[key.property]) {
        return d[key.property]
      }
      return '0 '
    }
  }
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
@import url("//unpkg.com/[email protected]/lib/theme-chalk/index.css");
.container {
  display: flex;
}
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/[email protected]/lib/index.js"></script>
<div id="app">
  <template>
    <div class='container'>
      <el-table :data="tableData">
        <el-table-column prop="date" label="Date" align="center"></el-table-column>
        <el-table-column label="Rooms" align="center">
          <el-table-column :label="room" prop="rooms" align="center" v-for="room in rooms" key="room">
            <el-table-column label="Attending" :prop="JSON.stringify({room, property:'attending'})" align="center" :formatter="cellFormatter">
            </el-table-column>
            <el-table-column label="Intereted" :prop="JSON.stringify({room, property:'interested'})" align="center" :formatter="cellFormatter">
            </el-table-column>
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>
  </template>
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

Thank You so much!!! is this documented somewhere? this "formatter" usage
it is in element.eleme.io/#/en-US/component/table formatter function that formats cell content Function(row, column, cellValue)
This is a nice complete(!), but I would rename renderCell as formatter (or similar), because you're using method signature Function(row, column, cellValue, index). Render functions has different meaning, since we're only formatting scalar value (i.e. not HTML). Also, there is a renderCell method that actually is like a Vue Render function Function(h, { column, $index }) unfortunately, only render-header method is overridable. See also what @[michiel-zoer] does below.
Thanks @renoirb for pointing that out; I've updated the function name.
2

To show nested data without looping use:

columns: [
  { prop: 'user_id', label: 'User ID' },
  { prop: 'market', label: 'Market ID', render: row => row.market.mkt_id },
  { prop: 'market', label: 'Pair',  render: row => row.market.mkt_name },
  { prop: 'exchange', label: 'Exchange ID', render: row => row.exchange.exch_id },
  { prop: 'exchange', label: 'Exchange', render: row => row.exchange.exch_name }
]

JSON:

{
  "user_id": 4,
  "type_id": 1,
  "market": {
        "id": 270,
        "mkt_id": 5946,
        "mkt_name": "AEX",
  },
  "exchange": {
        "id": 5,
        "exch_id": 15,
        "exch_name": "NL",
  }
}

1 Comment

It sounds like a nice solution ! Would you be able to show us a full example with jsfiddle to see how it applies ?
0

If there are only two rooms: A and B, maybe you can simple use this 'prop' for the columns:

prop="rooms[0].attending"
prop="rooms[0].interested"

index 0 for room A and index 1 for room B

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.