相互连接的下拉菜单不起作用的消费者

时间:2017-12-12 07:39:12

标签: dojo episerver

所以我已经按照本教程

https://world.episerver.com/blogs/Duong-Nguyen/Dates/2014/1/Country-Region-drop-down-lists-in-All-properties-mode/

努力创造依赖性下降。

public class LocationBlock : BlockData
{
    [SelectOne(SelectionFactoryType = typeof(CountrySelectionFactory))]
    public virtual string Country { get; set; }

    [SelectOne(SelectionFactoryType = typeof(RegionSelectionFactory))]
    [ClientEditor(ClientEditingClass = "alloy/editors/FilterableSelectionEditor", SelectionFactoryType = typeof(RegionSelectionFactory))]
    public virtual string Region { get; set; }
}
public class ArticlePage : StandardPage
{
    [Display(GroupName = "IndexData")]
    public virtual LocationBlock Location { get; set; }
}
class CountrySelectionFactory : ISelectionFactory
{
    public IEnumerable GetSelections(ExtendedMetadata metadata)
    {
        return new Country[]
        {
        new Country() { CountryCode = "US", Name = "United States" },
        new Country() { CountryCode = "SE", Name = "Sweden" }
        };
    }
}


class RegionSelectionFactory : ISelectionFactory
{
    public IEnumerable GetSelections(ExtendedMetadata metadata)
    {
        return new Region[]
        {
        new Region() { CountryCode = "US", RegionCode = "NY", Name = "New York" },
        new Region() { CountryCode = "US", RegionCode = "CA", Name = "California" },

        new Region() { CountryCode = "SE", RegionCode = "AB", Name = "Stockholm" },
        new Region() { CountryCode = "SE", RegionCode = "O", Name = "Västra Götaland" }
        };
    }
}

class Country : ISelectItem
{
    public string CountryCode { get; set; }
    public string Name { get; set; }

    public string Text
    {
        get
        {
            return Name;
        }
    }

    public object Value
    {
        get
        {
            return CountryCode;
        }
    }
}

class Region : ISelectItem
{
    public string CountryCode { get; set; }
    public string RegionCode { get; set; }
    public string Name { get; set; }

    public string Text
    {
        get
        {
            return Name;
        }
    }

    public object Value
    {
        get
        {
            return String.Format("{0}-{1}", CountryCode, RegionCode);
        }
    }
}
[EditorDescriptorRegistration(TargetType = typeof(LocationBlock))]
public class LocationBlockEditorDescriptor : EditorDescriptor
{
    public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable attributes)
    {
        base.ModifyMetadata(metadata, attributes);

        metadata.Properties.Cast().First().GroupSettings.ClientLayoutClass = "alloy/LocationBlockContainer";

        metadata.Properties.Cast().First().ClientEditingClass = "alloy/editors/FilterableSelectionEditor";
    }
}

之后我在root中创建了一个ClientResources文件夹。在那之下,我创建了Scripts文件夹并在那里放置了LocationBlockConatiner,在Scripts下我创建了另一个名为Editors的文件夹,并在那里放置了我的FilterableSelectionEditor。

我创建了一个module.config文件,其中的代码如下所示

LocationBlockContainer就是这个

define([
"dojo/_base/declare",
"dojo/_base/lang",

"epi/shell/layout/SimpleContainer"
],
function (
    declare,
    lang,

    SimpleContainer
) {

    return declare([SimpleContainer], {
        countryDropdown: null,
        regionDropdown: null,

        addChild: function (child) {
            // Summar: Add a widget to the container

            this.inherited(arguments);

            if (child.name.indexOf("country") >= 0) {
                // If it's the country drop down list
                this.countryDropdown = child;

                // Connect to change event to update the region drop down list
                this.own(this.countryDropdown.on("change", lang.hitch(this, this._updateRegionDropdown)));
            } else if (child.name.indexOf("region") >= 0) {
                // If it's the region drop down list
                this.regionDropdown = child;

                // Update the region drop down
                this._updateRegionDropdown(this.countryDropdown.value);
            }
        },

        _updateRegionDropdown: function (country) {

            console.log(1);

            if (country !== "" && this.previousCountry === "") {
                this.previousCountry = country;
            }
            // Clear the current value
            if (country !== this.previousCountry) {
                this.regionDropdown.set("value", null);
                this.previousCountry = country;
            }


            console.log(this.regionDropdown);

            // Set the filter
            this.regionDropdown.set("filter", function (region) {

                console.log(region);

                return region.value.indexOf(country) === 0;
            });

        }
    });
});

FilterableSelectionEditor.js就是这个

define([
"dojo/_base/declare",
"dojo/_base/array",

"epi-cms/contentediting/editors/SelectionEditor"
],
function (
    declare,
    array,

    SelectionEditor
) {

    return declare([SelectionEditor], {
        _allOptions: null,

        filter: null,

        _setOptionsAttr: function (options) {
            // summary: set the options

            this._allOptions = options;

            this.inherited(arguments, [array.filter(options, this.filter || function () {
                // return all options if no filter function provided.
                return true;
            }, this)]);
        },

        _setFilterAttr: function (filter) {
            // summary: set the option filter function

            this._set("filter", filter);

            this.set("options", this._allOptions);
        }
    });
});

它似乎不起作用。当我选择国家时,区域被清空,然后再次添加每个区域,因为最终结果显示所有区域,无论我选择了哪个国家/地区。

这里的任何帮助都会非常感激。

0 个答案:

没有答案