我有一个下拉列表,UI中的元素按照外观顺序显示
我希望下拉菜单在UI的顶部位置显示Header。
代码用户界面:
<select id="simTextEditorSelection" onchange="ShowTextEditorBasedOnSelection();" style="float:right;">
@foreach (PageInfoMV anItemForEditor in Model.ItemContents)
{
<option value="@anItemForEditor.ItemId">@anItemForEditor.ItemDisplayText</option>
}
我可以在上面的代码中以某种方式提及将特定ID显示为默认值(TOP Position)。
Model.ItemContents:
public List<PageInfoMV> ItemContents
{
get
{
if (this.itemContents == null) { this.itemContents = new List<PageInfoMV>(); }
if (!this.itemContents.Any(x => x.ItemId == Constants.HEADER_ID))
{
this.itemContents.Add(new PageInfoMV() { ItemId = Constants.HEADER_ID, ItemDisplayText = Constants.HEADER_DISPLAY_TEXT });
}
if (!this.itemContents.Any(x => x.ItemId == Constants.FOOTER_ID))
{
this.itemContents.Add(new PageInfoMV() { ItemId = Constants.FOOTER_ID, ItemDisplayText = Constants.FOOTER_DISPLAY_TEXT });
}
return this.itemContents;
}
set { this.itemContents = value; }
}
Constants.cs
public static readonly string HEADER_ID = "-1000";
public static readonly string FOOTER_ID = "-1001";
它只是一个代码段。如果需要更多代码块,请告诉我。感谢。
答案 0 :(得分:1)
如果你的标题选项值是常数,那么你应该在标题上设置。
<select id="simTextEditorSelection">
<option value="Footer">Footer</option>
<option value="Header"> Header</option>
<option value="Blabla">Blabla</option>
</select>
<script>
var val1=$("#simTextEditorSelection").find('option:eq(0)').val();
$('#simTextEditorSelection option[value="Header"]').insertBefore('#simTextEditorSelectionoption[value="'+val1+'"]');
</script>
var val1=$("#simTextEditorSelection").find('option:eq(0)').val();
$('#simTextEditorSelection option[value="Header"]').insertBefore('#simTextEditorSelection option[value="'+val1+'"]');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="simTextEditorSelection">
<option value="Footer">Footer</option>
<option value="Header"> Header</option>
<option value="Blabla">Blabla</option>
</select>
答案 1 :(得分:0)
如果您控制ItemContents
,我建议您添加一个任意值来对内容值进行排名;例如,标题为0,页脚为1等。然后,您可以使用此标题在视图中执行OrderBy()
:
@foreach (PageInfoMV anItemForEditor in Model.ItemContents.OrderBy(ic => ic.OrderValue))
{
...
}
编辑:
剩余物品的可选附加订购:
@foreach (PageInfoMV anItemForEditor in Model.ItemContents.OrderBy(ic => ic.OrderValue)
.ThenBy(ic => ic.SomeOtherValueMaybeName))
{
...
}