Jquery 拖动元素在拖动时通过对话框更改后变为原始默认状态

发布于 2024-11-18 12:38:25 字数 3465 浏览 2 评论 0原文

我的元素有这个拖放代码

$( ".sortable" ).sortable({
  revert: true
});
        $( ".draggable" ).draggable({
            connectToSortable: ".sortable",
            helper: "clone", //clone 
        revert: "true"
    });
     $( ".sortable" ).droppable({
        drop: function( event, ui ) {
             var $element = $(ui.draggable).clone();

            $element.find('.control').children('.delete').css('display', 'inline').click(function () {
                $(this).parent().remove();
            });// display properties Link
            $element.find('.control').children('.properties').css('display', 'inline'); 

            //For Text Box Change the text and add a label
            if($element.find('.control').find('input').attr('type') == "text")
            {
                $element.find('.control').find('.controlText').html("");
                $element.find('.control').find('label').html("Label Here"); 
            }

        }
    });

这是拖动元素的代码

 <div class="tools">    
 <ul>
 <li class="draggable" >
     <div class="control">
      <label>&nbsp;</label>
      <input type="text" name="txt" value="" /><span class="controlText"> Text Box </span>
      <div class="delete" style="display:none"><sup>x</sup></div>
      <div class="properties txtbox" style="display:none">Properties</div>
     </div>
    </li>
 </ul>
 </div>

当拖动文本框时,此事件附加到它

$('.txtbox').live('click', function() {
     //get the label
        var label = $(this).parent().find('label').html();
        $("#textbox_label").val(label);
        $( "#dialog-textbox" ).dialog( "open" ).data('parent_div',$(this).parent());
        return false;
});

这是单击属性时打开的对话框

<div id="dialog-textbox" title="Text Box Properties" style="display:none">
<p>This is the Text Box Properties Form.</p>
<form>
<fieldset>
    <label for="textbox_label">Enter Label </label>
    <input type="text" name="textbox_label" id="textbox_label" class="text ui-widget-content ui-corner-all" />      
</fieldset>
</form>

以下代码处理对话框

 $("#dialog-textbox").dialog({
                        autoOpen: false,
                        height: 300,
                        width: 350,
                        modal: true,
                        buttons: {
                                "Apply": function(){
                                     //code to update element goes here...
                                    var label = $("#textbox_label").val()
                                    var $elem_clicked = $("#dialog-textbox").data('parent_div'); //This retrieves
                                    $elem_clicked.find('label').html(label); 
                                    $( this ).dialog( "close" );
                                    },
                                Cancel: function() {
                                            $( this ).dialog( "close" );
                                        }
                        }
                         });

这里发生了什么是我有一个文本框,我将其拖动到 ul li 列表并且可排序,该文本框被添加到列表中,然后我显示并关闭附加到文本框的属性链接。单击属性时,将打开一个对话框并要求输入新标签。用户为显示的文本框提供新标签。但问题是,当我再次向上或向下拖动可排序列表中的框时,它会返回到其初始状态,并且新标签丢失......我认为这是由于辅助克隆造成的,或者我应该克隆可拖动的项目?

i have this drag drop code for my elements

$( ".sortable" ).sortable({
  revert: true
});
        $( ".draggable" ).draggable({
            connectToSortable: ".sortable",
            helper: "clone", //clone 
        revert: "true"
    });
     $( ".sortable" ).droppable({
        drop: function( event, ui ) {
             var $element = $(ui.draggable).clone();

            $element.find('.control').children('.delete').css('display', 'inline').click(function () {
                $(this).parent().remove();
            });// display properties Link
            $element.find('.control').children('.properties').css('display', 'inline'); 

            //For Text Box Change the text and add a label
            if($element.find('.control').find('input').attr('type') == "text")
            {
                $element.find('.control').find('.controlText').html("");
                $element.find('.control').find('label').html("Label Here"); 
            }

        }
    });

This is the code for the drag element

 <div class="tools">    
 <ul>
 <li class="draggable" >
     <div class="control">
      <label> </label>
      <input type="text" name="txt" value="" /><span class="controlText"> Text Box </span>
      <div class="delete" style="display:none"><sup>x</sup></div>
      <div class="properties txtbox" style="display:none">Properties</div>
     </div>
    </li>
 </ul>
 </div>

When the Text Box is dragged this event is attached to it

$('.txtbox').live('click', function() {
     //get the label
        var label = $(this).parent().find('label').html();
        $("#textbox_label").val(label);
        $( "#dialog-textbox" ).dialog( "open" ).data('parent_div',$(this).parent());
        return false;
});

This is the dialog which opens when the Properties is clicked

<div id="dialog-textbox" title="Text Box Properties" style="display:none">
<p>This is the Text Box Properties Form.</p>
<form>
<fieldset>
    <label for="textbox_label">Enter Label </label>
    <input type="text" name="textbox_label" id="textbox_label" class="text ui-widget-content ui-corner-all" />      
</fieldset>
</form>

The following code handles the Dialog

 $("#dialog-textbox").dialog({
                        autoOpen: false,
                        height: 300,
                        width: 350,
                        modal: true,
                        buttons: {
                                "Apply": function(){
                                     //code to update element goes here...
                                    var label = $("#textbox_label").val()
                                    var $elem_clicked = $("#dialog-textbox").data('parent_div'); //This retrieves
                                    $elem_clicked.find('label').html(label); 
                                    $( this ).dialog( "close" );
                                    },
                                Cancel: function() {
                                            $( this ).dialog( "close" );
                                        }
                        }
                         });

What is happening here is that i have a text box which i drag to a ul li list and that is sortable, the text box is added to the list and and then i show and close and properties links attached to the text box. when the properties is clicked a dialog box opens and asks for a new label. user gives new label to the text box that is shown with that. But what goes wrong is when i again drag the box in the sortable list up or down it goes back to its initial state and the new label is lost... I think this is due to the helper clone or should i make a clone of the draggable item?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜你已久 2024-11-25 12:38:25

使用一个简单的技巧来区分原始元素和拖动的副本:
- 在原始元素的标签中添加一个类,例如

 <label class='orig'> </label>

- 在 $(".sortable").droppable 处理程序中添加在末尾

$(ui.draggable).find('.control').find('.orig').html("Label Here").removeClass('orig');

它应该可以解决您的问题。请在此处查看。

Use a simple trick to distinguish you original element fromthe copy you drag:
- add a class to the label of the original element eg

 <label class='orig'> </label>

- in your $(".sortable").droppable handler add atthe end

$(ui.draggable).find('.control').find('.orig').html("Label Here").removeClass('orig');

It should solve your problem. Check it here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文