I'm using yii2 with dropzonejs (perminder-klair/yii2-dropzone)
When i want to initialize the view with some data i got this error,
It seems like the init call is not proccessed
error
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)
my 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}
});
}"
],
]);
?>
This plugin use Json::encode function for encoding options and because of this, in your code init encode as string. String is not a function.
You can simply use JsExpression for javascript function to prevent this.
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.
This code should work.
<?= \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}
});
}"
], ]); ?>
it looks like this part in your options overwrites the Dropzone.js init function:
'init' => "function(file){alert( ' is removed')}",
The Yii2 plugin you are using ist simply encoding the options array:
Json::encode($this->options)
So your init is no function it is a string like this:
var options = { "init" : "function(file){alert( ' is removed')}" }
It is not possible to set the init option over this php plugin.
Edit: Thanks #CooPer for the correction, Yii2 isn't my favourite framework so I'm not very familiar with it. It's a good solution.
Related
I have searched now very long for this but no answers, please help me.
I'm using the highchart-widget from 2amigos in yii2. When I'm creating a view in my controller via "render" the chart shows
and as you can see on the picture on the right the asset is included in the source of the network -> "b732256a - highcharts.src.js"
BUT NOW COMES THE PROBLEM, when I'm rendering the view with "renderAjax" and insert the view in an existing HTML document with ajax -> take a look at my javascript code: the the asset "b732256a - highcharts.src.js" is missing take a look at this foto
But when i access the action direct via browser:
index.php?r=bewt/ladeauswertung&standort=&datum=
it works with renderAjax and render, there is just a problem when i bind the view in an existing html document. im using this routine, to bind the view in an existing html document
document.getElementById("auswertungdetail").innerHTML = jsondata;
What you need to know about my code.
on a html-page there is a button, when the user press this button im calling the following javascript code, and the chart should show up without reloading the whole page
$(document).on('click', '#ladeauswertung', function ()
{
var ausgewaehlterstandort = document.getElementById("standorte").value;
var datum = document.getElementById("datum").value;
$.get("index.php?r=voti/ladeauswertung&standort=" + ausgewaehlterstandort + "&datum=" + datum,
function (jsondata)
{
document.getElementById("auswertungdetail").innerHTML = jsondata;
}
);
});
in my controller im rendering the view:
function actionLadeauswertung($standort, $datum)
{
//some algorithm that do smth with $standort and $datum, not important
$auswertung = 5;
$gesamtauswertung = 10;
return $this->renderAjax('auswertungdetail', ["auswertung" => $auswertung, "gesamtauswertung" => $gesamtauswertung]);
}
and here ist the view which i render in the controller above:
<?php
use dosamigos\highcharts\HighCharts;
?>
<?php echo
HighCharts::widget([
'clientOptions' => [
'chart' => [
'type' => 'bar'
],
'title' => [
'text' => 'Fruit Consumption'
],
'xAxis' => [
'categories' => [
'Apples',
'Bananas',
]
],
'yAxis' => [
'title' => [
'text' => 'Fruit eaten'
]
],
'series' => [
['name' => 'Jane', 'data' => [1, 0, 4]],
['name' => 'John', 'data' => [5, 7, 3]]
]
]
]);
?>
and in my view im calling the 2amigos highchart, it works perfectly with "render" but i wanted to add the view where the highchart is included dynamically when a button is pressed so have to use renderAjax.
PLEASE HELP!!!!!
I've got a number of links containing parameters which will open a dialog which is populated with an ajax call.
sample link:
Attach File
Here's the trigger for the modal:
$(".attach_timesheet_file").on("click", function(e) {
e.preventDefault();
var url = "<?=Yii::app()->createUrl('admin/timesheetNew/attachTimesheet')?>";
var id = $(this).data("id");
var weekStart = $(this).data("week-start");
var weekEnd = $(this).data("week-end");
$.ajax({
type: 'POST',
url:url,
data: {
id: id,
week_start: weekStart,
week_end: weekEnd
},
success: function(data) {
var modal = $("#attachFileModal");
try{
modal.html(data);
}
catch(error)
{
console.log(error);
}
modal.dialog('open');
return true;
}
})
});
The basic action called by the ajax:
public function actionAttachTimesheet(){
$projectId = Yii::app()->request->getPost('id', null);
$reportedWeekStart = Yii::app()->request->getPost('week_start', null);
$reportedWeekEnd = Yii::app()->request->getPost('week_end', null);
$this->renderPartial("_attachTimesheet", [
'projectId' => $projectId,
'reportedWeekStart' => $reportedWeekStart,
'reportedWeekEnd' => $reportedWeekEnd,
'dataProvider' => TimesheetAdditionalFile::additionalFilesDataProvider($projectId, $reportedWeekStart, $reportedWeekEnd)
], false, true);
}
And finally the CGridView widget inside the dialog:
$this->widget('zii.widgets.grid.CGridView', [
'id' => 'files-grid',
'dataProvider' => $dataProvider,
'columns' => [
[
'name' => 'filename',
'header' => 'File Name',
'value' => 'CHtml::link($data["filename"], Yii::app()->baseUrl . TimesheetNew::FILES_FOLDER . $data["filename"], ["class" => ($data["filetype"] == "pdf")?"fancybox_pdf":"fancybox_picture"])',
'type'=>'raw',
'headerHtmlOptions'=>array('style'=>'width: 250px'),
],
[
'class' => 'CButtonColumn',
'template' => '{delete}',
'buttons' => [
'delete' => [
'label' => 'Delete',
'imageUrl' => null,
'url' => 'Yii::app()->createUrl("admin/timesheetNew/deleteFile", ["id" => $data["id"]])'
]
],
'deleteConfirmation'=>'Are you sure you want to delete this file?',
]
]
]);
I've also used qq.FileUploader as well as fancybox inside the modal, but these do not seem to interfere with anything.
When I try to click any such "attach file" link, the dialog opens just fine and everything works as intended. I'm seeing my gridview, and I can add and delete files. However, when I close the dialog, it won't open this link or any other "attach file" links.
The error I'm getting in the console is this after re-clicking a link:
Uncaught TypeError: modal.dialog is not a function
I'm only experiencing this when using the gridview, when I comment out this widget code, I can freely open and close these dialogs at will.
Any help would be greatly appreciated :)
The solution was rather easy. By adding these lines at the top of the view file, dialogs can once again be opened and closed indefinitely.
Yii::app()->getClientScript()
->scriptMap = array(
'jquery.js' => false,
'jquery-ui.min.js' => false
);
I am using Yii framework on a project and i am using an extension which uses select2 jquery. I am unable to grasp how the implementation for ajax works with this extension or the select2.
My ajax call returns the following json.
[
{"id":"1", "text" : "Option one"},
{"id":"1", "text" : "Option one"},
{"id":"1", "text" : "Option one"}
]
The yii extension enfolds the select2 extension as below
$this->widget('ext.select2.ESelect2', array(
'name' => 'selectInput',
'ajax' => array(
'url'=>Yii::app()->createUrl('controller/ajaxAction'),
'dataType' => 'json',
'type' => 'GET',
'results' => 'js:function(data,page) {
var more = (page * 10) < data.total; return {results: data, more:more };
}',
'formatResult' => 'js:function(data){
return data.name;
}',
'formatSelection' => 'js: function(data) {
return data.name;
}',
),
));
I found a related question from this Question! The link to the extension am using is YII select2 Extention!
So a week later i merged with the answer to this question.
First let me highlight how the select2 ajax or in my case the Yii ESelect Extension.
The ajax options for jquery are the same as for the Eselect Extention i.e. url,type and datatype altho there is a slight difference on the format returned after successfully querying.
As for the result set for Eselect/select2 expects two parameters to be returned. that is
id : data.myOptionsValue;
text : data.myOptionText;
Reference :: https://select2.github.io/options.html#ajax
if we want to customize the format for the result set that is retured we can go a head and extend the plugin by using
'formatResult' => 'js:function(data){
return data.name;
}',
'formatSelection' => 'js: function(data) {
return data.name;
}',
I also had an issue getting my head around how the extention was quering. A look around and i realised that we have two datatype jsonp and json these two datatypes will handle data differently.
Jsonp (json padding) allows sending query parameters when querying. As for my case i am not passing any other parameters e.g an authkey e.t.c. In my case i changed the datatype to json and returning a json with id and text as results. See below my working snippet.
echo CHtml::textField('myElementName', '', array('class' => 'form-control col-lg-12'));
$this->widget('ext.select2.ESelect2', array(
'selector' => '#myElementName',
'options' => array(
'placeholder' => 'Search ..',
'ajax' => array(
'url' => Yii::app()->createUrl('controller/ajaxAction'),
'dataType' => 'json',
'delay' => 250,
'data' => 'js: function(term) {
return {
q: term,
};
}',
'results' => 'js: function(data){
return {results: data }
}',
),
),
));
I'm having issues converting the following jQuery/AJAX call to CakePHP's JS Helper.
Here is what currently works:
<script type="text/javascript">
//<![CDATA[
$(document).ready(function () {
$("#DatabaseServerId").bind("change", function (event) {
var server_id = $("#DatabaseServerId").val();
$.ajax({
async:true,
dataType:"html",
evalScripts:true,
success:function (data, textStatus) {
$("#DatabaseEnvironmentId").html(data);
},
url:"\/apsportal\/servers\/get_environments/"+server_id
}
);
return false;
});
});
//]]>
</script>
Here is what I have thus far:
$data = $this->Js->get('#DatabaseServerId')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#DatabaseServerId')->event('change',
$this->Js->request(
array('controller' => 'servers', 'action' => 'get_environments'),
array(
'update' => '#DatabaseEnvironmentId',
'async' => true,
'type' => 'html',
'dataExpression' => true,
'evalScripts' => true,
'data' => $data,
)
)
);
The issue is that the parameter passed has an array key specified and narrows the usage of this action.
http://my.domain.com/servers/get_environments?data%5BDatabase%5D%5Bserver_id%5D=36
Since server_id is being used in other models, I would rather have this as generic as possible.
Note: I figure if I standardize to the JS Helper, if something changes in the future, I'll only have to change things once.
Per the comments, I used raw jQuery instead of CakePHP's methods.
I am generating a checkbox using the corresponding method of CHtml, and I want to run some JavaScript code before and after the AJAX request. Here is the code:
echo CHtml::checkBox('markComplete', FALSE,
array(
'class' => 'markComplete',
'ajax' => array(
'type' => 'POST',
'url' => $this->createUrl('/events/events/MarkComplete'),
'data' => 'event_status='.$events['id'],
'beforeSend' => 'function(){ $(this).parent("TR").hide(); }',
'success' => 'function(resp) { $("#right").append(resp); }'
),
)
);
How can I tell Yii that beforeSend and success are JavaScript code and not plain strings?
Better option available starting from Yii 1.1.11
All framework classes now support custom JavaScript snippets through CJavaScriptExpression. Use it like this:
'ajax' => array(
'beforeSend' => new CJavaScriptExpression(
'function(){ $(this).parent("TR").hide(); }'
),
// ...
)
The option of prefixing strings with js: is still available by default, but it can now be disabled as required using an optional parameter on CJavaScript::encode.
Original answer
If you want to include literal JavaScript code as part of options, the convention in Yii is to prefix the code with js:. So you would write it like this:
'ajax' => array(
'type'=>'POST',
'url'=>$this->createUrl('/events/events/MarkComplete'),
'data'=>'event_status='.$events['id'],
'beforeSend' => 'js:function(){
$(this).parent("TR").hide();
}',
'success'=>'js:function(resp) {
$("#right").append(resp);
}'
),
)
Unfortunately this is not well documented, which is why people run into exactly this problem every now and then.