我正在使用Web表单(ASP.NET / C#)编写Web应用程序。我有一个使用jquery插件的ListBox控件。我使用数据库调用填充代码隐藏中的列表框。它工作正常,所以我想将组添加到ListBox。数据显示在列表中,而不是我设置的组中 我相信问题是选择的查询插件。我需要以某种方式设置这个插件的选项,但我没有看到任何关于如何做的文档。 这是我的javascript / HTML代码:
<script type="text/javascript">
$(document).ready(function () {
$(".chosen-select").chosen({
search_contains: true,
no_results_text: "Sorry, no match!",
allow_single_deselect: true
});
$('.chosen-container').css('width', '600px');
});
</script>
<asp:ListBox ID="lstBoxTo" runat="server" SelectionMode="Multiple"
data-placeholder="Choose recipient(s)…" multiple="true" class="chosen-select">
</asp:ListBox>
这是填充ListBox的C#代码:
foreach (DataRow row in m_dtRecipients.Rows)
{
ListItem recItem = new ListItem(row["Name"].ToString(), row["ID"].ToString());
if (row["UserID"].ToString().Equals("Global"))
{
recItem.Attributes[OPT_GROUP_ATTRIBUTE] = GLOBAL_GROUP;
}
else if (row["UserID"].ToString().Equals(m_strUserID))
{
recItem.Attributes[OPT_GROUP_ATTRIBUTE] = PERSONAL_GROUP;
}
else
{
recItem.Attributes[OPT_GROUP_ATTRIBUTE] = INDIVIDUAL_GROUP;
}
lstBoxTo.Items.Add(recItem);
}
数据正确,ListBox显示数据但不显示组。 如何获取所选的jquery插件以显示组中的数据?
感谢。
更新
我了解到ListBox和DropDownList不支持optgroup。所以我想尝试这个解决方案但是我无法理解javascript。 在后面的代码中,属性被添加到每个ListItem:
foreach (ListItem item in ((DropDownList)sender).Items)
{
if (System.Int32.Parse(item.Value) < 5)
item.Attributes.Add("classification", "LessThanFive");
else
item.Attributes.Add("classification", "GreaterThanFive");
}
这是javascript
<script>
$(document).ready(function() {
//Create groups for dropdown list
$("select.listsmall option[@classification='LessThanFive']").wrapAll("<optgroup label='Less than five'>");
$("select.listsmall option[@classification='GreaterThanFive']").wrapAll("<optgroup label='Greater than five'>");
});
我不明白&#34; select.listsmall&#39;代表。我尝试使用我的ListBox ID,但我得到一个例外。 任何人都可以解释这部分的JavaScript吗? 感谢。
更新 这就是我如何使用上面的代码隐藏和javascript:
private const string OPT_GROUP_ATTRIBUTE = "grouping";
private const string GLOBAL_GROUP = "Global Groups";
private const string PERSONAL_GROUP = "Personal Groups";
private const string INDIVIDUAL_GROUP = "Individuals";
foreach (DataRow row in m_dtRecipients.Rows)
{
ListItem recItem = new ListItem(row["Name"].ToString(), row["ID"].ToString());
if (row["UserID"].ToString().Equals("Global"))
{
recItem.Attributes.Add(OPT_GROUP_ATTRIBUTE, GLOBAL_GROUP);
}
else if (row["UserID"].ToString().Equals(m_strUserID))
{
recItem.Attributes.Add(OPT_GROUP_ATTRIBUTE, PERSONAL_GROUP);
}
else
{
recItem.Attributes.Add(OPT_GROUP_ATTRIBUTE, INDIVIDUAL_GROUP);
}
lstBoxTo.Items.Add(recItem);
}
这是ListBox HTML:
<asp:ListBox ID="lstBoxTo" runat="server" SelectionMode="Multiple"
data-placeholder="Choose recipient(s)…" multiple="true" class="chosen-select">
</asp:ListBox>
这是javascript:
$(document).ready(function () {
$(".chosen-select").chosen({
search_contains: true,
no_results_text: "Sorry, no match!",
allow_single_deselect: true,
group: true
});
$('.chosen-container').css('width', '600px');
//Create groups for dropdown list
$("select.chosen-select option[@grouping='Global Groups']").wrapAll("<optgroup label='Global Groups'>");
$("select.chosen-select option[@grouping='Personal Groups']").wrapAll("<optgroup label='Personal Groups'>");
$("select.chosen-select option[@grouping='Individuals']").wrapAll("<optgroup label='Individuals'>");
});
我有什么遗漏或错误吗?
更新 好吧,如果我删除了&#39; @&#39;从attribute = value,它不会抛出异常,但它也不会对列表进行分组。
答案 0 :(得分:0)
我发现了我的问题...所选的jquery配置功能必须在&#39; wrapAll&#39;之后。功能。这是改变:
$(document).ready(function () {
//Create groups for dropdown list
$(".chosen-select option[grouping='GlobalGroups']").wrapAll("<optgroup label='Global Groups'>");
$(".chosen-select option[grouping='PersonalGroups']").wrapAll("<optgroup label='Personal Groups'>");
$(".chosen-select option[grouping='Individuals']").wrapAll("<optgroup label='Individuals'>");
//Configure the ListBox using the 'chosen' jquery plugin
$(".chosen-select").chosen({
search_contains: true,
no_results_text: "Sorry, no match!",
allow_single_deselect: true
});
$('.chosen-container').css('width', '600px');
});