Save value in variable

Asked

Viewed 256 times

2

I’m using the jQueryUI - Sortable to change the positions of the data in a table. However, I need to save the starting position (the position the line was before dragging) and the final position (where the line was dragged). These variables, I will use as parameter to call a query ajax.

My table is as follows:

<h1>Classificar tabela utilizando jQuery UI</h1>
<a href='http://www.jqueryui.com/sortable/'>MjQuery UI - Sortable</a>

<table id="sort" class="grid">
    <thead>
        <tr><th class="index">Nº</th><th>Ano</th><th>Título</th><th>Tipo Sanguíneo</th></tr>
    </thead>
    <tbody>
        <tr><td class="index">1</td><td>1969</td><td>Fulano de Tal</td><td>A+</td></tr>
        <tr><td class="index">2</td><td>1952</td><td>Ciclano da Silva</td><td>B</td></tr>
        <tr><td class="index">3</td><td>1963</td><td>Beltrano Pereira</td><td>A+</td></tr>
        <tr><td class="index">4</td><td>1973</td><td>Joãozinho Mendes</td><td>C</td></tr>
        <tr><td class="index">5</td><td>1965</td><td>José Ciclano</td><td>A</td></tr>
    </tbody>
</table>

And I’m using the following code to "draw" the values:

var fixHelperModified = function(e, tr) {
    var $originals = tr.children();
    var $helper = tr.clone();
    $helper.children().each(function(index) {
        $(this).width($originals.eq(index).width())
    });
                console.log(tr.clone());
    return $helper;

},
    updateIndex = function(e, ui) {
        $('td.index', ui.item.parent()).each(function (i) {
            $(this).html(i + 1);
            console.log($(this).html(i + 1).val);
        });
    };

$("#sort tbody").sortable({
    helper: fixHelperModified,
    stop: updateIndex
}).disableSelection();

In my code, I need to create a variable to save the position values (initial and final).

I have a functional example in Jsfiddle. Just "drag and drop" the line, to better understand what I tried to explain.

1 answer

1

See this example on JSFIDDLE

I found here Stackoverflow - Sortable Change Event element position

put a console.log(pos); that shows you the return with beginning and end of each movement.

var comeco,final;
$( "#sortable" ).sortable({
    change: function(event, ui) {		
        var pos = ui.helper.index() > ui.placeholder.index() 
            ? { start: ui.helper.index(), end: ui.placeholder.index() }
            : { start: ui.placeholder.index(), end: ui.helper.index() }
       comeco = pos.start;  
        final = pos.end; 
         ui.item.startPos = ui.item.index();
        $(this)
            .children().removeClass( 'highlight' )
            .not( ui.helper ).slice( pos.start, pos.end ).addClass( 'highlight' );
    },
    stop: function(event, ui) {
        $(this).children().removeClass( 'highlight' );
        console.log("nova: " + ui.item.index());
        console.log("inicial: " + ui.item.startPos);
    }
});
#sortable { 
    list-style-type: none; 
    margin: 0; 
    padding: 0; 
    width: 60%; 
}
.ui-state-default { 
    border: 1px solid #d3d3d3;
    background-color: #E4E4E4;
    margin: 0 3px 3px 3px; 
    padding: 0.4em;
    font-size: 1.4em; 
    height: 18px; 
}
.highlight{
    background-color: #B24926;	
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<ul id="sortable">
    <li class="ui-state-default">Item 1</li>
    <li class="ui-state-default">Item 2</li>
    <li class="ui-state-default">Item 3</li>
    <li class="ui-state-default">Item 4</li>
    <li class="ui-state-default">Item 5</li>
    <li class="ui-state-default">Item 6</li>
    <li class="ui-state-default">Item 7</li>
    <li class="ui-state-default">Item 8</li>
    <li class="ui-state-default">Item 9</li>
</ul>

  • It would almost be that. However, if you observe, by dragging down, the start which is the initial position, but if it drags up, the end which is the starting position. I need it to stay fixed, regardless of the position you drag. Because these values will be taken to a method and changed in the database.

  • http://jsfiddle.net/Wfsc6/80/

  • I even did this @Gabrielrodrigues. However, drag item 1 to item 2 and then return to the original position. You will see that the values will be the same. The variable começo is always getting the lower value. However, if I drag an item from the bottom up, it would have to be with a higher value.

  • got it, your ultimate goal would be ? leave on the bench as this grid is configured ?

  • The demand would be to update the sequence in the bank as well. I used your code as a basis, and I made these changes, if you want to complete your code, I mark it as correct, because it really helped your response. http://jsfiddle.net/Wfsc6/83/ And if possible, bring the code to the answer, as the link may not exist with time.

  • I thought I was right, but neither are you... rsrs. From the bottom up, it’s working normal. However, from top to bottom you have a value, but if you change the same items from bottom to top, the values are different.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.