【问题标题】:dropzone js to link delete url with remove buttondropzone js 将删除 url 链接到删除按钮
【发布时间】:2014-08-05 22:53:12
【问题描述】:

在 Dropzonejs 中,我正在创建删除按钮,然后将其附加到缩略图中,如何使用 addRemoveLinks:true 将我从服务器直接获取的 url 链接到删除按钮,

//Write function if you need to add some event after files added
      myDropzone.on("addedfile", function(file) {
        console.log('Files Added using ---------->'+$attrs.apiCall);
      var _this=this;
        /* Maybe display some more file information on your page */
       var removeButton = Dropzone.createElement("<button data-dz-remove>-Remove file</button>");
        removeButton.addEventListener("click", function(e) {
        e.preventDefault();
        e.stopPropagation();
        _this.removeFile(file);
        });
        file.previewElement.appendChild(removeButton);
      });

【问题讨论】:

  • 你有没有想过这个问题?我正在做同样的事情......
  • @cjn 是的,我添加了删除链接功能。检查代码的答案。

标签: javascript jquery dropzone.js


【解决方案1】:

您可以添加删除链接..在添加文件事件中,您可以在服务器响应中获取URL并将其设置在删除链接中。

 myDropzone.on("addedfile", function (file) {
     var _this = this;

     /* Maybe display some more file information on your page */
        var removeButton = Dropzone.createElement("<button data-dz-remove " +
                        "class='del_thumbnail btn btn-default'><span class='glyphicon glyphicon-trash'></span></button>");

        removeButton.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopPropagation();
        var server_file = $(file.previewTemplate).children('.server_file').text();
        // Do a post request and pass this path and use server-side language to delete the file
        //          $.post(server_file,{'X-CSRFToken': $cookies.csrftoken}, 'json');
        $http({
            method: 'POSt',
            url: server_file,
            headers: {
                   'X-CSRFToken': $cookies.csrftoken
            }
        });
         _this.removeFile(file);
         });
         file.previewElement.appendChild(removeButton);
 });

【讨论】:

    【解决方案2】:

    这适用于 Dropzone 5.0.0

    <script>
        Dropzone.options.dropzoneForm = {
            addRemoveLinks: true,
            dictDefaultMessage: "<strong>Drop files here or click to upload. </strong>",
            init: function() {
                this.on("complete", function(file) {
                    $(".dz-remove").html("<div><span class='fa fa-trash text-danger' style='font-size: 1.5em'></span></div>");
                });
            }
        };
    </script>
    

    【讨论】:

      【解决方案3】:

      添加

      addRemoveLinks: true,
      

      然后在里面使用下面的

      init: function () {}
      

      当你点击 dz-remove 时,它​​会转到它的父级,然后查找图片 ID 所在的 span 元素的文本。

      使用 $ajax,您可以将该 imageId 发送到您的操作并执行您想要的操作。 注意:我在这里使用 toastr 来通知用户。

      $(".dz-remove").on("click", function (e) {
           e.preventDefault();
           e.stopPropagation();
      
           var imageId = $(this).parent().find(".dz-filename > span").text();
      
           $.ajax({
           url: "Your url here",
           data: { imageId: imageId},
           type: 'POST',
           success: function (data) {
                if (data.NotificationType === "Error") {
                     toastr.error(data.Message);
                } else {
                     toastr.success(data.Message);                          
                }},
                error: function (data) {
                     toastr.error(data.Message);
                }
           })
      
      });
      

      希望这可以帮助你兄弟:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        • 2011-02-25
        • 1970-01-01
        相关资源
        最近更新 更多