如何通过数组排序制作可排序的复选框列表

发布于 2024-11-26 01:13:04 字数 3312 浏览 1 评论 0原文

我有这个 html 代码:

<div id="format">
<ul id="sortable">
<li id="item1">
      <input class="checkbox" id="item1" name="params[checklist][]" value="item1" checked="checked" type="checkbox">
      <label for="item1">Item 1</label>
</li>
<li id="item2">
      <input class="checkbox" id="item2" name="params[checklist][]" value="item2" checked="checked" type="checkbox">
      <label for="item2">Item 2</label>
</li>
<li id="item3">
      <input class="checkbox" id="item3" name="params[checklist][]" value="item3" checked="checked" type="checkbox">
      <label for="item3">Item 3</label>
      </li>
</ul>
</div>
<input name="params[positions]" id="paramspositions" value="item2,item3,item1" class="text_area" size="50" type="text" />

和这个 jquery/ui 代码:

jQuery.noConflict();
//function that restores the list order from #paramspositions default value
function restoreOrder() {
    var list = jQuery("#format ul");
    if (list == null) return

    var old = jQuery("#paramspositions").val();
    // make array from saved order
    var IDs = old.split(",");

    // fetch current order
    var items = list.sortable("toArray");

    // make array from current order
    var rebuild = new Array();
    for (var v = 0, len = items.length; v < len; v++) {
        rebuild[items[v]] = items[v];
    }

    for (var i = 0, n = IDs.length; i < n; i++) {

        // item id from saved order
        var itemID = IDs[i];

        if (itemID in rebuild) {

            // select item id from current order
            var item = rebuild[itemID];

            // select the item according to current order
            var child = jQuery("ul.ui-sortable").children("#" + item);

            // select the item according to the saved order
            var savedOrd = jQuery("ul.ui-sortable").children("#" + itemID);

            // remove all the items
            child.remove();

            // add the items in turn according to saved order
            // we need to filter here since the "ui-sortable"
            // class is applied to all ul elements and we
            // only want the very first!  You can modify this
            // to support multiple lists - not tested!
            jQuery("ul.ui-sortable").filter(":first").append(savedOrd);
        }
    }
}

jQuery(function() {

    jQuery("#sortable").sortable({
        opacity: 0.6,
        update: function(event, ui) {
            aggiornaTesto();
        }
    });

    //function that updates #paramspositions value on check/uncheck and on moving checkbox
    function aggiornaTesto() {
        ids = [];
        jQuery("input:checkbox:checked").each(function(n) {
            ids.push(jQuery(this).attr("value"));
        });
        jQuery("#paramspositions").val(ids);
    }

    // here, we reload the saved order
    restoreOrder();
    jQuery(".checkbox").button();
    jQuery("#sortable input").click(aggiornaTesto);
}); // JavaScript Document

这些是我的问题:

  • 在这种情况下,除了在检查/取消选中时更新输入值之外,所有工作都有效,
  • 如果我删除 li 标签上的 id,除了在加载时恢复订单之外,所有工作都有效

我怎样才能做到这一点工作?基本上我需要一个可排序的复选框列表,在加载页面上,列表顺序取决于数组,在选中/取消选中和移动项目时,它会更新输入值。 非常感谢你,对我的英语感到抱歉。

I've this html code:

<div id="format">
<ul id="sortable">
<li id="item1">
      <input class="checkbox" id="item1" name="params[checklist][]" value="item1" checked="checked" type="checkbox">
      <label for="item1">Item 1</label>
</li>
<li id="item2">
      <input class="checkbox" id="item2" name="params[checklist][]" value="item2" checked="checked" type="checkbox">
      <label for="item2">Item 2</label>
</li>
<li id="item3">
      <input class="checkbox" id="item3" name="params[checklist][]" value="item3" checked="checked" type="checkbox">
      <label for="item3">Item 3</label>
      </li>
</ul>
</div>
<input name="params[positions]" id="paramspositions" value="item2,item3,item1" class="text_area" size="50" type="text" />

and this jquery/ui code:

jQuery.noConflict();
//function that restores the list order from #paramspositions default value
function restoreOrder() {
    var list = jQuery("#format ul");
    if (list == null) return

    var old = jQuery("#paramspositions").val();
    // make array from saved order
    var IDs = old.split(",");

    // fetch current order
    var items = list.sortable("toArray");

    // make array from current order
    var rebuild = new Array();
    for (var v = 0, len = items.length; v < len; v++) {
        rebuild[items[v]] = items[v];
    }

    for (var i = 0, n = IDs.length; i < n; i++) {

        // item id from saved order
        var itemID = IDs[i];

        if (itemID in rebuild) {

            // select item id from current order
            var item = rebuild[itemID];

            // select the item according to current order
            var child = jQuery("ul.ui-sortable").children("#" + item);

            // select the item according to the saved order
            var savedOrd = jQuery("ul.ui-sortable").children("#" + itemID);

            // remove all the items
            child.remove();

            // add the items in turn according to saved order
            // we need to filter here since the "ui-sortable"
            // class is applied to all ul elements and we
            // only want the very first!  You can modify this
            // to support multiple lists - not tested!
            jQuery("ul.ui-sortable").filter(":first").append(savedOrd);
        }
    }
}

jQuery(function() {

    jQuery("#sortable").sortable({
        opacity: 0.6,
        update: function(event, ui) {
            aggiornaTesto();
        }
    });

    //function that updates #paramspositions value on check/uncheck and on moving checkbox
    function aggiornaTesto() {
        ids = [];
        jQuery("input:checkbox:checked").each(function(n) {
            ids.push(jQuery(this).attr("value"));
        });
        jQuery("#paramspositions").val(ids);
    }

    // here, we reload the saved order
    restoreOrder();
    jQuery(".checkbox").button();
    jQuery("#sortable input").click(aggiornaTesto);
}); // JavaScript Document

These are my problems:

  • in this conditions all works except update input value on check/uncheck
  • if I delete ids on li tags all works except restoreOrder on load

How can I make this work? Basically I need a sortable checkbox list, that on loading page the list order depend from an array, and on check/uncheck and moving items its update the input value.
Thank you very much and sorry for my english.

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

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

发布评论

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

评论(1

宁愿没拥抱 2024-12-03 01:13:04

我找到了解决方案。问题是 li 和 input 具有相同的 id,如果我在每个 li id 中只添加一个下划线,如下所示:

<li id="item1_">
...
<li id="item3_">

在我在 RestoreOrder() jquery 函数中编辑这一行之后:

var itemID = IDs[i];

用这个:

var itemID = IDs[i]+"_";

但现在只有一个问题,例如,如果 #paramspositions 值中只有 2 个值,我怎样才能将这 2 个值放在顶部,将其他值放在底部?

I've found a solution. The problem is that li and input have the same id, if I add only an underscore in every li id like this:

<li id="item1_">
...
<li id="item3_">

after I've to edit this line in restoreOrder() jquery function:

var itemID = IDs[i];

with this:

var itemID = IDs[i]+"_";

but there is only one problem now, if in the #paramspositions value there're only 2 value for example, how can I put this 2 value on top, and the others on bottom?

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