如何对Sitecore Treelist中的选定项目进行排序?

时间:2014-01-30 16:33:06

标签: sitecore sitecore6

是否有办法让Sitecore Treelist中的所选项目按字母顺序排序?

2 个答案:

答案 0 :(得分:0)

不,但您可以查看创建自己的“已排序的树形图”。今天早些时候有人提出了一个不同的问题,但答案基本相同:

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字段。

答案 1 :(得分:0)

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