如何在 Sitecore 模板字段中使用下拉列表类型

How to use Droplist types in Sitecore template fields

有谁知道如何在模板字段中使用 "Droplist" 类型吗?

我猜 "Droplist" 与 <select><option></option></select> 类型相同。

我想指定 select 具有静态值的列表类型,以便 Sitecore 编辑者在创建页面时可以 select 仅从众多可用列表中选择一个。 我的计划是在列表 (<select>) 中添加 CSS class 名称 (<option>),编辑器将通过 select 使用其中一种样式.

如何在 select 列表中添加值?我必须写代码吗?

DroplistDroplink 字段类型相似,因为它们都是下拉列表。 Droplist 将仅存储项目的名称(因此该项目不会有 link),而 Droplink 存储项目的 ID。这意味着如果您重命名一个选项,或将其移动到您的内容树中的其他位置,Droplist 将不会更新(导致可能损坏 links),Droplink 将更新。

您可以通过将模板中的 Datasource 字段设置为某些内容来向 Droplist 添加值(例如 /sitecore/content/Home/CSS/ 如果您希望将 CSS class 个名字)。

您可以像这样在代码中访问 Droplist

Item item = Sitecore.Context.Item;
string css = item["FieldName"]; // Also possible is item.Fields["Fieldname"].Value;

A Droplink 可以这样访问:

string dropDownItemId = item["Fieldname"]; // Or, again, item.Fields["Fieldname"].Value; if you prefer
var cssItem = Sitecore.Context.Database.GetItem(dropDownItemId); // And now you can
// access any fields in this item.

编辑 A good article going into some more detail in the differences between Droplink and Droplist

我会完全按照@Trayek 在他的解决方案中建议的那样进行。要对此进行扩展,这应该是您的实施:

您创建自己的模板,可能名为 CSS Class,您将向其中添加一个 Single-Line 文本字段,也许称为 Value。创建项目时,您将在该字段中放置实际的 CSS 类。

接下来,您将在内容树中的某处添加一个文件夹项目,并将您的 CSS Class 项目添加到该文件夹​​。该文件夹也将成为您 droplist/droplink 的数据源。

要设置数据源,this blog post 应该会有帮助。您将希望在 For Fields without Search 下使用 number 1(下方包含屏幕截图)。我不久前写了这篇文章,所以如果您需要任何其他帮助,请告诉我。