Single selection for dynamic item field control selector

Posted by Community Admin on 05-Aug-2018 23:35

Single selection for dynamic item field control selector

All Replies

Posted by Community Admin on 19-Dec-2013 00:00

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

Posted by Community Admin on 21-Dec-2013 00:00

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"
2.  Find it's javascript file: and on this method: _doneButtonClicked clear the array of currently selected items this way: 
/*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]);

This should be enough to make it work for single item. 

Regards,
Kristian Smilenov
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 23-Dec-2013 00:00

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

Posted by Community Admin on 23-Dec-2013 00:00

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

Posted by Community Admin on 24-Dec-2013 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 24-Dec-2013 00:00

Thanks Kristian,

Happy holidays.
Michael

Posted by Community Admin on 28-Mar-2014 00:00

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();

Posted by Community Admin on 12-Apr-2014 00:00

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.

Posted by Community Admin on 20-Sep-2016 00:00

Turns out that I was able to get it working, but I had to create a "SmallText" field instead of a "Related Data Field"

This thread is closed