jQuery & JEasyUI dynamic droppable element

寵の児 提交于 2019-12-12 00:25:44

问题


I have an issue with jQuery & jEasyUI working together. In my code I have a button that when it is clicked it adds a new col-sm-3 to a row div. When I begin dragging around into the droppable nothing happens, although I'm suppose to see an alert. I played around and got it to work by calling .droppable() on newly added columns. That's where things began to get strange. The draggables started disappearing from their positions in the list. It was obvious that jQuery and JEasyUI are not playing well. JEasyUI has it's own draggable & droppable functions but there isn't anything written on how they could be applied to dynamically loaded DOM elements.

Code dynamically added:

This is HTML that I dynamically add to a row

<div class="col-sm-4" >
        <div class="box options-list">
            <div class="box-header with-border">
                <h3 class="box-title"></h3>
                <div class="box-tools pull-right">
                    <button class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
                </div>
            </div>
            <div class="box-body">
                <input class="form-control" placeholder="Name">
                <div class="row">
                    <div class="col-xs-6">
                        <div id='validate-checkbox' class="checkbox icheck">
                            <label>
                                <label for="validate-checkbox"></label>
                                <input  class="validate-checkbox" name="is_disabled" type="checkbox"> Validate
                            </label>
                        </div>
                    </div>
                    <div class="col-xs-6">
                        <div id='conform-value-checkbox' class="checkbox icheck">
                            <label>
                                <label for="conform-value-checkbox"></label>
                                <input  class="validate-checkbox" name="is_disabled" type="checkbox"> Conform
                            </label>
                        </div>
                    </div>
                </div>

                <hr>
                <div class="row">
                    <div class="col-xs-12">
                        <ul class="">

                        </ul>
                    </div>
                </div>
            </div>
        </div>
</div>

Static:

The element where the new element is appended to is <div class="row" id="sortable">

<style>
    #sortable { list-style-type: none; margin: 0; padding: 0; width: 100%; }
   .col-sm-4 .ui-sortable-placeholder  {
        background: red;

    }
</style>
<div class="box">
    <div class="box-body">
        <div class="row">
            <div class="col-md-6">
                <?= $this->Form->input('name', ['placeholder' => 'Enter part name']) ?>
            </div>
            <div class="col-lg-6">
                <?= $this->Form->input('description', ['placeholder' => '(Optional)']) ?>
            </div>
        </div>
        <div class="row">
            <div class="col-md-8">
                <?= $this->Form->input('item_part_type_id', ['label' => 'Type']) ?>
            </div>
            <div class="col-md-4">
                <label for="has-validation-checkbox"></label>
                <div id='has-validation-checkbox' class="checkbox icheck">
                    <label>
                        <input class="has-validation-checkbox" name="has_validation" type="checkbox"> Force Validation
                    </label>
                </div>
            </div>
    </div>
</div>
</div>

<div class="box">
    <div class="box-header with-border">
        <h3 class="box-title"><strong>Part Editor</strong></h3>
    </div>
    <div class="box-body">
        <div class="row">
           <div class="col-xs-12">
               <div class="easyui-layout" style="width:100%;height:400px;">
                   <div  data-options="region:'east',split:true" title="Existing Miniparts" style="width:20%;">



                   </div>
                   <div data-options="region:'west',split:true" title="Options" style="width:20%;">
                        <ul class="list-group" id="options-list">
                            <li class=" bg-black-gradient list-group-item">
                                Text<span class="pull-right"><i class="fa fa-cog"></i></span>
                            </li>
                            <li class="bg-black-gradient  list-group-item">
                                Textarea
                            </li>
                            <li class="bg-black-gradient  list-group-item">
                                List
                            </li>
                            <li class="bg-black-gradient list-group-item">
                                Fabric
                            </li>
                            <li class="bg-black-gradient list-group-item">
                                Multi Fabric
                            </li>
                            <li class="bg-black-gradient  list-group-item">
                                Buyin Code
                            </li>
                            <li class="bg-black-gradient  list-group-item">
                                Buyin Supplier
                            </li>
                        </ul>
                   </div>
                   <div  id="here"  data-options="region:'center',title:'Miniparts'" style="width: 60%; padding:5px;" class="bg-black-gradient" >
                    <div class="row" id="sortable">

                   </div>
                   </div>
                   </div>
           </div>
        </div>
    </div>
    <div class="box-footer">
        <button id="add-part-btn" class="btn btn-primary btn-sm pull-right"><i class="fa fa-plus"></i> Add Part</button>
    </div>
</div>

<div class="well clearfix">
    <div class="pull-right">
        <button class="btn btn-default btn-md"><i class="fa fa-times"></i> Discard Changes</button>
        <button class="btn btn-primary btn-md"><i class="fa fa-save"></i> Save Changes</button>
    </div>
</div>
<script>

    /*
     Each time this is triggered a new element is to be
     added to the sortable <div class="row">. The element then
     accepts a droppable from a UL list */
    $('#add-part-btn').click(function() {

        $.get("<?= $this->Url->build(['action' => 'minipart']) ?>", function (data) {
            var newElement = $(data);
            $('#sortable').append(newElement);
            $(newElement).droppable({
                accept: '.list-group-item',
                drop: function(e,ui)
                {
                    alert("AahhH! my foot!");
                }
            })
        });
    });

    $(function(){ // DOM Ready
        $( "#sortable" ).sortable();
        $( "#sortable" ).disableSelection();

    });

    /* If I put into the DOM Ready function above,the list item being dragged to behave strangely
    * So I left it out here instead. */
    $( ".list-group-item").draggable({
        revert: true,
        appendTo: '.easyui-layout',
        helper: 'clone',
        zIndex: 3
    }).disableSelection();


</script>

My project has so many dependencies that I don't think you will be able to run the snippets probably, but if you want to go the extra mile and help me out on this then please download the following dependencies below:

  • jQuery >=1.11.x
  • jQueryUI jEasyUI latest version
  • AdminLTE 2.1.1 (A bootstrap 3 theme, https://github.com/almasaeed2010/AdminLTE)

来源:https://stackoverflow.com/questions/31384287/jquery-jeasyui-dynamic-droppable-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!