Hello!
I wish to add functionality to the MultiSelect in order to capture the "paste" event, parse the input and if any values in the pasted input match are options to the MutliSelect, select them.
Below is the code I used to extend the MutliSelect. The parsingMutliSelect.value(...) call is indeed changing the values, however the UI is not updating and displaying the values as tags within the input field for the MultiSelect. In fact, the error "TypeError: Argument 1 of Node.removeChild is not an object" keeps occurring when the parsingMultiSelect._change() call is made. This error occurs at the line "tagList[0].removeChild(tagList[0].children[removedItem.position]);" within the _selectValue function definition within the MultiSelect configuration (kendo.multiselect.js).
Using my FireBug console, I can successfully get the MutliSelect to perform as expected by using the .value() and .trigger("change") methods on the MultiSelect instance. Why can I not seem to successfully set values from within the MutliSelect extension definition?
01.
(
function
($) {
02.
03.
var
kendo = window.kendo,
04.
ui = kendo.ui,
05.
MultiSelect = ui.MultiSelect,
06.
CHANGE =
"change"
,
07.
PASTE =
"paste"
;
08.
09.
var
ParsingMultiSelect = MultiSelect.extend({
10.
11.
init:
function
(element, options) {
12.
var
that =
this
;
13.
14.
// Base call to MultiSelect to initialize ParsingMultiSelect
15.
MultiSelect.fn.init.call(that, element, options);
16.
17.
that.input.on(PASTE, that, that._paste);
18.
},
19.
20.
options: {
21.
// Assigns the name as it should appear off the kendo namespace (i.e. kendo.ui.ParsingMultiSelect).
22.
// The jQuery plugin would be jQuery.fn.kendoParsingMultiSelect
23.
name:
"ParsingMultiSelect"
24.
},
25.
26.
events: [
27.
PASTE
28.
],
29.
30.
_paste:
function
(e) {
31.
alert(
"data pasted!"
);
32.
var
parsingMultiSelect = e.data,
33.
dataValueField = parsingMultiSelect.options.dataValueField,
34.
parsingMultiSelectDataSource = parsingMultiSelect.dataSource,
35.
pastedData = e.originalEvent.clipboardData.getData(
'text'
),
36.
validValues = [];
37.
38.
multiSelect._close();
39.
40.
parsingMultiSelectDataSource.fetch().done(
function
() {
41.
var
dataSourceData = parsingMultiSelectDataSource.data();
42.
var
dataSourceValues = [];
43.
44.
if
(dataSourceData
instanceof
kendo.data.ObservableArray) {
45.
dataSourceValues = dataSourceData.map(
function
(arrayElement, index) {
46.
return
arrayElement[dataValueField];
47.
});
48.
}
49.
50.
// Convert all commas to a single space then reduce all whitespace (and consecutive)
51.
// characters in the pasted input to a single space to get ready for splitting
52.
pastedData = pastedData.replace(/,/g,
""
).replace(/\s\s+/g,
" "
).trim();
53.
54.
pastedValues = pastedData.split(
" "
);
55.
56.
// Loop through the values pasted in. If any exist in the dataSource, push them to
57.
// the array of valid values.
58.
$.each(pastedValues,
function
(index, value) {
59.
if
(dataSourceValues.indexOf(value) != -1) {
60.
validValues.push(value);
61.
}
62.
});
63.
64.
parsingMultiSelect.value(e.data.value().concat(validValues));
65.
parsingMultiSelect._change();
66.
});
67.
68.
e.preventDefault();
69.
}
70.
});
71.
72.
// Add the ParsingMultiSelect to Kendo UI
73.
ui.plugin(ParsingMultiSelect);
74.
75.
})(jQuery);
76.
77.
78.
$(
"#parsingmultiselect"
).kendoParsingMultiSelect({
79.
dataSource: [
80.
{ unitId:
"0001X"
},
81.
{ unitId:
"0002X"
},
82.
{ unitId:
"0003X"
},
83.
{ unitId:
"0004X"
},
84.
{ unitId:
"0005X"
},
85.
{ unitId:
"0006X"
}
86.
],
87.
dataTextField:
"unitId"
,
88.
dataValueField:
"unitId"
,
89.
placeholder:
"Select a Unit ID..."
90.
});
...and a jsFiddle: https://jsfiddle.net/Lvvwzmyv/3/. Try pasting a single value: "0002X" and try pasting multiple values: "0002X, 0004X, 0005X" and observe the unintended behaviour.
Thank you,
R. J.