HI, 我想将已删除项目的位置保存到数据库[aspx,javascript]。用户可以删除任意数量的项目,调整大小并删除[隐藏],最后当他们点击“保存”按钮时,它应该保存到数据库中。我有代码在drop / stop中执行此操作,但它将保存我想要仅在最后阶段保存的所有丢弃的项目。 我想很多开发人员应该已经这样做了,所以请建议一些代码。
$(function() {
$('#frame img').live('mousemove', function(event) {
$('#frame img').resizable().parent().draggable();
});
});
$(function() {
$('#frame img').live('dblclick', function(event) {
// $(this).resizable("destroy") not working
$(this).hide();
//$(this).unbind("resizable"); not working
//$(this).removeclass(); not working
});
});
$(document).ready(function() {
//Counter
counter = 0;
//Make element draggable
$("img").draggable({
helper: 'clone',
containment: '#frame',
//When first dragged
stop: function(ev, ui) {
var pos = $(ui.helper).offset();
objName = "#clonediv" + counter
$(objName).css({ "left": pos.left, "top": pos.top });
$(objName).removeClass("drag");
//When an existiung object is dragged
$(objName).draggable({
containment: 'parent',
stop: function(ev, ui) {
var pos = $(ui.helper).offset();
}
});
}
});
//Make element droppable
$("#frame").droppable({
drop: function(ev, ui) {
if (ui.helper.attr('id').search(/drag[0-9]/) != -1) {
var pos = $(ui.helper).offset();
counter++;
var element = $(ui.helper).clone();
//var element = element1.resizable();
element.addClass("tempclass");
$(this).append(element);
$(".tempclass").attr("id", "clonediv" + counter);
//$(".tempclass").attr("onclick",function(){ $(this).remove(););
$("#clonediv" + counter).removeClass("tempclass");
//Get the dynamically item id
draggedNumber = ui.helper.attr('id').search(/drag([0-9])/)
itemDragged = "dragged" + RegExp.$1
//console.log(itemDragged)
//alert('left' + pos.left + ',top' + pos.top + 'of item' + itemDragged);
$("#clonediv" + counter).addClass(itemDragged);
}
}
});
//Make the element resizable
});
如果有任何问题,请纠正我。 提前致谢
答案 0 :(得分:2)
假设您的元素是ID为items
的列表下的列表项。
var locations = [];
$('#items li').each(function(i) {
locations[i] = {
x: $(this).offset().left,
y: $(this).offset().top
};
});
然后将此帖子发送到您的服务器端。如果不知道更多细节,我无法100%确定是否只需要这些。
在页面加载时,只需循环遍历位置并向li
添加属性,例如(假设PHP)
<ul id="items">
<?php foreach($items as $item): ?>
<li style="left: <?php echo $item['x']; ?>px; top: <?php echo $item['x']; ?>px">item</li>
<?php endforeach; ?>
</ul>
当然你可能还需要做
#items {
position: relative;
}
#items li {
position: absolute;
}
答案 1 :(得分:0)
一种方法可能是将拖拽停止时的坐标放入隐藏字段,然后您可以在后面的代码中读取并存储在数据库中。
当您重新渲染页面时,您还需要填写该字段并使用javascript将对象放回原位。
此外,提及您正在使用的语言背后的代码可能很方便。 C#,PHP,Java,什么?
答案 2 :(得分:0)
根据您对上述问题的回答:
“我想将div2中的图像保存到数据库中。请参阅上面的代码”
似乎您需要做的就是停止,获取div2的子元素。
jQuery会......
var x = "";
$("#div2").children("img").each(function(){
x = x + "; " + $(this).attr("src");
});
这将返回div2中所有img srcs的分号分隔字符串。
答案 3 :(得分:0)
答案 4 :(得分:0)
我曾经为我的一个班级项目做过这个。由于我只有一周的时间来实施,因此实施起来并不是很好。我做的方式是每当用户拖放时,我从javascript获得最终的x.location和y.location并调用asmx web服务,我将发送(x,y)。在asmx文件中,我编写了代码以将其保存在数据库中。
答案 5 :(得分:0)
所以你不想把它们保存到最后?
您正在将删除的克隆添加到#frame,此时为其指定一个类以指示其已删除的项目。我在'ref'attr中引用了一个初始对象,而不是作为一个类。
<强>可投放强>
$("#frame").droppable({
drop: function(ev, ui) {
if (ui.helper.attr('id').search(/drag[0-9]/) != -1) {
var pos = $(ui.helper).offset();
counter++;
var element = $(ui.helper).clone();
$(this).append(element);
element.attr("id", "clonediv" + counter);
// Get the dynamic item id
draggedNumber = ui.helper.attr('id').search(/drag([0-9])/)
itemDragged = "dragged" + RegExp.$1
element.attr('ref', itemDragged);
}
}
});
click方法应该类似于(假设有一个帖子来保存所有丢弃的项目(x,y,width,height,原始对象id)):
<强>的OnClick 强>
$("#saveDrops").click(function () {
var dObjects = [];
$.each('#frame .droppedClass', function(i) {
dObjects[i] = {
x: $(this).offset().left,
y: $(this).offset().top,
height: $(this).width(),
width: $(this).height(),
identifier: $(this).attr('ref');
}
});
$.post('savemystuff.aspx', dObjects, function(data){
// Do something with the results
});
});