如何在 ASPNET MVC 中制作具有重新排列功能的复选框?

发布于 2024-11-18 19:07:12 字数 310 浏览 9 评论 0原文

我有一个要求,需要在 ASPNET MVC 中具有重新排列功能的复选框。

主要目的是了解用户选择了哪个复选框,并允许他们根据需要重新排列复选框项目顺序。

我的问题是,mvc(或jquery)中是否有类似的现有控件?如果没有,是否有更好的 UI 控件来实现我的目的?

我发现类似的问题没有合适的答案。 带有复选框的列表框

我请求您的回复。谢谢。

I've got a requirement that needs a checkbox with rearranging capability in ASPNET MVC.

The main purpose is to know which checkbox users selected and allow them to rearrange the checkbox item order as they want.

My Question is, do there any existing control like this available in mvc(or jquery)? If no, do there any better UI control to fulfill my purpose?

I found the similar question with no appropriate answer.
listbox with checkboxes

I beg for your response. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彡翼 2024-11-25 19:07:12

为什么不在 jQuery-UI Sortable< 中使用普通复选框 /strong>

<ul id="sortable" class='ckb-list'>
  <li><input id="chb-1" type="checkbox" value="1"/> Checkbox 1</li>
  <li><input id="chb-2" type="checkbox" value="2"/> Checkbox 2</li>
  <li><input id="chb-3" type="checkbox" value="3"/> Checkbox 3</li>
  <li><input id="chb-4" type="checkbox" value="4"/> Checkbox 4</li>
  <li><input id="chb-5" type="checkbox" value="5"/> Checkbox 5</li>
</ul>

然后申请

$(function() {
  // sortable methods
  $( "#sortable" ).sortable();
  $( "#sortable" ).disableSelection();

  // retrieve selection list
  $(".btn-save").bind("click", function() {

    var ids = "",
        iBox = 0;

    // foreach input in .ckb-list
    $(".ckb-list li > input").each(function() {

      if($(this).prop("checked")) {
        // it checked, so, we save
        ids += ++iBox + ". " + $(this).attr("id") + "\n";
      }

    });

    alert("the checkboxes selected and sorted are:\n\n" + ids);
  });
});

JSBIN 中的实时示例

您可以轻松转换 具有剃刀语法的复选框 5

@Html.Checkbox("chb-5", Model.CheckBox5Value)

为了持久化,您需要做的就是按照正确的顺序编写 HTML,让我们想象一下您有这个表:

[TblUserOptions] 
UserOption_id
Option_id
Value
Order

您需要做的就是:

<ul id="sortable" class='ckb-list'>
@foreach(var option in Model.TblUserOptions
                            .OrderBy(x => x.Order)
                            .ThenByDescending(x => x.Option_id))
{
    <li>@Html.Checkbox("chb-" + option.Option_id, option.Value)</li>
}
</ul>

您将始终将复选框正确排序开始,保存后您需要做的就是循环遍历所有复选框并更新新的 Order 值。

why don't you use the normal checkbox with the jQuery-UI Sortable?

<ul id="sortable" class='ckb-list'>
  <li><input id="chb-1" type="checkbox" value="1"/> Checkbox 1</li>
  <li><input id="chb-2" type="checkbox" value="2"/> Checkbox 2</li>
  <li><input id="chb-3" type="checkbox" value="3"/> Checkbox 3</li>
  <li><input id="chb-4" type="checkbox" value="4"/> Checkbox 4</li>
  <li><input id="chb-5" type="checkbox" value="5"/> Checkbox 5</li>
</ul>

and then apply

$(function() {
  // sortable methods
  $( "#sortable" ).sortable();
  $( "#sortable" ).disableSelection();

  // retrieve selection list
  $(".btn-save").bind("click", function() {

    var ids = "",
        iBox = 0;

    // foreach input in .ckb-list
    $(".ckb-list li > input").each(function() {

      if($(this).prop("checked")) {
        // it checked, so, we save
        ids += ++iBox + ". " + $(this).attr("id") + "\n";
      }

    });

    alert("the checkboxes selected and sorted are:\n\n" + ids);
  });
});

Live example in JSBIN

You can easily convert the <input id="chb-5" type="checkbox" value="5"/> Checkbox 5 with the razor syntax:

@Html.Checkbox("chb-5", Model.CheckBox5Value)

for persistence all you need to do is write the HTML is the correct order, let's image you have this table:

[TblUserOptions] 
UserOption_id
Option_id
Value
Order

all you need to do is:

<ul id="sortable" class='ckb-list'>
@foreach(var option in Model.TblUserOptions
                            .OrderBy(x => x.Order)
                            .ThenByDescending(x => x.Option_id))
{
    <li>@Html.Checkbox("chb-" + option.Option_id, option.Value)</li>
}
</ul>

You will always have the checkboxes sorted correctly on start, upon save all you need to do is loop through all checkboxes and update the new Order value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文