Yii load datepicker in partial view - yii

Trying to load a block with a datepicker from a view.. but it loads only the textfield without datepicking feature. What am I doing wrong?
The create view
<?php echo $form->dropDownList($model,'operation_type',CHtml::listData(OperationType::model()->findAll(),
'id','name'),array(
'class'=>'span3',
'empty'=>'----- Type -----',
'id'=>'idOpType',
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('operations/meta'),
//'dataType'=>'json',
'data'=>array('idOpType'=>'js:this.options[this.selectedIndex].innerHTML'),
'success'=>'function(data){
$("#opTypeBlock").html(data);
}',
),
)); ?>
The Controller Action
public function actionMeta(){
$data= new OperationsMeta();
$this->renderPartial('_meta',array('model'=>$data));
}
The view I am trying to load
<p>Select due date</p>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
'attribute'=>'param',
'options'=>array(
'showAnim'=>'fold',
),
'model'=>$model,
'htmlOptions'=>array(
'style'=>'height:20px;',
'class'=>'inline',
'id'=>'datepickerOpType',
),
));
?>

Try
$this->renderPartial('_meta',array('model'=>$data), false, true);
so it loads JS files.

Use renderAjax() which injects into the rendering result with JS/CSS scripts and files which are registered with the view, as stated here.

Related

clistview a path in itemView in yii

I have a widget that renders a file. From there, it has a clistview. But the path is not working. I am getting the error require(): Filename cannot be empty.
So originally I had the page render from $model, but I realized I should be using clistview. It works fine if I just do a renderPartial $model but it doesn't work in clistview.
from my widget:
$this->renderFile(Yii::getPathOfAlias('application.views.grid'). '/index.php',
array('product'=>$product));
pass to my grid/index.php. I'm showing both ways I had. The top one works but I would need to do a foreach loop. I want to get rid of that due to pagination problems, so I want to use clistview.
<?php
// this works fine, but not what I want.
// $this->controller->renderPartial('//grid/_view',array(
// 'product'=>$product,
// ))
?>
<?php
// $path = '//grid/_view'; I've tried something like this and put it in itemView. It doesn't work.
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$product,
'itemView'=>'//grid/_view',
'template' => '{pager}{items}',
);
?>
You always can set path for itemView as alias
$this->widget('zii.widgets.CListView', array(
'itemView'=>'application.views.grid._view'

how to keep the photo for the registration page

i want to keep the photo column for the registration page so that user can select the image and display in that column for this i am using ImageSelect extension in yii framework but it is not working suggest me how to keep the image while registering the user
Here is my code for ImageSelect
<?php echo $form->labelEx($model,'profileImage'); ?>
<?php
$this->widget('ext.imageSelect.ImageSelect', array(
'path'=>Yii::app()->baseUrl . '/images/Penguins.jpg',
'alt'=>'alt text',
'uploadUrl'=> 'profileinfo/upload',
'htmlOptions'=>array()
));
?>
my controller code is
public function actionUpload()
{
$file = CUploadedFile::getInstanceByName('file');
// Do your business ... save on file system for example,
// and/or do some db operations for example
$file->saveAs(Yii::app()->baseUrl . '/images/'.$file->getName());
// return the new file path
echo Yii::app()->baseUrl.'/images/'.$file->getName();
}

Yii:: Could I get the value of the view in the controller

I create a form use the CActiveForm to create a form.
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'report-form',
'enableAjaxValidation'=>false,
'enableClientValidation'=>true,
'focus'=>array($exps[0],'productname'),
)); ?>
and when I call the ajax handler in the controller.I want to create a new html string to update the form.
but I can't find the way to get access to value $form!
could I get the value in the controller form the view?
any suggestion would be appreciated!
You could create the form object at your controller and pass it to the view via the 2nd argument of the render() function..
// inside controller
$form = $this->beginWidget('CActiveForm', array(
'id'=>'report-form',
'enableAjaxValidation'=>false,
'enableClientValidation'=>true,
'focus'=>array($exps[0],'productname'),
));
$this->render($myView, array(
'form' => $form,
));

YII - renderPartial gives error with CActiveForm

I am trying to include form on product page using renderPartial but it gives error
Fatal error: Call to a member function getErrors() on a non-object in /Applications/MAMP/htdocs/yii/framework/web/helpers/CHtml.php on line 1605
i am using below code...
in product page
// product detail goes here, use below form to make an inquiry about this product
<?php $this->renderPartial('inquiry'); ?>
and in inquiry page page
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'query-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name'); ?>
<?php echo $form->error($model,'name'); ?>
</div>
There is a variable called $model in your view, but in your controller's renderPartial() call, you are not passing $model into your view. So the view is looking for a variable called $model, but it does not exist.
You need to generate a new model in your controller, then pass it into the view, as follows:
$model = new Product(); //use whatever class you created for the model in place of 'Product' here
$this->renderPartial('inquiry', array('model'=>$model));
The 'model'=>$model tells Yii to pass the variable $model into the view, and the 'model' represents the name you use to access that variable from within the view. So if you write something like:
$this->renderPartial('inquiry', array('product'=>$model));
Then in the view, you would access the variable by typing $product instead of $model.

Yii - Design question - Keeping dialoges separate from the main view file and accessing them

I have a case where on my view file there are 6 links and clicking on them opens CJuiDialog boxes . I am keeping all the 6 dialog boxes code in the same view file along with links and that is causing the file to be big and loading them all together once .The ideal scenario is the dialog boxes should get loaded only when the use clicks on the links .
So is there any way we can keep only the links code in the main view file and keeping all the dialogue boxes in separate files and loading them only when the user clicks on the links
I mean
index.php ( view containing only links)
_dialog1 ( containing code for first dialog )
_dialog2 ( containing code for second dialog )
_dialog3 ( containing code for third dialog )
_dialog4 ( containing code for fourth dialog )
_dialog5 ( containing code for fifth dialog )
_dialog6 ( containing code for sixth dialog )
Sample Code
//First Dialog code
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'mydialog1',
'options'=>array(
'title'=>'Dialog box 1',
'autoOpen'=>false,
'modal'=>true,
),
));
echo 'First dialog content here';
$this->endWidget('zii.widgets.jui.CJuiDialog');
echo CHtml::link('open dialog', '#', array(
'onclick'=>'$("#mydialog1").dialog("open"); return false;',
));
//2nd dialog code
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'mydialog2',
'options'=>array(
'title'=>'Dialog box 1',
'autoOpen'=>false,
'modal'=>true,
),
));
echo 'dialog2 content here';
$this->endWidget('zii.widgets.jui.CJuiDialog');
echo CHtml::link('open dialog', '#', array(
'onclick'=>'$("#mydialog2").dialog("open"); return false;',
));
Solution I came with
//In controller
public function actionOpenDialog1()
{
$data = array();
$this->renderPartial('_dialogContent1', $data, false, true);
}
public function actionOpenDialog2()
{
$data = array();
$this->renderPartial('_dialogContent2', $data, false, true);
}
//In index.view
<div id="data">
</div>
<?php
echo CHtml::ajaxButton ("Open first dialog", CController::createUrl('dialogTesting/openDialog1'),array('update' => '#data'));
echo CHtml::ajaxButton ("Open second dialog", CController::createUrl('dialogTesting/openDialog2'),array('update' => '#data'));
?>
//_dialogContent1.php
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'mydialog1',
'options'=>array(
'title'=>'Dialog box 1',
'autoOpen'=>true,
'modal'=>true,
),
));
echo 'first dialog content here';
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
//_dialogContent2.php
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'mydialog1',
'options'=>array(
'title'=>'Dialog box 1',
'autoOpen'=>true,
'modal'=>true,
),
));
echo 'first dialog content here';
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
Thanks a lot for your help
Regards
Kiran
Naming files
The convention in Yii is to name the view files:
index.php: a full view
_dialog1.php: a partial view (included from another view)
Sub views
Then you can include partial views with CController::renderPartial():
$this->beginWidget(...);
$this->renderPartial('_dialog1', array('var1' => 23, 'var2' => "var"));
$this->endWidget(...);
Factorizing code
This should make your source node much lighter. But I suggest you go farther and avoid duplicating all those widget calls. To do this, you should define a structure for your dialog parameters and loop over it. Something like:
$dialogs = array(
'mydialog1' => array(
'file' => '_dialog1',
'options' => array('title' => "My title 1",),
),
'mydialog2' => array(
'file' => '_dialog12,
'options' => array('title' => "My title 2",),
),
);
$defaultOptions = array(
'autoOpen' => false,
'modal' => true,
);
foreach ($dialogs as $id => $dialog) {
$this->beginWidget(
'zii.widgets.jui.CJuiDialog',
array(
'id' => $id,
'options' => CMap::mergeArray($defaultOptions, $dialog['options']),
)
);
// ... include partial view ...
This factorization will make your code more compact, but it will above simplify the future changes. Using data structures to avoid code duplication is a well-known practice.
AJAX
Lastly, if you really want the partial views to be loaded dynamically, that means you have to use AJAX. Be careful, because your page may be less reactive from a user point of view. If all your forms amount to a few Kb of HTML, then there's no nedd for AJAX. But if you go this way, then you'll need to:
Add an CJuiDialog containing just <div id="dialog-ajax"></div>.
Create another action that will apply renderPartial() on the dialog views.
Replace the content of the previous foreach loop with code that writes JS like function dialog1() {jQuery("#dialog-ajax").load(...);}. You'll need to hack if you want to change dynamically the widget title.
Bind some events (clicks) to these JS functions.
Another way would be to make your aJAX action render a full CJuiDialog, it might be simpler and avoid JS hacks. Anyway, I'm not sure you really need AJAX.
My answer
//In controller
public function actionOpenDialog1()
{
$data = array();
$this->renderPartial('_dialogContent1', $data, false, true);
}
public function actionOpenDialog2()
{
$data = array();
$this->renderPartial('_dialogContent2', $data, false, true);
}
//In index.view
'#data'));
echo CHtml::ajaxButton ("Open second dialog", CController::createUrl('dialogTesting/openDialog2'),array('update' => '#data'));
?>
//_dialogContent1.php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'mydialog1',
'options'=>array(
'title'=>'Dialog box 1',
'autoOpen'=>true,
'modal'=>true,
),
));
echo 'first dialog content here';
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
//_dialogContent2.php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'mydialog1',
'options'=>array(
'title'=>'Dialog box 1',
'autoOpen'=>true,
'modal'=>true,
),
));
echo 'first dialog content here';
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
You can simply include one empty CJuiDialog in your page, and when it needs to be displayed load the contents with jQuery AJAX (load is the simplest, and probably enough) that returns a render of the appropriate view before opening the dialog.