Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery sortable change number of element?

I am using simple Jquery sortable that user can drag and drop element, but on every element i have some number, on loading screen all numbers are in order, but what i need whabe someone reoders element to auto chnage that number according to his selection. Here is working fiddle what i have

http://jsfiddle.net/4bhb4z1e/

I need when someone move etc. Number 10 to be the first to chnage that number to 1 and all others to reindex?

Here is code for now

<ol class="example">
    <li> <span>1</span>
        <ol></ol>
    </li>
    <li> <span>2</span>
        <ol></ol>
    </li>
    <li> <span>3</span>
        <ol>
            <li><span>4</span>
            </li>
            <li><span>5</span>
            </li>
            <li> <span>6</span>
                <ol>
                    <li><span>7</span>
                    </li>
                </ol>
                <ol>
                    <li><span>8</span>
                    </li>
                    <li><span>9</span>
                    </li>
                </ol>
            </li>
        </ol>
    </li>
    <li><span>10</span>
    </li>
</ol>

JS

$(function  () {
  $("ol.example").sortable()
})

UPDATE

Now i have structure like this 1, 2, 3, 4, 5 etc.

What i need when someone move 4 to first place to chnage that number to 1 and all other numbers to reorder, like I move number 4 to first place it chnage number to 1, and first that is under new 1 to chnage that number to 2 and further

like image 774
Miomir Dancevic Avatar asked Nov 29 '25 07:11

Miomir Dancevic


1 Answers

That plugin has onDrop event, so you can update your code this way:

$(function () {
    $(".example").sortable({
        onDrop: function ($item, container, _super, event) {
            $('.example li').removeClass('dragged');
            $('.example li').removeAttr('style');
            $("body").removeClass('dragging');
            $('.example span').each(function (i) {
                var humanNum = i + 1;
                $(this).html(humanNum + '');
            });
        }
    });
});

Look at Fiddle!

like image 151
skobaljic Avatar answered Dec 01 '25 23:12

skobaljic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!