1.0.0 • Published 3 years ago

@shapla/vue-table v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Shapla Data Table

A simple responsive data table component for Vue 3

Supports:

  • Row Actions with Slot Support
  • Custom Column Slot
  • Sorting

Table of contents

Installation

npm install --save @shapla/vue-table

Usage

Styles

with Sass:

import '@shapla/vue-table/src/index.scss';

with CSS:

import '@shapla/vue-table/dist/style.css';

Note: @shapla/vue-table component has dependency over @shapla/vue-checkbox, also remember to include that style

Javascript Instantiation

import ShaplaTable from '@shapla/vue-table';

export default {
  name: 'Hello',

  components: {
    ShaplaTable
  },

  data() {
    return {
      selectedItems: [],
      columns: [
        {key: 'title', label: 'Title', sortable: true},
        {key: 'author', label: 'Author'}
      ],
      actions: [
        {key: 'edit', label: 'Edit'},
        {key: 'trash', label: 'Delete'}
      ],
      items: [
        {
          id: 1,
          title: 'Wings of Fire: An Autobiography',
          author: ['A.P.J. Abdul Kalam'],
          image: 'https://images.gr-assets.com/books/1295670969l/634583.jpg'
        },
        {
          id: 2,
          title: 'Who Moved My Cheese?',
          author: ['Spencer Johnson', 'Kenneth H. Blanchard'],
          image: 'https://images.gr-assets.com/books/1388639717l/4894.jpg'
        },
        {
          id: 3,
          title: 'Option B',
          author: ['Sheryl Sandberg', 'Adam Grant', 'Adam M. Grant'],
          image: 'https://images.gr-assets.com/books/1493998427l/32938155.jpg'
        }
      ]
    };
  },
}
<shapla-table
  :columns="columns"
  :items="items"
  :actions="actions"
  :show-cb="true"
  action-column="title"
  @action:click="onActionClick"
>
  <template slot="title" slot-scope="data">
    <img :src="data.row.image" :alt="data.row.title" width="50">
    <strong><a href="#">{{ data.row.title }}</a></strong>
  </template>

  <template slot="author" slot-scope="data">
    {{ data.row.author.join(', ') }}
  </template>
</shapla-table>

Props

PropertyTypeRequiredDefaultDescription
itemsArrayyesnullPass an Array of Objects with key:value format.
columnsArrayyesnullPass an Array of Objects. See columns data object
selectedItemsArrayno[]Pass an Array of object id
actionsArrayno[]If you want to show row actions, pass an Array of Objects
actionColumnStringnotitleDefine which is the action column so we could place action items there.
indexStringnoidThe index identifier of the row
showCbBooleannotrueWhether to show the bulk checkbox in each rows
selectAllTextStringnoSelect AllShows if no items are found
notFoundTextStringnoNo items found.Shows if no items are found
sortByStringnonullThe property in data on which to initially sort.
sortOrderStringnoascThe initial sort order.
mobileWidthNumberno768Mobile breakpoint for table.

columns data object

PropertyTypeRequiredDefaultDescription
keyStringyes | Column key.
labelStringyes | Column label
numericBooleannofalseSet true if table column data type is numeric.
sortableBooleannofalseWhether the column data can be sorted by asc or desc order.

actions data object

PropertyTypeRequiredDefaultDescription
keyStringyes | Action key
labelStringyes | Action label

Listeners

The table component fires the following events:

click:action: When a row action is clicked, it fires the event. The action name and the current row will be passed.

<!-- template -->
<data-table @click:action="onActionClick">
</data-table>


<!-- method -->
methods: {
  onActionClick(action, row) {
    if ( 'trash' === action ) {
      if ( confirm('Are you sure to delete?') ) {
        alert('deleted: ' + row.title);
      }
    }
  }
}

click:sort: When a sorted column is clicked

<!-- template -->
<data-table @click:sort="sortCallback">
</data-table>

<!-- method -->
methods: {
  sortCallback(column, order) {
    this.sortBy = column;
    this.sortOrder = order;

    // this.loadItems(column, order);
  }
}

select:item: When an item or all items are selected. Array of selected items will be passed.

<!-- template -->
<data-table @select:item="onItemSelect">
</data-table>

<!-- method -->
methods: {
  onItemSelect(ids) {
    this.selectedItems = ids;
  }
}

Extra components

import {Pagination} from '@shapla/vue-table';

export default {
  components: {
    Pagination
  },
  methods: {
    paginate(nextPage){
      // Handle pagination event
    }
  }
}
<pagination @paginate="paginate"></pagination>

Props

PropertyTypeRequiredDefaultDescription
totalItemsNumberYes0Total number of items.
perPageNumberYes20Number of items to show per page.
currentPageNumberYes1Current page number.
sizeStringNodefaultPagination button size. Value can be default, small, medium, large
textNameStringNoitemsPlural name of item.
textNameSingularStringNoitemSingular name of item.
textCurrentPageStringNoCurrent PageScreen reader text for current page.
textFirstPageStringNoFirst PageScreen reader text for first page.
textPreviousPageStringNoPrevious PageScreen reader text for previous page.
textNextPageStringNoNext PageScreen reader text for next page.
textLastPageStringNoLast PageScreen reader text for last page.
textOfStringNoofScreen reader text for 'of' text.

Listeners

The pagination component fires the following events:

paginate: When any navigation icon is clicked, it fires the event and it gives current page number.

<!-- template -->
<pagination @paginate="paginate"></pagination>


<!-- method -->
methods: {
  paginate(NextPage){
    // Handle click event
  }
}
import {StatusList} from '@shapla/vue-table';

export default {
  name: 'Hello',

  components: {
    StatusList
  }
}
  <status-list :statuses="statuses"/>

Props

PropertyTypeRequiredDefaultDescription
typeStringnohorizontalset vertical to see vertical design
statusesArrayno[]Pass an Array of Objects. See statuses data object

statuses data object

PropertyTypeRequiredDefaultDescription
keyStringyes | Status key.
labelStringyes | Status label
activeBooleanyes | If set true, status will be highlighted.
countNumberyes | How many items has for current status.