Vue UI Components Documentation
Vue Grid Component
The Grid component for Vue is a powerful widget that displays tabular data. It offers rich support for interacting with data, including paging, grouping, sorting, filtering and editing.
Prerequisites
Refer to Vue Getting Started before you start with this help topic.
Configuration
After you have created your App.vue file, here is how you should structure it:
The Grid component for Vue requires the following import:
import JqxGrid from "jqwidgets-scripts/jqwidgets-vue/vue_jqxgrid.vue";
Add the jqxGrid component to the components section of the the Vue class:
components: { JqxGrid },
Template
The App.vue has a <template>
structural tag where we determine the application structure.
There we will also set the tags for the new components - <JqxGrid/>
<template> <JqxGrid :width="width" :source="dataAdapter" :columns="columns" :columnsresize="true" :pageable="true"> </JqxGrid></template>
Properties
The properties of the <JqxGrid/> component are defined in the data
member of the Vue class.
We should put them in the return object of the data function:
data: function () { return { width: 850, dataAdapter: new jqx.dataAdapter(this.source), columns: [ { text: 'Name', datafield: 'name', width: 250 }, { text: 'Beverage Type', datafield: 'type', width: 250 }, { text: 'Calories', datafield: 'calories', width: 180 }, { text: 'Total Fat', datafield: 'totalfat', width: 120 }, { text: 'Protein', datafield: 'protein', minwidth: 120 } ] }}
Events
The events in Vue are set as an attribute with @ prefix, for example:
<JqxGrid @pagechanged="onPageChanged()" :width="width" :source="dataAdapter" :columns="columns" :columnsresize="true" :pageable="true"></JqxGrid>
All events that are bound to a component are implemented in the methods
member of the Vue class.
methods: { onPageChanged: function () { // Do something... }}
Methods
To use a component's method we should have its reference. In Vue we refer to a component by the special $refs
property.
Before that we need to add the desired name reference to that component:
<JqxGrid ref="grid"></JqxGrid>
Here how you can use a component's method:
this.$refs.grid.getpaginginformation();
methods: { // Add here all used callbacks and/or events onPageChanged: function () { // Do something... let info = this.$refs.grid.getpaginginformation(); alert(info); }}
If we want to add additional methods we should also implement them in the methods
member.
In case we need to do some precalculation or something else before the components are rendered, we should use the beforeCreate
member.
In this case:
beforeCreate: function () { // Add here any data where you want to transform before components be rendered this.source = { datatype: 'json', datafields: [ { name: 'name', type: 'string' }, { name: 'type', type: 'string' }, { name: 'calories', type: 'int' }, { name: 'totalfat', type: 'string' }, { name: 'protein', type: 'string' } ], id: 'id', url: 'sampledata/beverages.txt' };}
If you have followed the above steps, you App.vue file would look like this:
App.vue:
<template> <JqxGrid ref="grid" @pagechanged="onPageChanged()" :width="width" :source="dataAdapter" :columns="columns" :columnsresize="true" :pageable="true"> </JqxGrid></template> <script> // Import the components that will be used import JqxGrid from "jqwidgets-scripts/jqwidgets-vue/vue_jqxgrid.vue"; export default { components: { // Adding imported widgets here JqxGrid }, data: function () { // Define properties which will use in the widget return { width: 850, dataAdapter: new jqx.dataAdapter(this.source), columns: [ { text: 'Name', datafield: 'name', width: 250 }, { text: 'Beverage Type', datafield: 'type', width: 250 }, { text: 'Calories', datafield: 'calories', width: 180 }, { text: 'Total Fat', datafield: 'totalfat', width: 120 }, { text: 'Protein', datafield: 'protein', minwidth: 120 } ] } }, beforeCreate: function () { // Add here any data where you want to transform before components be rendered this.source = { datatype: 'json', datafields: [ { name: 'name', type: 'string' }, { name: 'type', type: 'string' }, { name: 'calories', type: 'int' }, { name: 'totalfat', type: 'string' }, { name: 'protein', type: 'string' } ], id: 'id', url: 'sampledata/beverages.txt' }; }, methods: { // Add here all used callbacks and/or events onPageChanged: function () { // Do something... let info = this.$refs.grid.getpaginginformation(); alert(info); } } }</script> <style></style>
Grid Example
<template> <JqxGrid :width="width" :source="dataAdapter" :columns="columns" :pageable="true" :autoheight="true" :sortable="true" :altrows="true" :enabletooltip="true" :editable="true" :selectionmode="'multiplecellsadvanced'" :columngroups="columngroups"> </JqxGrid></template><script> import JqxGrid from "jqwidgets-scripts/jqwidgets-vue/vue_jqxgrid.vue"; export default { components: { JqxGrid }, data: function () { return { width: 850, dataAdapter: new jqx.dataAdapter(this.source), columns: [ { text: 'Product Name', columngroup: 'ProductDetails', datafield: 'ProductName', width: 250 }, { text: 'Quantity per Unit', columngroup: 'ProductDetails', datafield: 'QuantityPerUnit', cellsalign: 'right', align: 'right' }, { text: 'Unit Price', columngroup: 'ProductDetails', datafield: 'UnitPrice', align: 'right', cellsalign: 'right', cellsformat: 'c2' }, { text: 'Units In Stock', datafield: 'UnitsInStock', cellsalign: 'right', cellsrenderer: this.cellsrenderer, width: 100 }, { text: 'Discontinued', columntype: 'checkbox', datafield: 'Discontinued', align: 'center' } ], columngroups: [ { text: 'Product Details', align: 'center', name: 'ProductDetails' } ] } }, beforeCreate: function () { this.source = { datatype: 'xml', datafields: [ { name: 'ProductName', type: 'string' }, { name: 'QuantityPerUnit', type: 'int' }, { name: 'UnitPrice', type: 'float' }, { name: 'UnitsInStock', type: 'float' }, { name: 'Discontinued', type: 'bool' } ], root: 'Products', record: 'Product', id: 'ProductID', url: 'sampledata/products.xml' }; }, methods: { cellsrenderer: function (row, columnsfield, value, defaulthtml, columnproperties, rowdata) { if (value < 20) { return '<span style="margin: 4px; float: ' + columnproperties.cellsalign + '; color: #ff0000;">' + value + '</span>'; } else { return '<span style="margin: 4px; float: ' + columnproperties.cellsalign + '; color: #008000;">' + value + '</span>'; } } } }</script><style></style>