Skip to content

Plugin: CustomBorders

Description

This plugin enables an option to apply custom borders through the context menu (configurable with context menu key borders).

To initialize Handsontable with predefined custom borders, provide cell coordinates and border styles in a form of an array.

When a border property is set to an empty object {} or an empty string '', the default style is applied: 1px solid black.

The plugin also integrates with the [[ContextMenu]] plugin. Adding 'borders' to the contextMenu items enables users to apply or remove borders on selected cells directly from the right-click menu.

See customBorders configuration option or go to Custom cell borders demo for more examples.

Example

// Enable custom borders with context menu integration.
// When a border property is an empty object, the default style (1px solid black) is applied.
new Handsontable(container, {
customBorders: [
{
range: {
from: { row: 1, col: 1 },
to: { row: 3, col: 4 },
},
top: {}, // default: 1px solid black
bottom: {}, // default: 1px solid black
start: {}, // default: 1px solid black
end: {}, // default: 1px solid black
},
{
row: 2,
col: 2,
start: { width: 2, color: 'red', style: 'dotted' },
end: { width: 1, color: 'green', style: 'dashed' },
top: '', // default: 1px solid black
bottom: '', // default: 1px solid black
},
],
// Enable the 'borders' item in the context menu so users can
// apply or remove borders from the right-click menu.
contextMenu: ['borders'],
});

Options

customBorders

Source code

customBorders.customBorders : boolean | Array<object>

The customBorders option configures the CustomBorders plugin.

To enable the CustomBorders plugin (and add its menu items to the context menu), set the customBorders option to true.

To enable the CustomBorders plugin and add a predefined border around a particular cell, set the customBorders option to an array of objects. Each object represents a border configuration for one cell, and has the following properties:

PropertySub-propertiesTypesDescription
row-row: NumberThe cell’s row coordinate.
col-col: NumberThe cell’s column coordinate.
startwidth
color
style
width: Number
color: String
style: String
If the layout direction is LTR (default): start sets the width (width), color (color) and style (style) of the left-hand border.

If the layout direction is RTL: start sets the width (width), color (color) and style (style) of the right-hand border.
endwidth
color
style
width: Number
color: String
style: String
If the layout direction is LTR (default): end sets the width (width), color (color) and style (style) of the right-hand border.

If the layout direction is RTL: end sets the width (width), color (color) and style (style) of the left-hand border.
topwidth
color
style
width: Number
color: String
style: String
Sets the width (width), color (color) and style (style) of the top border.
bottomwidth
color
style
width: Number
color: String
style: String
Sets the width (width), color (color) and style (style) of the bottom border.

To enable the CustomBorders plugin and add a predefined border around a range of cells, set the customBorders option to an array of objects. Each object represents a border configuration for a single range of cells, and has the following properties:

PropertySub-propertiesTypesDescription
rangefrom {row, col}
to {row, col}
from: Object
to: Object
row: Number
col: Number
If the layout direction is LTR (default):
- from selects the range’s top-left corner.
- to selects the range’s bottom-right corner.

If the layout direction is RTL:
- from selects the range’s top-right corner.
- to selects the range’s bottom-left corner.
startwidth
color
style
width: Number
color: String
style: String
If the layout direction is LTR (default): start sets the width (width), color (color) and style (style) of the left-hand border.

If the layout direction is RTL: start sets the width (width), color (color) and style (style) of the right-hand border.
endwidth
color
style
width: Number
color: String
style: String
If the layout direction is LTR (default): end sets the width (width), color (color) and style (style) of the right-hand border.

If the layout direction is RTL: end sets the width (width), color (color) and style (style) of the left-hand border.
topwidth
color
style
width: Number
color: String
style: String
Sets the width (width), color (color) and style (style) of the top border.
bottomwidth
color
style
width: Number
color: String
style: String
Sets the width (width), color (color) and style (style) of the bottom border.

Read more:

Default: false
Example

// enable the `CustomBorders` plugin
customBorders: true,
// enable the `CustomBorders` plugin
// and add a predefined border for a particular cell
customBorders: [
// add an object with a border configuration for one cell
{
// set the cell's row coordinate
row: 2,
// set the cell's column coordinate
col: 2,
// set the left/right border's width and color
start: {
width: 2,
color: 'red'
},
// set the right/left border's width, color and style
end: {
width: 1,
color: 'green',
style: 'dashed'
},
// set the top border's width and color
top: '',
// set the bottom border's width and color
bottom: ''
}
],
// enable the `CustomBorders` plugin
// and add a predefined border for a range of cells
customBorders: [
// add an object with a border configuration for one range of cells
{
// select a range of cells
range: {
// set the range's top-left corner
from: {
row: 1,
col: 1
},
// set the range's bottom-right corner
to: {
row: 3,
col: 4
}
},
// set the left/right border's width, color and style
start: {
width: 2,
color: 'red',
style: 'dashed'
},
// set the right/left border's width and color
end: {},
// set the top border's width and color
top: {},
// set the bottom border's width and color
bottom: {}
}
],

Methods

clearBorders

Source code

customBorders.clearBorders(selectionRanges)

Clear custom borders.

Example

const customBordersPlugin = hot.getPlugin('customBorders');
// Using an array of arrays (produced by `.getSelected()` method).
customBordersPlugin.clearBorders([[1, 1, 2, 2], [6, 2, 0, 2]]);
// Using an array of CellRange objects (produced by `.getSelectedRange()` method).
customBordersPlugin.clearBorders(hot.getSelectedRange());
// Using without param - clear all customBorders.
customBordersPlugin.clearBorders();
ParamTypeDescription
selectionRangesArray<Array>
Array<CellRange>
Array of selection ranges.

destroy

Source code

customBorders.destroy()

Destroys the plugin instance.

disablePlugin

Source code

customBorders.disablePlugin()

Disables the plugin functionality for this Handsontable instance.

enablePlugin

Source code

customBorders.enablePlugin()

Enables the plugin functionality for this Handsontable instance.

getBorders

Source code

customBorders.getBorders(selectionRanges) ⇒ Array<object>

Get custom borders.

Example

const customBordersPlugin = hot.getPlugin('customBorders');
// Using an array of arrays (produced by `.getSelected()` method).
customBordersPlugin.getBorders([[1, 1, 2, 2], [6, 2, 0, 2]]);
// Using an array of CellRange objects (produced by `.getSelectedRange()` method).
customBordersPlugin.getBorders(hot.getSelectedRange());
// Using without param - return all customBorders.
customBordersPlugin.getBorders();
ParamTypeDescription
selectionRangesArray<Array>
Array<CellRange>
Array of selection ranges.

Returns: Array<object> - Returns array of border objects.

isEnabled

Source code

customBorders.isEnabled() ⇒ boolean

Checks if the plugin is enabled in the handsontable settings. This method is executed in Hooks#beforeInit hook and if it returns true then the CustomBorders#enablePlugin method is called.

setBorders

Source code

customBorders.setBorders(selectionRanges, borderObject)

Set custom borders.

Example

const customBordersPlugin = hot.getPlugin('customBorders');
// Using an array of arrays (produced by `.getSelected()` method).
customBordersPlugin.setBorders([[1, 1, 2, 2], [6, 2, 0, 2]], {start: {width: 2, color: 'blue'}});
// Using an array of CellRange objects (produced by `.getSelectedRange()` method).
// Selecting a cell range.
hot.selectCell(0, 0, 2, 2);
// Returning selected cells' range with the getSelectedRange method.
customBordersPlugin.setBorders(hot.getSelectedRange(), {start: {hide: false, width: 2, color: 'blue'}});
ParamTypeDescription
selectionRangesArray<Array>
Array<CellRange>
Array of selection ranges.
borderObjectobjectObject with top, right, bottom and start properties.

updatePlugin

Source code

customBorders.updatePlugin()

Updates the plugin’s state.

This method is executed when updateSettings() is invoked with any of the following configuration options: