As more and more companies collect various types of information, the need for data analysis tools, such as spreadsheets, is rapidly increasing. Because of this data volume, it is imperative to ensure data accuracy and consistency to provide consistent and reliable analysis. One way to do so is to create fields in a spreadsheet linking to specific data selections, both large and small, to ensure consistency with spelling, formatting, and definitions.
To help facilitate this concept, SpreadJS supports the use of CellTypes, which creates different types of cells in SpreadJS, including buttons, combo boxes, checkboxes, radio buttons, and much more. Here is a link to our documentation, which provides information on these cell types. This list contains CustomCellType–allowing for the creation of several new cell types to achieve different functionalities within our JavaScript spreadsheet.
In this blog, we discuss using the CellTypes concept to create a custom AutoCompleteCellType utilizing the select2() jquery library to implement an auto-complete drop-down list. As the name suggests, AutoCompleteCellType, when applied to a cell in Edit mode, lets a user begin typing to populate a list of values from the drop-down box. The user can select one of these options to set as the cell value. The options are auto-populated and displayed from the Select2 library, allowing users to connect to a remote database using ajax methods. Let's get started with a simple example to create a cell in a spreadsheet such as this:
In a nutshell:
- Implement the AutoCompleteCellType
- Create the editor element
- Paint the cell on the canvas
- Activate and deactivate the editor
- Get and set the editor value
- Handle reserved keyboard keys
Getting started:
1. Implement the AutoCompleteCellType
2. Create the editor element
First, create a div element and append a select element with set attributes to create the editor element. Then, use the SpreadJS getCellRect method to display the cell's width and height in the new editor accordingly.
Finally, add event listeners from select2, so SpreadJS stops editing the active cell using the endEdit method when a result is selected. Also, when the select2 drop-down is open, it will set the attributes for the select2-dropdown.
3. Paint the cell on the canvas
Use the paint method of SpreadJS' Base class to paint a cell on the canvas. For this example, use the id and value of the value and display them based on the selected item from the drop-down.
4. Activate and deactivate the editor
Next, we show how to activate the editor, using the activateEditor method, to search for and process a searched term's results. The Select2 library supports AJAX methods, which are used to add a remote data source to the select drop-down, based on the search term. To accomplish this, first find the select2 element and add our remote data source by using an ajax call to a specified URL. Then, process the results in a format select2 can understand. For this example, return the item's country name and numericCode.
To deactivate the editor, use the SpreadJS deactiveEditor method and use the select2 libraries destroy method to remove the select2 widget:
5. Get and set the editor value
SpreadJS' getEditorValue method receives the editor value and assigns the id and text as the id and value. Then, set the editor's value using the setEditorValue and append the selected items with the returned id and value.
6. Handle reserved keyboard keys
When a user is typing, and the results are populating in the select drop-down, SpreadJS needs to handle the keyboard arrow keys and escape. To do this, use SpreadJS isReserveKey method. This is used to determine if the cell type handles the keyboard event itself. In this case, use the keycode numbers 40, 38, 13, and 27–representing the down, up, end, and escape keys.
Outcome:
With this code-logic applied, users can set a cell or range of cells to their cell type to be set as the custom autoCompleteCellType like so:
When users type in the cells that have the autoCompleteCellType applied to them, the new editor appears, and the drop-down is auto-populated based on what has been entered:
Here is a link to a live sandbox showing these steps in action: https://codesandbox.io/s/spreadjs-autocomplete-celltype-blog-sample-enter-country-g402p?file=/src/index.js
This is just one example SpreadJS' customization options used to create custom cell types. SpreadJS' JavaScript API enables users to add customized behavior to a JavaScript spreadsheet to fit their needs. Developers can even implement different libraries (similarly to how Select2 was implemented here) to achieve different behaviors based on their requirements.
Get started developing with the world's #1 JavaScript spreadsheet, SpreadJS, with a trial today!