In Spring 2013 (see Release Note) KendoUI introduced a new widget: MultiSelect. Similar to a Drop Down List but this time you see all selected options as some sort of buttons with an (x) for deleting it and a Drop Down List for adding new items.
But as well as it happens with dates or booleans, you do not have a Kendo UI out-of-the-box support of input fields of this type, in a Kendo UI Grid. If you want to use a MultiSelect, DatePicker, AutoComplete, CheckBox,… you need to write and editor function.
KendoUI: MultiSelect
Using MultiSelect widget for an input field is pretty simple, you just need to define an HTML select and invoke the kendoMultiSelect initialization with little more than a DataSource that is an array of valid options.
<select id="select"></select>
$("#select").kendoMultiSelect({ dataSource: [ "Africa", "Europe", "Asia", "North America", "South America", "Antarctica", "Australia" ] });
And if we display what it stores, we can see that is is an array of strings.
KendoUI: MultiSelect, saving ids
But in the previous case the DataSource is just an array of strings but you might have both text: what you want to show; and value what you want to store. You configure which field is used for text in dataTextField and which for value with dataValueField
If so, you define the DataSource as:
$("#select").kendoMultiSelect({ dataTextField : "text", dataValueField: "id", dataSource : [ { text: "Africa", id: 1 }, { text: "Europe", id: 2 }, { text: "Asia", id: 3 }, { text: "North America", id: 4 }, { text: "South America", id: 5 }, { text: "Antarctica", id: 6 }, { text: "Australia", id: 7 } ] });
Now, when we display the values, we get:
KendoUI Grid with MultiSelect field
Now, it is time for using this MultiSelect in a Grid with the considerations above.
KendoUI Grid displaying MultiSelect cell
1. We have seen that values are stored in an array in KendoUI MultiSelect, so we cannot directly show them in a Grid cell or it will be displayed as an array.
2. This is actually easy to solve, we use a template in the column definition that serialize MultiSelect value to a string separated by commas using join method.
columns : [ ... { field: "Cities", width: 300, template: "#= Cities.join(', ') #" } ]
KendoUI Grid editing MultiSelect cell
Now, we have to inform KendoUI Grid how we want a multi-value cell to be edited: edit using a MultiSelect; and we do this using editor attribute in the column definition.
columns : [ ... { field: "Cities", width: 300, template: "#= Cities.join(', ') #", editor: citiesEditor } ]
The only remaining thing is define citiesEditor that is pretty simple:
function citiesEditor(container, options) { $("<select multiple='multiple' data-bind='value : Cities'/>") .appendTo(container) .kendoMultiSelect({ dataSource: CitiesDS }); }
Generate an HTML select with option multiple and bind it to field Cities doing data-bind=’value : Cities’. Now our Grid while editing the list of Cities looks like:
So, it turned out not to be so difficult. Although that is still pending the case when we want to save the id instead of the text. This will be covered in the second part of this post.
Wow this is exactly the functionality I am look for, but unfortunately the limited code examples has left me guessing how to do this. 😦
What is what you miss… maybe I can help you 😀
Hi,
In your example by taking groupable = true, then the cities column is not grouping. Please can you solve this issue.
Correct! grouping by cities fails. But what does it mean group by an array of cities? all cities the same? All cities the same and same order? Do you really need to group by a multiselect?
Please can you post how to do the grouping on multiselect column. 😦
Did you try answering the question on what means “grouping by a multivalue”?
Sir , Can we change the grid records based on dropdown selection. and while adding record to grid i want to send dropdown value along with gird data.
Exmaple like this : http://jsfiddle.net/jddevight/hzvQn/
Very nice – thanks.
I however am stuck on this:
columns : [
…
{ field: “Cities”, width: 300, template: “#= Cities.join(‘, ‘) #”, editor: citiesEditor }
]
I’m getting an error: “Uncaught TypeError: Object has no method ‘join’ ”
I’m not sure what I’m doing wrong, any help would be greatly appreciated!
Lisa
What is different in your code compared with mine? How are you sending the information from the server (how the JSON looks like)?
Just wanted to say I got this working, this blog helped me a lot.
Very nice. Thanks!
Wondering how can I filter multi selct column.
As far as I know this is not supported by KendoUI since you cannot define custom filter functions. So you should do some programming for filtering values and probably you will have some trade offs as if you want to select multiple cities in the same record or records with any of the cities selected. Also questions as what to do if you search for Orleans, should it show just Orleans or also New Orleans.