Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery sortable - prevent duplicates between 2 lists

as in the title, how can I prevent duplicates between 2 lists. I am trying to stop duplicates in the #selected list. I suspect its done in the receive event but i am not having any luck.

    $(function () {

    $("#options").sortable({
        connectWith: $("#selected"),
        helper: 'original',
        revert: true,
        tolerance: "pointer",
    });

    $("#selected").sortable({
        connectWith: $("#options"),
        helper: 'original',
        receive: function (event, ui) {            
        },
    });

    $("#options,#selected").disableSelection();

    $("#SkillGroups").change(function () {
        $.ajax({
            type: "POST",
            url: "/Contractor/Contractor/GetSkillsBySkillGroup",
            data: { skillGroupId: $("#SkillGroups").val() },
            success: function (result) {
                $loadList(result);
            }
        })
    });
});
like image 366
Greg Avatar asked Feb 18 '23 00:02

Greg


1 Answers

Finally worked it out. A bit obvious when you see the solution. There are probably other ways to do this as well.

 $("#selected").sortable({
        connectWith: $("#options"),
        helper: 'original',
        receive: function (event, ui) {

            var identicalItemCount  = $("#selected").children('li:contains(' + ui.item.text() + ')').length;
            alert(identicalItemCount);
            if (identicalItemCount > 1) {
                alert("Ahem.... we have a duplicate!!");
                $("#selected").children('li:contains(' + ui.item.text() + ')').first().remove();
            }
        },
    });
like image 124
Greg Avatar answered Mar 03 '23 09:03

Greg