Single selection for dynamic item field control selector
Hi,
The Thunder template "Sitefinity Dynamic Items Field Control Selector" works with "Array of GUIDs" field type, displaying a dialog box allowing the user to multi-select items.
Is there another template or a simple way to modify the generated code to only allow the user to select only one item? Also, I suppose for fields that should allow the user to select only one item, the type should be "GUID" instead of "Array of GUIDs", isn't it?
Thanks,
Michael
Hi Michael,
Yes you are correct. If you want to have a single item selector, you need to bind it to field of type Guid.
However, you can make a simple modification over the currently generated dynamic item field control selector, to restrict its multiple selection. What you need to do is:
1. Find the FlatSelector in the ControlSelector template and set its AllowMultipleSelection property to false:
AllowMultipleSelection="false"
/*push newly selected items in the observable array*/
var selectedItems = this.get_itemsSelector().getSelectedItems();
this._selectedItems.items.empty();
for (var i = 0; i < selectedItems.length; i++)
this._selectedItems.items.push(selectedItems[i]);
Hi Kristian,
This line:
this._selectedItems.items.empty();
is giving me the following error "TypeError: this._selectedItems.item.empty is not a function" when I click on "Done".
I tried to replaced it with:
this._selectedItems = kendo.observable( items: [] );
which I took from the top of the JavaScript file. I can then click on "Done", but this does not work either.
Thanks,
Michael
This seems to do the trick.
var data = this._selectedItems.toJSON();
for (var i = 0; i < data.items.length; i++)
this._selectedItems.items.splice(i, 1);
Michael
Hi Michael,
I tested the sample on my side before sending you my recommendations. I am not sure what went wrong
on your side, but I am glad you are okay now. If you have any other problems do not hesitate to contact us.
Happy holidays,
Kristian Smilenov
Telerik
Thanks Kristian,
Happy holidays.
Michael
If you're like me and you run into a deserialization error when you try to publish content with a single-select dynamic item field control, there's one more tweak to make in the JS:
/*Gets the value of the field control.*/
get_value:
function
()
/*on publish if we have items in the kendo observable array
we get their ids in a aray of Guids so that they can be persisted*/
var
selectedKeysArray =
new
Array();
var
data =
this
._selectedItems.toJSON();
for
(
var
i = 0; i < data.items.length; i++)
selectedKeysArray.push(data.items[i].OriginalContentId);
if
(selectedKeysArray.length > 0)
// return selectedKeysArray;
// just return a single item here
return
selectedKeysArray[0];
else
return
null
;
,
Also, if the selected value isn't showing up in your form, you may need to update the way the filterExpression is constructed in set_value:
var
filterExpression =
'OriginalContentId == '
+ value.toString();
Jon, thanks so much for posting that. New Sitefinity user and this was driving me nuts trying to figure out the deserialization error I was getting.
Turns out that I was able to get it working, but I had to create a "SmallText" field instead of a "Related Data Field"