Cell in Vue Grid component
20 Sep 202424 minutes to read
In the Syncfusion Vue Grid, a cell refers to an individual data point or a unit within a grid column that displays data. It represents the intersection of a row and a column, and it contains specific information associated with that row and column. Each cell can display text, numbers, or other content related to the data it represents.
The Grid component allows you to customize the appearance and behavior of cells using various features and options. You can define templates, format cell values, enable or disable editing, and perform various other operations on the cells to create interactive and informative data grids in your web applications.
Displaying the HTML content
Displaying HTML content in a Grid can be useful in scenarios where you want to display formatted content, such as images, links, or tables, in a tabular format. Grid component allows you to display HTML tags in the Grid header and content. By default, the HTML content is encoded to prevent potential security vulnerabilities. However, you can enable the disableHtmlEncode property by setting the value as false to display HTML tags without encoding. This feature is useful when you want to display HTML content in a grid cell.
In the following example, the EJ2 Toggle Switch Button component is added to enable and disable the disableHtmlEncode property. When the switch is toggled, the change event is triggered and the disableHtmlEncode
property of the column is updated accordingly. The refreshColumns method is called to refresh the grid and display the updated content.
<template>
<div id="app">
<div>
<label style="padding: 10px 10px">
Enable or disable HTML Encode
</label>
<ejs-switch id="switch" :change="change"></ejs-switch>
</div>
<ejs-grid ref='grid' :dataSource="data" height='315' style="padding: 10px 10px">
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=90></e-column>
<e-column field='CustomerID' headerText='<strong> Customer ID </strong>' width=100></e-column>
<e-column field='Freight' headerText='Freight' textAlign='Right' format='C2' width=80></e-column>
<e-column field='ShipCity' headerText='Ship City' width=120> </e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-grids";
import { SwitchComponent as EjsSwitch } from "@syncfusion/ej2-vue-buttons";
import { data } from './datasource.js';
import { ref } from 'vue';
const grid = ref(null);
const change = function (args) {
if (args.checked) {
grid.value.getColumnByField('CustomerID').disableHtmlEncode = false;
}
else {
grid.value.getColumnByField('CustomerID').disableHtmlEncode = true;
}
grid.value.refreshColumns()
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<div>
<label style="padding: 10px 10px">
Enable or disable HTML Encode
</label>
<ejs-switch id="switch" :change="change"></ejs-switch>
</div>
<ejs-grid ref='grid' :dataSource="data" height='315' style="padding: 10px 10px">
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=90></e-column>
<e-column field='CustomerID' headerText='<strong> Customer ID </strong>' width=100></e-column>
<e-column field='Freight' headerText='Freight' textAlign='Right' format='C2' width=80></e-column>
<e-column field='ShipCity' headerText='Ship City' width=120> </e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective } from "@syncfusion/ej2-vue-grids";
import { SwitchComponent } from "@syncfusion/ej2-vue-buttons";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-switch":SwitchComponent,
"ejs-grid":GridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective
},
data() {
return {
data: data
};
},
methods: {
change: function(args) {
if(args.checked){
this.$refs.grid.getColumnByField('CustomerID').disableHtmlEncode = false;
}
else{
this.$refs.grid.getColumnByField('CustomerID').disableHtmlEncode = true;
}
this.$refs.grid.refreshColumns()
}
},
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
- The disableHtmlEncode property disables HTML encoding for the corresponding column in the grid.
- If the property is set to true, any HTML tags in the column’s data will be displayed.
- If the property is set to false, the HTML tags will be removed and displayed as plain text.
- Disabling HTML encoding can potentially introduce security vulnerabilities, so use caution when enabling this feature.
- If enableHtmlSanitizer property of grid is set to true, then the content is sanitized to prevent any potential security vulnerabilities.
- You can also disable the
disableHtmlEncode
property of the column using getColumns method on change event of Switch component.This is demonstrated in the below code snippet,
change: function(args) {
if (args.checked) {
this.$refs.grid.getColumns()[1].disableHtmlEncode = false;
} else {
this.$refs.grid.getColumns()[1].disableHtmlEncode = true;
}
this.$refs.grid.refreshColumns();
}
Autowrap the grid content
The auto wrap feature allows the cell content in the grid to wrap to the next line when it exceeds the boundary of the specified cell width. The cell content wrapping works based on the position of white space between words. To support the Autowrap functionality in Syncfusion Grid, you should set the appropriate width for the columns. The column width defines the maximum width of a column and helps to wrap the content automatically.
To enable auto wrap, set the allowTextWrap property to true. You can also configure the wrap mode by setting the textWrapSettings.wrapMode property.
Grid provides the below three options for configuring:
- Both - This is the default value for wrapMode. With this option, both the grid Header and Content text is wrapped.
- Header - With this option, only the grid header text is wrapped.
- Content - With this option, only the grid content is wrapped.
The following example demonstrates how to set the allowTextWrap
property to true and specify the wrap mode as Content by setting the textWrapSettings.wrapMode
property. Also change the textWrapSettings.wrapMode
property to Content and Both on changing the dropdown value using the change event of the DropDownList component.
<template>
<div id="app">
<div style="display: inline-block;">
<label style="padding: 10px 10px 12px 0"> Auto wrap mode: </label>
<ejs-dropdownlist ref='dropdown' id='dropdownlist' index="0" width="150" :dataSource="ddlData" :fields='fields'
:change="change"></ejs-dropdownlist>
</div>
<ejs-grid ref='grid' id="grid" style="margin-top: 5px" :dataSource='data' :allowTextWrap='true'
:textWrapSettings='wrapSettings' height='310' width='800'>
<e-columns>
<e-column field='Inventor' headerText='Inventor Name' width='150' textAlign="Right"></e-column>
<e-column field='NumberofPatentFamilies' headerText="Number of Patent Families" width='150'
textAlign="Right"></e-column>
<e-column field='Country' headerText='Country' width='140'></e-column>
<e-column field='Active' width='120'></e-column>
<e-column field='Mainfieldsofinvention' headerText='Main fields of invention' width='200'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-grids";
import { DropDownListComponent as EjsDropdownlist } from "@syncfusion/ej2-vue-dropdowns";
import { data } from './datasource.js';
import { ref } from 'vue';
const grid = ref(null);
const wrapSettings = { wrapMode: 'Content' };
const fields = { text: 'text', value: 'value' };
const ddlData = [
{ text: 'Content', value: 'Content' },
{ text: 'Both', value: 'Both' },
];
const change = function (args) {
grid.value.textWrapSettings.wrapMode = args.value;
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";</style>
<template>
<div id="app">
<div style="display: inline-block;">
<label style="padding: 10px 10px 12px 0"> Auto wrap mode: </label>
<ejs-dropdownlist ref='dropdown' id='dropdownlist' index="0"
width="150" :dataSource="ddlData" :fields='fields' :change="change"></ejs-dropdownlist>
</div>
<ejs-grid ref='grid' id="grid" style="margin-top: 5px" :dataSource='data' :allowTextWrap='true' :textWrapSettings='wrapSettings' height='310' width='800'>
<e-columns>
<e-column field='Inventor' headerText='Inventor Name' width='150' textAlign="Right"></e-column>
<e-column field='NumberofPatentFamilies' headerText="Number of Patent Families" width='150' textAlign="Right"></e-column>
<e-column field='Country' headerText='Country' width='140'></e-column>
<e-column field='Active' width='120'></e-column>
<e-column field='Mainfieldsofinvention' headerText='Main fields of invention' width='200'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective } from "@syncfusion/ej2-vue-grids";
import { DropDownListComponent } from "@syncfusion/ej2-vue-dropdowns";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-dropdownlist":DropDownListComponent,
"ejs-grid":GridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective
},
data() {
return {
data: data,
wrapSettings: { wrapMode: 'Content' },
fields: { text: 'text', value: 'value' },
ddlData : [
{ text: 'Content', value: 'Content' },
{ text: 'Both', value: 'Both' },
],
};
},
methods: {
change: function(args) {
let grid = this.$refs.grid.ej2Instances;
grid.textWrapSettings.wrapMode = args.value;
}
},
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
Customize cell styles
Customizing the grid cell styles allows you to modify the appearance of cells in the Grid control to meet your design requirements. You can customize the font, background color, and other styles of the cells. To customize the cell styles in the grid, you can use grid event, css, property or method support.
Using event
To customize the appearance of the grid cell, you can use the queryCellInfo event of the grid. This event is triggered when each header cell is rendered in the grid, and provides an object that contains information about the header cell. You can use this object to modify the styles of the header cell.
The following example demonstrates how to add a queryCellInfo
event handler to the grid. In the event handler, checked whether the current column is Freight field and then applied the appropriate CSS class to the cell based on its value.
<template>
<div id="app">
<ejs-grid :dataSource="data" :enableHover='false' :allowSelection='false' height='315' :queryCellInfo='customiseCell'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=90></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='Freight' headerText='Freight' textAlign='Right' format='C2' width=80></e-column>
<e-column field='ShipCity' headerText='Ship City' width=120></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
const customiseCell = function (args) {
if (args.column.field === 'Freight') {
if (args.data['Freight'] <= 30) {
args.cell.classList.add('below-30');
} else if (args.data['Freight'] > 30 & args.data['Freight'] < 80) {
args.cell.classList.add('below-80');
} else {
args.cell.classList.add('above-80');
}
}
}
</script>
<style>
.below-30 {
background-color: orangered;
}
.below-80 {
background-color: yellow;
}
.above-80 {
background-color: greenyellow
}
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";</style>
<template>
<div id="app">
<ejs-grid :dataSource="data" :enableHover='false' :allowSelection='false' height='315' :queryCellInfo='customiseCell'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=90></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='Freight' headerText='Freight' textAlign='Right' format='C2' width=80></e-column>
<e-column field='ShipCity' headerText='Ship City' width=120></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-grid": GridComponent,
"e-columns": ColumnsDirective,
"e-column": ColumnDirective
},
data() {
return {
data: data
};
},
methods: {
customiseCell: function (args) {
if (args.column.field === 'Freight') {
if (args.data['Freight'] <= 30) {
args.cell.classList.add('below-30');
} else if (args.data['Freight'] > 30 & args.data['Freight'] < 80) {
args.cell.classList.add('below-80');
} else {
args.cell.classList.add('above-80');
}
}
}
}
}
</script>
<style>
.below-30 {
background-color: orangered;
}
.below-80 {
background-color: yellow;
}
.above-80 {
background-color: greenyellow
}
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";</style>
- The queryCellInfo event is triggered for every cell of the grid, so it may impact the performance of the grid whether used to modify a large number of cells.
Using CSS
You can apply styles to the cells using CSS selectors. The Grid provides a class name for each cell element, which you can use to apply styles to that specific cell or cells in a particular column. The e-rowcell
class is used to style the row cells, and the e-selectionbackground
class is used to change the background color of the selected row.
.e-grid td.e-cellselectionbackground {
background: #9ac5ee;
font-style: italic;
}
The following example demonstrates how to customize the appearance of a specific row in the grid on selection using className
.
<template>
<div id="app">
<ejs-grid :dataSource="data" height='315' :selectionSettings='selectionOptions'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width=90></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=100></e-column>
<e-column field='ShipCity' headerText='Ship City' width=90></e-column>
<e-column field='ShipName' headerText='Ship Name' textAlign='Right' width=120 > </e-column> </e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
const selectionOptions = { type: 'Multiple', mode: 'Cell' };
</script>
<style>
.e-grid td.e-cellselectionbackground {
background: #9ac5ee;
font-style: italic;
}
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<ejs-grid :dataSource="data" height='315' :selectionSettings='selectionOptions'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width=90></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=100></e-column>
<e-column field='ShipCity' headerText='Ship City' width=90></e-column>
<e-column field='ShipName' headerText='Ship Name' textAlign='Right' width=120 > </e-column> </e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-grid":GridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective
},
data() {
return {
data: data,
selectionOptions: { type: 'Multiple', mode: 'Cell' }
};
}
}
</script>
<style>
.e-grid td.e-cellselectionbackground {
background: #9ac5ee;
font-style: italic;
}
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
Using property
To customize the style of grid cells, define customAttributes property to the column definition object. The customAttributes
property takes an object with the name-value pair to customize the CSS properties for grid cells. You can also set multiple CSS properties to the custom class using the customAttributes property.
.custom-css {
background: #d7f0f4;
font-style: italic;
color:navy
}
Here, setting the customAttributes property of the ShipCity column to an object that contains the CSS class ‘custom-css’. This CSS class will be applied to all the cells in the ShipCity column of the grid.
<e-column field='ShipCity' headerText='Ship City' :customAttributes= "{class: 'custom-css'}" width=90 >
</e-column>
The following example demonstrates how to customize the appearance of the OrderID and ShipCity columns using custom attributes.
<template>
<div id="app">
<ejs-grid :dataSource="data" height='315'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' :customAttributes= "{class: 'custom-css'}" width=90></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=100></e-column>
<e-column field='ShipCity' headerText='Ship City' :customAttributes= "{class: 'custom-css'}" width=90></e-column>
<e-column field='ShipName' headerText='Ship Name' textAlign='Right' width=120 > </e-column> </e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
</script>
<style>
.custom-css {
background: #d7f0f4;
font-style: italic;
color:navy
}
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<ejs-grid :dataSource="data" height='315'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' :customAttributes= "{class: 'custom-css'}" width=90></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=100></e-column>
<e-column field='ShipCity' headerText='Ship City' :customAttributes= "{class: 'custom-css'}" width=90></e-column>
<e-column field='ShipName' headerText='Ship Name' textAlign='Right' width=120 > </e-column> </e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-grid":GridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective
},
data() {
return {
data: data
};
}
}
</script>
<style>
.custom-css {
background: #d7f0f4;
font-style: italic;
color:navy
}
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
Using methods
The Grid provides below methods to customize the appearance of the grid columns header and cell:
-
getHeaderContent: The
getHeaderContent
method is used to customize the appearance of the column header in the grid and accessing the header element using thequerySelector
method and applying the style using the style property of the cell element. -
getCellFromIndex: The
getCellFromIndex
method is used to customize the appearance of a specific cell in the grid by specifying the index of the row and column for which you want to customize the appearance.
The following example demonstrates how to use getColumnHeaderByIndex and getCellFromIndex methods to customize the appearance of the CustomerID column header and specific cell inside the dataBound event of the grid.
<template>
<div id="app">
<ejs-grid ref='grid' :dataSource="data" height='315' :dataBound="dataBound">
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=90></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='ShipCity' headerText='Ship City' width=120></e-column>
<e-column field='ShipName' headerText='Ship Name' textAlign='Right' width=80></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
import { ref } from 'vue';
const grid = ref(null);
const dataBound = function() {
let header = grid.value.getHeaderContent().querySelector('.e-headercell');
header.style.backgroundColor = 'red';
header.style.color = 'white';
let cell = grid.value.getCellFromIndex(1, 2);
cell.style.background = '#f9920b';
cell.style.color = 'white';
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<ejs-grid ref='grid' :dataSource="data" height='315' :dataBound="dataBound">
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=90></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='ShipCity' headerText='Ship City' width=120></e-column>
<e-column field='ShipName' headerText='Ship Name' textAlign='Right' width=80></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-grid":GridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective,
},
data() {
return {
data: data
};
},
methods: {
dataBound: function() {
let header=this.$refs.grid.getHeaderContent().querySelector('.e-headercell');
header.style.backgroundColor = 'red';
header.style.color = 'white';
let cell = this.$refs.grid.getCellFromIndex(1, 2);
cell.style.background = '#f9920b';
cell.style.color = 'white';
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
Make sure to pass the correct row and column indices to getCellFromIndex method, or else the appearance of the wrong cell might get customized.
Clip mode
The clip mode feature is useful when you have a long text or content in a grid cell, which overflows the cell’s width or height. It provides options to display the overflow content by either truncating it, displaying an ellipsis or displaying an ellipsis with a tooltip. You can enable this feature by setting columns.clipMode property to one of the below available options.
There are three types of clipMode
available:
- Clip: Truncates the cell content when it overflows its area.
- Ellipsis: Displays ellipsis when the cell content overflows its area.
- EllipsisWithTooltip: Displays ellipsis when the cell content overflows its area, also it will display the tooltip while hover on ellipsis is applied. Also it will display the tooltip while hover on ellipsis is applied.
The following example demonstrates, how to set the clipMode
property to Clip , Ellipsis and EllipsisWithTooltip for the Main Fields of Invention column, on changing the dropdown value using the change event of the DropDownList
component. The refreshColumns method is used to refresh the grid and display the updated content.
<template>
<div id="app">
<div style="display: inline-block;">
<label style="padding: 10px 10px 12px 0"> Change the clip mode: </label>
<ejs-dropdownlist ref='dropdown' id='dropdownlist' index="0" width="150" :dataSource="ddlData" :fields='fields'
:change="change"></ejs-dropdownlist>
</div>
<ejs-grid ref='grid' style="padding: 5px 5px" :dataSource='data' height='315'>
<e-columns>
<e-column field='MainFieldsofInvention' headerText='Invention' width='130'></e-column>
<e-column field='Inventor' headerText='Inventor' width='80'></e-column>
<e-column field='NumberofPatentFamilies' headerText='Count' width='100'></e-column>
<e-column field='Country' headerText='Country' width='80'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-grids";
import { DropDownListComponent as EjsDropdownlist } from "@syncfusion/ej2-vue-dropdowns";
import { data } from './datasource.js';
import { ref } from 'vue';
const grid = ref(null);
const fields = { text: 'text', value: 'value' };
const ddlData = [
{ text: 'Ellipsis', value: 'Ellipsis' },
{ text: 'Clip', value: 'Clip' },
{ text: 'Ellipsis with Tooltip', value: 'EllipsisWithTooltip' },
];
const change = function (args) {
grid.value.getColumnByField('MainFieldsofInvention').clipMode = args.value
grid.value.refreshColumns();
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<div style="display: inline-block;">
<label style="padding: 10px 10px 12px 0"> Change the clip mode: </label>
<ejs-dropdownlist ref='dropdown' id='dropdownlist' index="0"
width="150" :dataSource="ddlData" :fields='fields' :change="change"
></ejs-dropdownlist>
</div>
<ejs-grid ref='grid' style="padding: 5px 5px" :dataSource='data' height='315' >
<e-columns>
<e-column field='MainFieldsofInvention' headerText='Invention' width='130'></e-column>
<e-column field='Inventor' headerText='Inventor' width='80'></e-column>
<e-column field='NumberofPatentFamilies' headerText='Count' width='100'></e-column>
<e-column field='Country' headerText='Country' width='80'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective } from "@syncfusion/ej2-vue-grids";
import { DropDownListComponent } from "@syncfusion/ej2-vue-dropdowns";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-dropdownlist":DropDownListComponent,
"ejs-grid":GridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective
},
data() {
return {
data: data,
fields: { text: 'text', value: 'value' },
ddlData : [
{ text: 'Ellipsis', value: 'Ellipsis' },
{ text: 'Clip', value: 'Clip' },
{ text: 'Ellipsis with Tooltip', value: 'EllipsisWithTooltip' },
],
};
},
methods: {
change: function(args) {
this.$refs.grid.getColumnByField('MainFieldsofInvention').clipMode = args.value
this.$refs.grid.refreshColumns();
}
},
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
Tooltip
The Syncfusion Grid allows you to display information about the grid columns to the user when they hover over them with the mouse.
Render bootstrap tooltip in grid cells
The Grid component allows rendering Bootstrap tooltips in the cells. To enable this feature, you need to add the Bootstrap css to initialize the tooltip.
This is demonstrated in the sample code below which shows how to enable Bootstrap tooltip for the CustomerID field using template
in grid cells,
Step 1: Install the Bootstrap package in your application using the following command and add the style of the respective packages in the app.vue file,
To install bootstrap, use the following command.
npm install vue bootstrap bootstrap-vue
Register the following BootstrapVue in your app.vue file
import Vue from 'vue'
import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'
// Import Bootstrap and BootstrapVue CSS files (order is important)
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
// Make BootstrapVue available throughout your project
Vue.use(BootstrapVue)
// Optionally install the BootstrapVue icon components plugin
Vue.use(IconsPlugin)
Step 2: The following code demonstrates how to render Bootstrap tooltip for the CustomerID field with template
<template>
<div id="app">
<ejs-grid :dataSource="data" :allowPaging="true" >
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=90></e-column>
<e-column field='CustomerID' headerText='Customer ID' textAlign='Center' width=120 :template="'cTemplate'">
<template v-slot:cTemplate={data}>
<div class="text-center">
<span
v-b-tooltip.hover
:title="data.CustomerID"
>{{data.CustomerID}}</span>
</div>
</template>
</e-column>
<e-column field='Freight' headerText='Freight' textAlign='Right' format='C2' width=90></e-column>
<e-column field='OrderDate' headerText='Order Date' textAlign='Right' type='date' format="yMd" width=120></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { provide } from "vue";
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns, Page } from '@syncfusion/ej2-vue-grids';
import { BootstrapVue, IconsComponent } from 'bootstrap-vue';
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import { data } from './datasource.js';
provide('grid', [Page]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
You can get the reference GitHub sample here.
The following screenshot represents the Bootstrap tooltip for the CustomerID field,
Display custom tooltip for columns
The Grid provides a feature to display custom tooltips for its columns using the EJ2 Tooltip component. This allows you to provide additional information about the columns when the user hovers over them.
To enable custom tooltips for columns in the Grid, you can render the Grid control inside the Tooltip component and set the target as .e-rowcell
. This will display the tooltip when hovering over the grid cells.
Change the tooltip content for the grid cells by using the following code in the beforeRender event.
beforeRender(args){
if (args.target.classList.contains('e-rowcell')) {
// event triggered before render the tooltip on target element.
this.$refs.tooltip.content = 'This is value "' + args.target.innerText + '" ';
}
}
The following example demonstrates how to customize the tooltip content for the grid cells by using the beforeRender event of the EJ2 Tooltip component.
<template>
<div id="app">
<ejs-tooltip ref="tooltip" target=".e-rowcell" :beforeRender="beforeRender">
<ejs-grid ref="grid" :dataSource="data" height="315px">
<e-columns>
<e-column field="OrderID" headerText="Order ID" textAlign="Right" width="90"></e-column>
<e-column field="CustomerID" headerText="Customer ID" width="120"></e-column>
<e-column field="Freight" headerText="Freight" textAlign="Right" format="C2" width="90"></e-column>
<e-column field="ShipName" headerText="Ship Name" width="120"></e-column>
</e-columns>
</ejs-grid>
</ejs-tooltip>
</div>
</template>
<script setup>
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
import { TooltipComponent as EjsTooltip } from "@syncfusion/ej2-vue-popups";
import { ref } from 'vue';
const tooltip = ref(null);
const beforeRender = function (args) {
if (args.target.classList.contains('e-rowcell')) {
tooltip.value.ej2Instances.content = 'The value is "' + args.target.innerText + '" ';
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<ejs-tooltip ref="tooltip" target=".e-rowcell" :beforeRender="beforeRender">
<ejs-grid ref="grid" :dataSource="data" height="315px">
<e-columns>
<e-column field="OrderID" headerText="Order ID" textAlign="Right" width="90"></e-column>
<e-column field="CustomerID" headerText="Customer ID" width="120"></e-column>
<e-column field="Freight" headerText="Freight" textAlign="Right" format="C2" width="90"></e-column>
<e-column field="ShipName" headerText="Ship Name" width="120"></e-column>
</e-columns>
</ejs-grid>
</ejs-tooltip>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
import { TooltipComponent } from "@syncfusion/ej2-vue-popups";
export default {
name: "App",
components: {
"ejs-tooltip": TooltipComponent,
"ejs-grid": GridComponent,
"e-columns": ColumnsDirective,
"e-column": ColumnDirective
},
data() {
return {
data: data
};
},
methods: {
beforeRender: function (args) {
if (args.target.classList.contains('e-rowcell')) {
this.$refs.tooltip.ej2Instances.content = 'The value is "' + args.target.innerText + '" ';
}
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
Grid lines
The gridLines in a grid are used to separate the cells with horizontal and vertical lines for better readability. You can enable the grid lines by setting the gridLines
property to one of the following values:
Modes | Actions |
---|---|
Both | Displays both the horizontal and vertical grid lines. |
None | No grid lines are displayed. |
Horizontal | Displays the horizontal grid lines only. |
Vertical | Displays the vertical grid lines only. |
Default | Displays grid lines based on the theme. |
The following example demonstrates how to set the gridLines property based on changing the dropdown value using the change event of the DropDownList component.
<template>
<div id="app">
<div style="display: inline-block;">
<label style="padding: 10px 10px 15px 0"> Change the grid lines: </label>
<ejs-dropdownlist ref='dropdown' id='dropdownlist' index="0" width="130" :dataSource="ddlData"
:change="change"></ejs-dropdownlist>
</div>
<ejs-grid ref='grid' style="padding: 5px 5px" :dataSource='data' height='315' gridLines='Default'>
<e-columns>
<e-column field='Inventor' headerText='Inventor Name' width='180' textAlign="Right"></e-column>
<e-column field='NumberofPatentFamilies' headerText="Number of Patent Families" width='180'
textAlign="Right"></e-column>
<e-column field='Country' headerText='Country' width='140'></e-column>
<e-column field='Active' width='120'></e-column>
<e-column field='Mainfieldsofinvention' headerText='Main fields of invention' width='200'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-grids";
import { DropDownListComponent as EjsDropdownlist } from "@syncfusion/ej2-vue-dropdowns";
import { data } from './datasource.js';
import { ref } from 'vue';
const grid = ref(null);
const ddlData = [
{ text: 'Default', value: 'Default' },
{ text: 'Both', value: 'Both' },
{ text: 'Horizontal', value: 'Horizontal' },
{ text: 'Vertical', value: 'Vertical' },
{ text: 'None', value: 'None' },
];
const change = function (args) {
grid.value.ej2Instances.gridLines = args.value
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<div style="display: inline-block;">
<label style="padding: 10px 10px 15px 0"> Change the grid lines: </label>
<ejs-dropdownlist ref='dropdown' id='dropdownlist' index="0"
width="130" :dataSource="ddlData" :change="change"
></ejs-dropdownlist>
</div>
<ejs-grid ref='grid' style="padding: 5px 5px" :dataSource='data' height='315' gridLines='Default'>
<e-columns>
<e-column field='Inventor' headerText='Inventor Name' width='180' textAlign="Right"></e-column>
<e-column field='NumberofPatentFamilies' headerText="Number of Patent Families" width='180' textAlign="Right"></e-column>
<e-column field='Country' headerText='Country' width='140'></e-column>
<e-column field='Active' width='120'></e-column>
<e-column field='Mainfieldsofinvention' headerText='Main fields of invention' width='200'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective } from "@syncfusion/ej2-vue-grids";
import { DropDownListComponent } from "@syncfusion/ej2-vue-dropdowns";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-dropdownlist":DropDownListComponent,
"ejs-grid":GridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective
},
data() {
return {
data: data,
ddlData : [
{ text: 'Default', value: 'Default' },
{ text: 'Both', value: 'Both' },
{ text: 'Horizontal', value: 'Horizontal' },
{ text: 'Vertical', value: 'Vertical' },
{ text: 'None', value: 'None' },
],
};
},
methods: {
change: function(args) {
this.$refs.grid.ej2Instances.gridLines= args.value
}
},
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
By default, the grid renders with Default mode.