【问题标题】:How to sort the selected items in a Sitecore Treelist?如何对 Sitecore Treelist 中的选定项目进行排序?
【发布时间】:2014-01-30 16:33:05
【问题描述】:

有没有办法让 Sitecore 树列表中的选定项目始终按字母顺序排序?

【问题讨论】:

    标签: sitecore sitecore6


    【解决方案1】:

    不,但您可以考虑创建自己的“排序树列表”。今天早些时候有人问了一个不同的问题,但答案基本相同:

    Sitecore Tree list datasource - VersionExist

    Sitecore 允许您创建自定义字段类型。它们可以基于现有的,但有一些额外的调整。

    正如另一个问题的答案中提到的,这里有 2 篇文章是不错的起点:

    Creating a Composite Custom Field

    Apply Dynamic TreeList Source Parameters with the Sitecore ASP.NET CMS

    这是我的实现,虽然很长,但主要是从反编译的 Treelist 代码中复制和粘贴的。我已经突出显示了 Value 属性和 Add 方法中的哪些新位:

    namespace CustomFieldTypes
    {
        public class Treelist : Sitecore.Shell.Applications.ContentEditor.TreeList
        {
            public override string Value
            {
                get
                {
                    // ---------- New code here -----------
                    var ids = base.Value.Split('|');
                    var db = Sitecore.Configuration.Factory.GetDatabase("master");
                    var items = ids.Select(id => db.GetItem(id)).Where(item => item != null);
                    var orderedItems = items.OrderBy(item => item.Name);
                    var orderedIds = orderedItems.Select(item => item.ID.ToString());
    
                    return String.Join("|", orderedIds);
                    // ---------------------------------------
                }
                set
                {
                    base.Value = value;
                }
            }
    
            protected void Add()
            {
                if (this.Disabled)
                return;
                string viewStateString = this.GetViewStateString("ID");
                TreeviewEx treeviewEx = this.FindControl(viewStateString + "_all") as TreeviewEx;
                Assert.IsNotNull((object) treeviewEx, typeof (DataTreeview));
                Listbox listbox = this.FindControl(viewStateString + "_selected") as Listbox;
                Assert.IsNotNull((object) listbox, typeof (Listbox));
                Item selectionItem = treeviewEx.GetSelectionItem();
                if (selectionItem == null)
                {
                    SheerResponse.Alert("Select an item in the Content Tree.", new string[0]);
                }
                else
                {
                    if (this.HasExcludeTemplateForSelection(selectionItem))
                    return;
                    if (this.IsDeniedMultipleSelection(selectionItem, listbox))
                    {
                         SheerResponse.Alert("You cannot select the same item twice.", new string[0]);
                    }
                    else
                    {
                        if (!this.HasIncludeTemplateForSelection(selectionItem))
                             return;
                        SheerResponse.Eval("scForm.browser.getControl('" + viewStateString + "_selected').selectedIndex=-1");
                        ListItem listItem = new ListItem();
                        listItem.ID = Sitecore.Web.UI.HtmlControls.Control.GetUniqueID("L");
    
                        // ----- New Code Here -----------------------
                        bool listItemAdded = false;
                        for (int i = 0; i < listbox.Controls.Count; i++ )
                        {
                            ListItem control = (ListItem)listbox.Controls[i];
    
                            if (control == null)
                                return;
    
                            if (String.Compare(GetHeaderValue(selectionItem), control.Header) < 0)
                            {
                                listbox.Controls.AddAt(i, listItem);
                                listItemAdded = true;
                                break;
                            }
                        }
    
                        if (!listItemAdded)
                        {
                            Sitecore.Context.ClientPage.AddControl((System.Web.UI.Control)listbox, (System.Web.UI.Control)listItem);                     
                        }
                        // ------------------------------------------
    
                        listItem.Header = this.GetHeaderValue(selectionItem);
                        listItem.Value = listItem.ID + (object) "|" + (string) (object) selectionItem.ID.ToString();
                        SheerResponse.Refresh((Sitecore.Web.UI.HtmlControls.Control) listbox);
                        SetModified();
                    }
                }
            }
    
            protected static void SetModified()
            {
                Sitecore.Context.ClientPage.Modified = true;
            }
    
            private bool HasIncludeTemplateForSelection(Item item)
            {
                Assert.ArgumentNotNull((object)item, "item");
                if (this.IncludeTemplatesForSelection.Length == 0)
                    return true;
                else
                    return HasItemTemplate(item, this.IncludeTemplatesForSelection);
            }
    
    
            private bool HasExcludeTemplateForSelection(Item item)
            {
                if (item == null)
                    return true;
                else
                    return HasItemTemplate(item, this.ExcludeTemplatesForSelection);
            }
    
            private bool IsDeniedMultipleSelection(Item item, Listbox listbox)
            {
                Assert.ArgumentNotNull((object)listbox, "listbox");
                if (item == null)
                     return true;
                if (this.AllowMultipleSelection)
                     return false;
                foreach (Sitecore.Web.UI.HtmlControls.Control control in listbox.Controls)
                {
                     string[] strArray = control.Value.Split(new char[1]
            {
              '|'
            });
                    if (strArray.Length >= 2 && strArray[1] == item.ID.ToString())
                        return true;
                }
                return false;
            }
    
            private static bool HasItemTemplate(Item item, string templateList)
            {
                Assert.ArgumentNotNull((object)templateList, "templateList");
                if (item == null || templateList.Length == 0)
                    return false;
                string[] strArray = templateList.Split(new char[1]
              {
                 ','
               });
                    ArrayList arrayList = new ArrayList(strArray.Length);
                    for (int index = 0; index < strArray.Length; ++index)
                        arrayList.Add((object)strArray[index].Trim().ToLowerInvariant());
                    return arrayList.Contains((object)item.TemplateName.Trim().ToLowerInvariant());
                }
        }
    }
    

    当它编译并在您的应用程序中时,您需要转到核心数据库中的/sitecore/system/Field types/List Types/Treelist。在那里,您需要填写AssemblyClass 字段,并清除Control 字段。

    【讨论】:

    • 感谢您的回复。但是,我在网上看到的所有示例(包括您发布的示例)都在谈论排序或修改 Treelist 的 source。我不想修改 source 项目(在左侧)。我想修改右侧的项目列表 - 选定的项目。我似乎找不到显示如何操作 Treelist 右侧所选项目的代码示例。
    • 谢谢!那讲得通。现在你向我展示了它似乎很简单。我应该想到的。我会测试一下。
    【解决方案2】:

    您可以创建一个自定义字段并使用泛型对所选列表中的项目进行排序,然后将 guid 保存回该字段。我在a recent blog post 中介绍了这一点。涉及的编码并不多。

    【讨论】:

      猜你喜欢
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多