Bulma Table with Actions

by WILLIAM CORREA

HTML

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.4.1/css/bulma.css">
<div id="app">
  <h1>{{ title }}</h1>
  <b-paginate-select v-model="perPage"></b-paginate-select>
  <b-button @click="print" template-inline>
    <button class="button">Imprimir</button>
  </b-button>
  <hr>
  <b-paginate v-model="currentPage" :last="totalPages"></b-paginate>
  <b-table :header="header" :table="table"></b-table>
  <b-paginate v-model="currentPage" :last="totalPages"></b-paginate>
  <button @click="add">Mais</button>
</div>

<div id="printable">Lala</div>

<!-- template for the modal component -->
<script type="text/x-template" id="template-modal">
  <transition name="modal">
    <div class="modal-mask">
      <div class="modal-wrapper">
        <div class="modal-container">

          <div class="modal-header">
            <slot name="header">
              {{ header }}
            </slot>
          </div>

          <div class="modal-body">
            <slot name="body">
              {{ body }}
            </slot>
          </div>

          <div class="modal-footer">
            <slot name="footer">
              <button class="button is-primary modal-default-button" @click="$emit('close')">
                OK
              </button>
            </slot>
          </div>
        </div>
      </div>
    </div>
  </transition>
</script>

<script type="text/x-template" id="template-table">
  <div id="table">
    <table class="table">
      <thead>
        <tr>
          <th>#</th>
          <th v-for="_head in header">{{ _head.label }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(_row, $index) in table">
          <th scope="row">
            <component v-for="_action in actions" :key="_action.id" :is="_action.is" :context="_row"></component>
          </th>
          <td v-for="(_key, $id) in columns"> {{ _row[_key] }}</td>
        </tr>
      </tbody>
    </table>
    <v-modal v-if="modal.visible" @close="modal.visible = false"...

CSS

@media print {
  #app {
    display: none
  }
}

@media screen {
  #printable {
    display: none
  }
}

body {
  padding: 10px
}

.pagination a {
  max-width: 80px
}

th[scope="row"] {
  cursor: pointer;
}


/* modal */

.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .5);
  display: table;
  transition: opacity .3s ease;
}

.modal-wrapper {
  display: table-cell;
  vertical-align: middle;
}

.modal-container {
  width: 300px;
  margin: 0px auto;
  padding: 20px 30px;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
  transition: all .3s ease;
  font-family: Helvetica, Arial, sans-serif;
}

.modal-header h3 {
  margin-top: 0;
  color: #42b983;
}

.modal-body {
  margin: 20px 0;
}

.modal-footer {
  padding: 0 0 30px 0;
}

.modal-default-button {
  float: right;
}


/*
 * The following styles are auto-applied to elements with
 * transition="modal" when their visibility is toggled
 * by Vue.js.
 *
 * You can easily play with the modal transition by editing
 * these styles.
 */

.modal-enter {
  opacity: 0;
}

.modal-leave-active {
  opacity: 0;
}

.modal-enter .modal-container,
.modal-leave-active .modal-container {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}

JavaScript

Vue.component('b-paginate-select', Vue.extend({
  props: ['value'],
  data: () => ({
    perPage: 0
  }),
  mounted() {
    this.perPage = this.value;
  },
  template: '#template-paginate-select'
}));

Vue.component('b-paginate', Vue.extend({
  props: ['value', 'last'],
  template: '#template-pagination'
}))

Vue.component('b-action-link', Vue.extend({
  props: ['context'],
  template: `<a @click="handleClick"> {{ context['id'] }} </a>`,
  methods: {
    handleClick() {
      this.context.lastName = 'Clicked'
      console.log(this.context)
    }
  }
}))

Vue.component('b-table', Vue.extend({
  template: '#template-table',
  props: {
    header: {
      required: true
    },
    table: {
      required: true
    },
    actions: {
      default: () => ([{
        is: 'b-action-link',
        label: 'Abrir'
      }])
    }
  },
  data: () => ({
    columns: [],
    modal: {
      visible: false,
      header: '',
      body: ''
    }
  }),
  created() {
    this.columns = this.header.map((_item) => {
      return _item.column;
    });
  }
}))

new Vue({
  el: '#app',
  data: {
    title: 'Bulma Table',
    currentPage: 0,
    perPage: 3,
    totalPages: 1,
    header: [{
      column: 'firstName',
      label: 'First Name'
    }, {
      column: 'lastName',
      label: 'Last Name'
    }, {
      column: 'userName',
      label: 'Username'
    }],
    table: [],
    all: [{
      id: 1,
      firstName: 'Mark',
      lastName: 'Otto',
      userName: '@mdo'
    }, {
      id: 2,
      firstName: 'Jacob',
      lastName: 'Thornton',
      userName: '@fat'
    }, {
      id: 3,
      firstName: 'Larry',
      lastName: 'the Bird',
      userName: '@twitter'
    }, {
      id: 4,
      firstName: 'Mark 1',
      lastName: 'Otto 1',
      userName: '@mdo 1'
    }, {
      id: 5,
      firstName: 'Jacob 1',
      lastName: 'Thornton 1',
      userName: '@fat 1'
    }, {
      id: 6,
      firstName: 'Larry 1',
      lastName: 'the Bird 1',
      userName: '@twitter 1'
 ...