+1-888-365-2779
Try Now
More in this section

Forums / Thunder / Single selection for dynamic item field control selector

Single selection for dynamic item field control selector

9 posts, 1 answered
  1. Michael
    Michael avatar
    41 posts
    Registered:
    07 Nov 2013
    19 Dec 2013
    Link to this post
    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
  2. Kristian Smilenov
    Kristian Smilenov avatar
    32 posts
    Registered:
    18 Mar 2015
    21 Dec 2013
    Link to this post
    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
  3. Michael
    Michael avatar
    41 posts
    Registered:
    07 Nov 2013
    23 Dec 2013
    Link to this post
    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
  4. Michael
    Michael avatar
    41 posts
    Registered:
    07 Nov 2013
    23 Dec 2013
    Link to this post
    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
    Answered
  5. Kristian Smilenov
    Kristian Smilenov avatar
    32 posts
    Registered:
    18 Mar 2015
    24 Dec 2013
    Link to this post
    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
  6. Michael
    Michael avatar
    41 posts
    Registered:
    07 Nov 2013
    24 Dec 2013
    Link to this post
    Thanks Kristian,

    Happy holidays.
    Michael
  7. Jon
    Jon avatar
    2 posts
    Registered:
    16 Mar 2011
    28 Mar 2014
    Link to this post

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

  8. Mike
    Mike avatar
    10 posts
    Registered:
    19 Feb 2014
    12 Apr 2014 in reply to Jon
    Link to this post

    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.

  9. Adam
    Adam avatar
    23 posts
    Registered:
    05 Apr 2013
    20 Sep
    Link to this post
    Turns out that I was able to get it working, but I had to create a "SmallText" field instead of a "Related Data Field"
9 posts, 1 answered