在 rails 中保存可拖动 div 的位置 [英] Saving the position of a draggable div in rails

查看:29
本文介绍了在 rails 中保存可拖动 div 的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使 div 可拖动后,我希望在删除 div 时将 div 的位置传递给控制器​​,以便将其保存到数据库中.

After making a div draggable, i would like, when the div is dropped, to pass to the controller the div's position so that it can be saved on to the database.

我的第一个想法是在 div 中有一个隐藏的表单,其中包含 2 个文本字段(一个用于 X,一个用于 Y),通过 javascript 保持更新并在放置时提交.

My first idea would be to have an hidden form within the div, with 2 text fields (one for X and one for Y) which is kept updated via javascript and submitted on drop.

然而,这听起来有点像黑客,并不是一个优雅的解决方案.有没有人遇到过这种情况?

However this sounds like a bit of a hack and is not an elegant solution. Has anybody encountered this before?

谢谢.

推荐答案

我还想说将位置存储在文本字段中是一个坏主意.jQuery UI 提供可拖动且易于使用.下面的示例将在元素被删除时发送一个 ajax 请求.

I was also going to say storing the positions in text fields is a bad idea. jQuery UI provides draggable and is easily used. The example below will send a ajax request whenever the element is dropped.

$('#draggable_element').draggable({  
    stop: function(event, ui) {   
        $.post("http://example.com/some_action",
            { top: ui.position["top"], left: ui.position["left"] },
            function() {  
                // callback stuff
            }
        );
    }  
});

这篇关于在 rails 中保存可拖动 div 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆