gpt4 book ai didi

jquery ui 可排序 : How to get current index with id and old index with id and pass parameter in ajax in update event

转载 作者:行者123 更新时间:2023-12-01 07:12:30 26 4
gpt4 key购买 nike

我有 2 个 html 表。我正在使用 jquery UI 更改表的位置,并通过 ajax 传递此 jquery 事件参数,同时获取表位置的索引和项目 id,以便我可以在数据库中更新表的当前位置。现在我能够获取当前索引的位置和 id,但我也想获取旧索引和它们的 id。这样我就可以将旧索引、旧 ID、新索引、新 ID 位置轻松存储在数据库中。但不知道该怎么做。

这是 fiddle :demo

这是我的代码:

dashboard.js

$("#widget_update").sortable({      
update : function(event, ui) {
var widget = $('#widget_update').sortable('serialize');

$.ajax({
type: "POST",
url: "ajax/dashboard.php",
dataType : 'json',
cache: false,
data: {'aktion' : 'show-widget','widget':ui.item.index(),'item':ui.item[0].id},
success: function(data){
$('#widget').html(data.html);
},
error: function(data){
alert('Error');
}
});
}
});

最佳答案

看看这个 DEMO

JS代码:

$("#sortable").sortable({
/*stop: function(event, ui) {
alert("New position: " + ui.item.index());
}*/
start: function(e, ui) {
// creates a temporary attribute on the element with the old index
$(this).attr('data-previndex', ui.item.index());
},
update: function(e, ui) {
// gets the new and old index then removes the temporary attribute
var newIndex = ui.item.index();
var oldIndex = $(this).attr('data-previndex');
var element_id = ui.item.attr('id');
alert('id of Item moved = '+element_id+' old position = '+oldIndex+' new position = '+newIndex);
$(this).removeAttr('data-previndex');

///code to pass the data using AJAX
$.ajax({
type: "POST",
url: "your_url",
data: {'item_id':element_id,'item_old_index':oldIndex,'item_new_index':newIndex},
success: function(data){
//code on success
},
error: function(data){
alert('Error');
}
});
}
});
$("#sortable").disableSelection();

HTML 代码:

<h4>Notice: The actual index of the elements starts from 0</h4>
<ul id="sortable">
<li class="ui-state-default" id="item_1"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
<li class="ui-state-default" id="item_2"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 2</li>
<li class="ui-state-default" id="item_3"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 3</li>
<li class="ui-state-default" id="item_4"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 4</li>
<li class="ui-state-default" id="item_5"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 5</li>
<li class="ui-state-default" id="item_6"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 6</li>
<li class="ui-state-default" id="item_7"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 7</li>
</ul>

关于jquery ui 可排序 : How to get current index with id and old index with id and pass parameter in ajax in update event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24570093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com