You want a cell in your FlexGrid to have a dropdown that contains colors to select. You can accomplish this in different ways, depending on the circumstances of your application. One way is to use Wijmo's InputColor, and another is to use ComboBox, which we will use in this example.
Wijmo's ComboBox control combines an input element with a drop-down list. You can use it to select and/or edit strings or objects from lists.
You can use a similar approach to add any other icons and do customization to change the view on the grid and ComboBox.
Ready to Try it Out? Download Wijmo Today!
Getting Started
Create a FlexGrid as you normally would, and in the column you want to display colors, set these properties:
- header displays the header title
- [binding] are the values the column will bind to in our app.component.ts file
- [editor] is what will edit our column and is assigned to the combo box that is also in our app.component.html file
Add the combo box to the app.component.html file as well and set these properties:
- The #id is what will link the column to this combo box as the editor
- [itemSource] is our data we are binding to
- [displayMemberPath] specifies which member (property) will be displayed as text in the dropdown.
- (initialized) is an event that is fired when the component has been initialized
- (formatItem) fires every time a cell is rendered in the grid and allows the user to modify the contents
Now, we need the code that will get this combo box working. In your app.component.ts file, start by adding this code:
Here, we are adding our module imports, injecting dependencies into the constructor, and assigning the data and colors variables with data.
Next, we will add this code:
The gridInit()
function will be called when the FlexGrid is initialized. Here, we are adding logic to determine the columns' properties and set them accordingly.
Next, we will add the remaining methods to get the other code working:
In this code, we have initCombo,
which is called when the comboBox is initialized. We also have the addCustomColorIcon
function, which is used in our initCombo
function, where it creates a div and adds a class for styling. Finally, we have formatCombo,
which is used as a directive in our app.component.html so an event can format the cells for display.
The completed file should look like this:
These methods format the combo box's data and properties to fit our requirements. Please reference the code and adjust it to fit your needs.
This is our app.component.html file for our example.
This is our data-svc.service.ts file that will help with the data for our FlexGrid.
When running, your FlexGrid should look something like this:
We hope you find this helpful! Please be sure to reference the Wijmo documentation and demos if you need additional information about ComboBox.
Example code can be found here on JS CodeMine.
Ready to Try it Out? Download Wijmo Today!