我正在尝试使包jFiler中的图像容器可拖动,以便在拖放后可以对图像位置进行排序。这是full script,但是该示例不起作用。我在jFiler模板的父div中添加了id="sortable",并将其添加到我的all.js中:

$("#sortable").sortable({
          containment: "parent",
          placeholder: "ui-state-highlight"
      });


但是由于某种原因,它不起作用。
这是我的jFiler.js:

$('#upload').filer({
            limit: null,
            maxSize: null,
            extensions: null,
            changeInput: '<div class="jFiler-input-dragDrop"><h1>+</h1></div>',
            showThumbs: true,
            appendTo: '.uploaded_items',
            theme: "default",
            templates: {
                box: '<div class="jFiler-item-list" id="sortable"></div>',
                item: '<div class="jFiler-item img-container">\
                            {{fi-image}}\<div class="overlay"><a href="#"><span><i class="jFiler-item-trash-action ion-trash-a"></span></a>\
</div></div>',
                itemAppend: '<div class="jFiler-item img-container dragImg">\
                            {{fi-image}}\<div class="overlay"><a href="#"><span><i class="jFiler-item-trash-action ion-trash-a"></span></a>\
</div></div>',
                progressBar: '<div class="bar"></div>',
                itemAppendToEnd: false,
                removeConfirmation: false,
                _selectors: {
                    list: '.jFiler-item-list',
                    item: '.jFiler-item',
                    progressBar: '.bar',
                    remove: '.jFiler-item-trash-action',
                }
            },


上传后,它将创建如下所示的html:

<div class="uploaded_items">
   <div class="jFiler-items jFiler-row">
      <div class="jFiler-item-list" id="sortable">
         <div class="jFiler-item img-container" data-jfiler-index="3" style="">
            <div class="jFiler-item-thumb-image"><img src="http://myApp.app/imagecache/thumb/bear_PNG1190.png" draggable="false"></div>
            <div class="overlay"><a href="#"><span><i class="jFiler-item-trash-action ion-trash-a"></i></span></a></div>
         </div>
         <div class="jFiler-item img-container" data-jfiler-index="2" style="">
            <div class="jFiler-item-thumb-image"><img src="http://myApp.app/imagecache/thumb/bear_PNG1189.png" draggable="false"></div>
            <div class="overlay"><a href="#"><span><i class="jFiler-item-trash-action ion-trash-a"></i></span></a></div>
         </div>
         <div class="jFiler-item img-container" data-jfiler-index="1" style="">
            <div class="jFiler-item-thumb-image"><img src="http://myApp.app/imagecache/thumb/bear_PNG1188.png" draggable="false"></div>
            <div class="overlay"><a href="#"><span><i class="jFiler-item-trash-action ion-trash-a"></i></span></a></div>
         </div>
         <div class="jFiler-item img-container" data-jfiler-index="0" style="">
            <div class="jFiler-item-thumb-image"><img src="http://myApp.app/imagecache/thumb/bear_PNG1183.png" draggable="false"></div>
            <div class="overlay"><a href="#"><span><i class="jFiler-item-trash-action ion-trash-a"></i></span></a></div>
         </div>
      </div>
   </div>
</div>


然后按以下顺序调用脚本:

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js" integrity="sha384-I6F5OKECLVtK/BL+8iSLDEHowSAfUo76ZL9+kGAgTRdiByINKJaqTPH/QVNS1VDb" crossorigin="anonymous"></script>
  <script type="text/javascript" src="{{ asset('js/zurb/zurb.js') }}"></script>
  <script type="text/javascript" src="{{ asset('js/jquery-ui/jquery-ui.min.js') }}"></script>
  <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
  <script type="text/javascript" src="{{ asset('js/jquery-filer/jquery-filer.js') }}"></script>
  <script type="text/javascript" src="{{ asset('js/editor/editor.js') }}"></script>
  <script type="text/javascript" src="{{ asset('js/datepicker/datepicker.js') }}"></script>
  <script src="https://code.highcharts.com/highcharts.js"></script>
  <script src="https://code.highcharts.com/modules/exporting.js"></script>
  <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.pjax/1.9.6/jquery.pjax.js"></script>
  <script src="{{ asset('js/moment/moment.js') }}"></script>
  <script src="{{ asset('js/charts.js') }}"></script>
  <script src="{{ asset('js/all.js') }}"></script>


更新的代码:

我也使用了这个:

$(".dragdiv").each(function(){
            makeElementAsDragAndDrop($(this));
        });

        $('.images-refresh').hide();
        $('.images-refresh').click(function(){
            $(this).closest("form").submit()
        });

        function makeElementAsDragAndDrop(elem) {
            $(elem).draggable({
                revert: "invalid",
                cursor: "move",
                helper: "clone"
            });
            $(elem).droppable({
                activeClass: "ui-state-hover",
                hoverClass: "ui-state-active",
                drop: function(event, ui) {
                    $('.images-form a').hide();
                    $('.images-refresh').show();
                    var $dragElem = $(ui.draggable).clone().replaceAll(this);
                    $(this).replaceAll(ui.draggable);
                    makeElementAsDragAndDrop(this);
                    makeElementAsDragAndDrop($dragElem);

                }
            });
        }


而且在未使用jFiler创建的元素上效果很好,例如我在编辑表单中创建的元素:

      @foreach($images as $image)
        <div class="large-2 columns end dragdiv">
            <img src="http://coop.app/{{ $image->path}}">
        </div>
      @endforeach


但是它仍然不能与jFiler创建的div一起使用。它们不同于不是由jFiler创建的其他div不会获得ui-draggable ui-draggable-handle ui-droppable属性。

最佳答案

上载项目后,我将尝试运行refresh()


  刷新可排序项目。触发所有可排序项目的重新加载,从而导致新项目被识别。


我不熟悉此插件,因此请猜测要使用的功能:

jQuery的

$(function() {
  $("#sortable").sortable({
    containment: "parent",
    placeholder: "ui-state-highlight"
  });
  $('#upload').filer({
    limit: null,
    maxSize: null,
    extensions: null,
    changeInput: '<div class="jFiler-input-dragDrop"><h1>+</h1></div>',
    showThumbs: true,
    appendTo: '.uploaded_items',
    theme: "default",
    templates: {
      box: '<div class="jFiler-item-list" id="sortable"></div>',
      item: '<div class="jFiler-item img-container">\
                            {{fi-image}}\<div class="overlay"><a href="#"><span><i class="jFiler-item-trash-action ion-trash-a"></span></a>\
</div></div>',
      itemAppend: '<div class="jFiler-item img-container dragImg">\
                            {{fi-image}}\<div class="overlay"><a href="#"><span><i class="jFiler-item-trash-action ion-trash-a"></span></a>\
</div></div>',
      progressBar: '<div class="bar"></div>',
      itemAppendToEnd: false,
      removeConfirmation: false,
      _selectors: {
        list: '.jFiler-item-list',
        item: '.jFiler-item',
        progressBar: '.bar',
        remove: '.jFiler-item-trash-action',
      }
    },
    afterShow: function() {
      $("#sortable").sortable("refresh");
    }
  });
});

关于javascript - jQuery拖放可排序divs不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37612855/

10-11 06:13