php - Yii2 Dropzone.js 初始化不工作

标签 php yii dropzone.js yii2-advanced-app

我正在使用 yii2 和 dropzonejs (perminder-klair/yii2-dropzone)

当我想用一些数据初始化 View 时我得到了这个错误, 似乎未处理 init 调用

错误

dropzone.min.js:1 Uncaught TypeError: this.options.init.call is not a function
    at c.init (dropzone.min.js:1)
    at new c (dropzone.min.js:1)
    at HTMLDocument.<anonymous> (index.php?r=branches/upload:672)
    at fire (jquery.js:3187)
    at Object.fireWith [as resolveWith] (jquery.js:3317)
    at Function.ready (jquery.js:3536)
    at HTMLDocument.completed (jquery.js:3552)

我的dropzonejs

 <?= \kato\DropZone::widget([
        'autoDiscover' => false,
       'options' => [
         'init' => "function(file){alert( ' is removed')}",
         'url'=> 'index.php?r=branches/upload',
           'maxFilesize' => '2',
           'addRemoveLinks' =>true,
            'acceptedFiles' =>'image/*',    


             ],
       'clientEvents' => [
           'complete' => "function(file){console.log(file)}",
          // 'removedfile' => "function(file){alert(file.name + ' is removed')}"
           'removedfile' => "function(file){
             alert('Delete this file?');
          $.ajax({
               url: 'index.php?r=branches/rmf',
               type: 'GET',
               data: { 'filetodelete': file.name}
          });

           }"
       ],
   ]);
?>

最佳答案

此插件使用 Json::encode 函数来编码选项,因此,在您的代码 init 中编码为字符串。字符串不是函数

您可以简单地使用 JsExpression javascript 函数来防止这种情况。

When using yii\helpers\Json::encode() or yii\helpers\Json::htmlEncode() to encode a value, JsonExpression objects will be specially handled and encoded as a JavaScript expression instead of a string.

这段代码应该可以工作。

<?= \kato\DropZone::widget([
        'autoDiscover' => false,
       'options' => [
         'init' => new JsExpression("function(file){alert( ' is removed')}"),
         'url'=> 'index.php?r=branches/upload',
           'maxFilesize' => '2',
           'addRemoveLinks' =>true,
            'acceptedFiles' =>'image/*',    


             ],
       'clientEvents' => [
           'complete' => "function(file){console.log(file)}",
          // 'removedfile' => "function(file){alert(file.name + ' is removed')}"
           'removedfile' => "function(file){
             alert('Delete this file?');
          $.ajax({
               url: 'index.php?r=branches/rmf',
               type: 'GET',
               data: { 'filetodelete': file.name}
          });

           }"
       ],    ]); ?>

关于php - Yii2 Dropzone.js 初始化不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43420115/

相关文章:

php - Yii CDbCriteria 从另一个相关对象的相关对象中选择一个字段

javascript - 如何在 Yii 和 Ajax 中加载文章而不重新加载页面

向客户端报告进度的 PHP 脚本

PHP应用平台

php - 插入一行,然后在一个查询中从该行中选择

php - Yii框架中定义的Gii的路由在哪里?

jquery - onclick 清除 dropzone 文件字段

javascript - Dropzone - 最大文件数不起作用

javascript - Rails 应用程序中的隐藏字段未通过 dropzone.js 传递

php - 使用不可见的 div 检测 IFrame 内的点击