简体   繁体   English

使用JQuery将列表元素从一个无序列表移动到另一个

[英]Moving list element from one unordered list to another with JQuery

I have looked at a few of the other answers to this question, but I have still not been able to get it done correctly. 我已经查看了该问题的其他一些答案,但仍无法正确完成。 I have two unordered lists. 我有两个无序列表。 One is dynamically created from a database table. 一个是从数据库表动态创建的。 The other is just a static table that begins empty. 另一个只是一个开始为空的静态表。 Each list contains, or will contain, list elements with nested anchor and awesome-font icon elements. 每个列表包含或将包含带有嵌套锚点和真棒字体图标元素的列表元素。 My goal is to be able to, upon clicking the icon, transfer the list element from one list to the other, while changing the icon from a plus to minus sign. 我的目标是能够在单击图标时将列表元素从一个列表转移到另一个列表,同时将图标从加号更改为减号。 So far, I can successfully transfer items from the dynamic list to the static one, and I can change the icons,but I can not figure out how to transfer them back to the original list. 到目前为止,我可以成功地将项目从动态列表转移到静态列表,并且可以更改图标,但是我不知道如何将它们转移回原始列表。

Here's my html: 这是我的html:

<div class="row" id="constructTable">
        <div class="col-md-6">
            <h1>Choose your Columns:</h1>
            <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                <div class="panel panel-default">
                    <div class="panel-heading" id="heading1" role="tab">
                        <h4 class="panel-title">
                            <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse1" aria-expanded="false" aria-controls="collapse1" class="collapsed"><span style="font-size: 16px;">Candidate Data</span></a>
                        </h4>
                    </div>
                    <div id="collapse1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading1" aria-expanded="false" style="height:0px;">
                        <ul class="list-group choose" id="candidate">
                            <li class="list-group-item" id="candidate0">
                                <a class="btn btn-sm btn-default">
                                    <i class="fa fa-lg fa-plus"></i>
                                </a>
                                <span style="font-size: 14px; margin-left:5px;">First Name</span>
                            </li>
                            <li class="list-group-item" id="candidate1">
                                <a class="btn btn-sm btn-default">
                                    <i class="fa fa-lg fa-plus"></i>
                                </a>
                                <span style="font-size: 14px; margin-left:5px;">Last Name</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            <?php $counter = 2; ?>
            <?php foreach ($mergedTests as $test): ?>
                <?php $count=0 ?>
                <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true" style="margin-top:-10px;">
                    <div class="panel panel-default">
                        <div class="panel-heading" id="heading<?php echo $counter; ?>" role="tab">
                            <h4 class="panel-title">
                                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse<?php echo $counter; ?>" aria-expanded="false" aria-controls="collapse<?php echo $counter; ?>" class="collapsed">
                                <span style="font-size: 16px;"><?php if( ! $test->example ): ?><?php echo $test->example;  ?> Data<?php else: ?><?php echo $test->example; ?> Data<?php endif; ?></span>
                                </a>
                            </h4>
                        </div>
                    </div>
                    <?php $label = explode(",", $test->subtestLabel); ?>
                    <div id="collapse<?php echo $counter; ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading<?php echo $counter; ?>" aria-expanded="false" style="height:0px;">
                        <ul class="list-group" id="<?php echo $test->subTestAbbreviation; ?>">
                            <?php if ( ! $test->testMakerTestId) : ?>
                                <?php foreach($label as $category): ?>
                                    <li class="list-group-item" id="<?php echo $test->example; ?><?php echo $count; ?>" >
                                        <a class="btn btn-sm btn-default">
                                            <i class="fa fa-lg fa-plus"></i>
                                        </a>
                                        <span  style="font-size: 14px; margin-left:5px;"><?php echo $test->example; ?> <?php echo $category; ?></span>
                                    </li>
                                    <?php $count++; ?>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <li class="list-group-item" id="<?php echo $test->subTestAbbreviation; ?>0">
                                    <a class="btn btn-sm btn-default">
                                        <i class="fa fa-lg fa-plus"></i>
                                    </a>
                                    <span style="font-size: 14px; margin-left:5px;"><?php echo $test->example; ?> Raw Score</span>
                                </li>
                                <li class="list-group-item" id="<?php echo $test->subTestAbbreviation; ?>1">
                                    <a class="btn btn-sm btn-default">
                                        <i class="fa fa-lg fa-plus"></i>
                                    </a>
                                    <span style="font-size: 14px; margin-left:5px;"><?php echo $test->example; ?> Percentile</span>
                                </li>
                            <?php endif; ?>
                        </ul>
                    </div>
                </div>
                <?php $counter++; ?>
            <?php endforeach; ?>
        </div>
        <div class="col-md-6">
            <h1>Arrange Your Columns:</h1>
            <ul class="list-group ui-sortable" id="selectedColumns">
            </ul>
        </div>
    </div>
</div>

And my JQuery attempt so far: 到目前为止,我的JQuery尝试:

$('li a').click(function(){
    var parentId = $(this).parent().attr('id');
    $(this).find($(".fa")).removeClass('fa-plus').addClass('fa-minus');
    $("#selectedColumns").append($("#" + parentId));
});

//  Move selection from Arrange Your Columns back to Choose Your Columns
$('ul#selectedColumns').click(function(){
    var parentId = $(this).parent().attr("id");
    console.log(parentId);
    transferId = parentId.slice(0, -1);
    $("#" + parentId).find($(".fa")).removeClass('fa-minus').addClass('fa-plus');
    $("#" + transferId).append($("#" + parentId));
});

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you! 谢谢!

Ok here you may need to use names for these lists, the target is selectedColumns, and the source is unordered lists has some database generated id. 好的,您可能需要为这些列表使用名称,目标是selectedColumns,源是无序列表,具有一些数据库生成的ID。 I will put a class name source-list to your datbase generated lists. 我将一个类名source-list放入您的datbase生成的列表中。

We can use this class name as a common selector of source lists and since the dom will change via user interaction we should use .on instead of .click binding. 我们可以使用此类名称作为源列表的公共选择器,并且由于dom将通过用户交互而更改,因此我们应使用.on而不是.click绑定。

First we send a clone of the selected item to selectedColumns and hide the original one so we wont have to find exact place of the element to roll back; 首先,我们将所选项目的一个副本发送到selectedColumns,并隐藏原始项目,这样我们就不必找到要回滚的元素的确切位置;

$(".source-list").on("li a","click", function(e) {
    // get id of element for further reference
    var itemId = $(this).attr("id");

    // clone the item to a variable
    var cloneItem = $(this).parent().clone();
    // change the appearence of cloned item
    cloneItem.find($(".fa")).removeClass('fa-plus').addClass('fa-minus');
    // append cloned item to selectedColumns
    cloneItem.data("id", itemId).removeAttr('id').appendTo("#selectedColumns");
    // hide the original item
    $(this).hide();
});

$("#selectedColumns").on("li a","click", function(e) {
    var itemId = $(this).data("id");
    $(this).remove();
    $(itemId).show();
});

Since this code has alot of errors / typos, i have created a fiddle for this, 由于此代码有很多错误/错别字,因此我为此创建了一个小提琴,

https://jsfiddle.net/wxsnagr9/7/ https://jsfiddle.net/wxsnagr9/7/

Looks to me like what you have should be fine for the most part, you should just fix the selectors (also, using event delegation will save you from needing to bind data/recheck parent locations): 在我看来,大多数情况下您应该拥有的东西应该很好,您应该只修复选择器(而且,使用事件委托可以使您免于绑定数据/重新检查父位置):

$('#candidate').click('li a', function(){
    $(this).find(".fa").removeClass('fa-plus').addClass('fa-minus');
    $(this).parent().appendTo($("#selectedColumns"));
});


$('#selectedColumns').click('li a', function(){
    $(this).find(".fa").addClass('fa-plus').removeClass('fa-minus');
    $(this).parent().appendTo($("#candidate"));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以 HTML 无序列表的形式将数组元素从 html 表移动到另一个数组 - Moving array element from html table to another array in the form of an HTML unordered list 如何把检索到的<li>使用 JQuery 的另一个无序列表末尾的无序列表元素? - How to put the retrieved <li> element of an unordered list at the end of another unordered list using JQuery? 如何使用jQuery一次填充一个无序列表元素? - How do I populate an unordered list one element at a time with jQuery? 将列表项从一个ul元素移动到另一个ul元素 - Issue moving list item from one ul element to another ul element jQuery无序列表元素单击选择器问题 - jQuery unordered list element click selector issue 试图从无序列表中获取最后一个元素 - Trying to get last element from a unordered list jQuery-将列表项的元素移到同一项的另一个元素内+对列表的每个项执行此操作 - jQuery - moving an element of a list item inside another element of the same item + do this for each item of my list 如何仅获取JQuery中无序列表菜单中的第一个列表元素? - How to get only the first list element in menu of unordered list in JQuery? 尝试使用jQuery选择一个无序列表中的列表项和另一个无序列表 - Trying to select a list item within an unordered list with another unordered list using jquery 如何从无序列表中选择项目 - How to select item from unordered list jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM