I have in my controller:
public function actionFilterClients {
if (Yii::app()->request->isAjaxRequest) {
if (isset($_POST['category_id'])) {
$criteria = new CDbCriteria;
$criteria->condition = "user_id=:user_id";
$criteria->params = array(':user_id' => Yii::app()->user->id);
$criteria->compare('category_id',$_POST['category_id'],true);
$dataProvider = new CActiveDataProvider('Client', array(
'criteria'=>$criteria,
));
$this->renderPartial('transfer_step_3' , array('dataProvider'=>$dataProvider)) ;
}
}
}
In my view among other things I have:
<?php $filter=$this->beginWidget('CActiveForm', array(
'id'=>'client-filter-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal'),
)); ?>
<label for="category_id">View clients in category:</label>
<?php echo CHtml::dropDownList('category_id','',Client::clientCategories(), array('options' => array('2'=>array('selected'=>true)))); ?>
<?php
echo CHtml::ajaxButton(
'Filter Clients',
'filterclients',
array(
'type'=>'POST',
'update' => 'client-grid' ,
'success' =>"function(data) {
\$.fn.yiiGridView.update('client-grid');}",
)
);
?>
<?php $this->endWidget(); ?>
and
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'type'=>'bordered striped condensed',
'id'=>'client-grid',
'ajaxUpdate' => true ,
'rowCssClassExpression'=>'($data->duplicate==2)?"yellow":($data->duplicate==1?"blue":"")',
'dataProvider'=>(isset($dataProvider)?$dataProvider:$clients->moveclients()),
'template'=>"{items}\n{pager}",
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
'selectableRows'=>2,
'id'=>'clients',
),
'name',
'surname',
'telephone',
'email',
array(
'header'=>'Category',
'name' => 'category_title',
'type' => 'raw',
'value' => '$data->category->title',
),
),
)); ?>
Because this is a multi-step form, the cgridview dataprovider defaults to listing all clients ($clients->moveclients() lists all clients).
The ajax button posts the category_id to the client/filterclients url correctly.
I can see with firebug that actionFilterClients returns the rendered html correctly (with the correct clients) but the gridview is not updated...
Any ideas on why not?
In the end I added another view that had only a gridview in it and modified my code thus:
Controller:
$this->renderPartial('_ajax_transfer_step_3' , array('dataProvider'=>$dataProvider)) ;
Original view:
<?php $filter=$this->beginWidget('CActiveForm', array(
'id'=>'customer-filter-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal'),
)); ?>
<label for="category_id">View customers in category:</label>
<?php
echo CHtml::dropDownList('category_id', '', Customer::customerCategories(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('filtercustomers'),
'data'=>'js:jQuery(this).serialize()',
'success'=>'function(response) {
jQuery("#customer-grid").html(response)
}',
)
)
);
?>
<?php $this->endWidget(); ?>
Now it just replaces a portion of the page.
I still haven't figured out why my original code didn't update the gridview though.
Related
i'm using Yii-user, and have made some modification to the user/views/user/registration.php file.
for some reason even when i fill in firstname and lastname, it still says i left those fields empty. any idea why?
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'registration-form',
'type'=>'vertical',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
<?php echo $form->textField($model,'email', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('email'))); ?>
<?php echo $form->passwordField($model,'password', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('password'))); ?>
<?php echo $form->passwordField($model,'verifyPassword', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('verifyPassword'))); ?>
<p class="text-seperator"> about you </p>
<?php echo $form->textField($profile,'firstname', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('firstname'), 'maxlength'=> 255)); ?>
<?php echo $form->textField($profile,'lastname', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('lastname'), 'maxlength'=> 255)); ?>
and in my user/models/registrationform.php i have this
class RegistrationForm extends User {
public $firstname; //added
public $lastname; //added
public $verifyPassword;
public $verifyCode;
public function rules() {
$rules = array(
array('firstname, lastname, password, verifyPassword, email', 'required'),
array('firstname', 'length', 'max'=>225, 'min' => 3,'message' => UserModule::t("Incorrect First Name (length between 3 and 225 characters).")),
array('lastname', 'length', 'max'=>225, 'min' => 3,'message' => UserModule::t("Incorrect Last Name (length between 3 and 225 characters).")),
.........
}
}
to fill up firstname and last name using the original code they used this...
$profileFields=Profile::getFields();
if ($profileFields) {
foreach($profileFields as $field) {
?>
<!--div class="row"-->
<?php echo $form->labelEx($profile,$field->varname); ?>
<?php
if ($widgetEdit = $field->widgetEdit($profile)) {
echo $widgetEdit;
} elseif ($field->range) {
echo $form->dropDownList($profile,$field->varname,Profile::range($field->range));
} elseif ($field->field_type=="TEXT") {
echo$form->textArea($profile,$field->varname,array('rows'=>6, 'cols'=>50));
} else {
echo $field->varname.'<br />';
echo $form->textField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255)));
}
?>
<?php echo $form->error($profile,$field->varname); ?>
<!--/div-->
<?php
}
}
I think you need to change this,
you have passed $model in here:
<?php echo $form->textField($model,'email', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('email'))); ?>
but you have given $profile here:
<?php echo $form->textField($profile,'firstname', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('firstname'), 'maxlength'=> 255)); ?>
if you check out the generated html, you will notice that the generated input has different name!
i'm not sure why this works (would be great is someone could explain in detail)
but the issue was here..
public function rules() {
$rules = array(
array('firstname, lastname, password, verifyPassword, email', 'required'),
array('firstname', 'length', 'max'=>225, 'min' => 3,'message' => UserModule::t("Incorrect First Name (length between 3 and 225 characters).")),
array('lastname', 'length', 'max'=>225, 'min' => 3,'message' => UserModule::t("Incorrect Last Name (length between 3 and 225 characters).")),
.........
}
}
firstname, lastname when this is removed it works. and it still is set as required.
Is this not defined?! It says dataProvider is not defined. Btw, I did read this. Does it mean I have to list each items in view? Not following what it's saying.
noob question. Sorry.
public function actionIndex()
{
$id=1;
$dataProvider = new CArrayDataProvider(array(
array('product_name'=>Product::model()->product_name,
'inventory_id'=>Product::model()->inventory_id,
'product_price'=>Product::model()->product_price,
),
$this->render('index',array(
'model'=>$this->loadModel($id),
'dataProvider'=>$dataProvider,
))
));
}
view
<div class="widget-body">
<?php $this->widget('bootstrap.widgets.TbDetailView', array(
'type'=>'striped bordered condensed',
'dataProvider'=>$dataProvider,
'columns'=>array(
array('name'=>'product_name', 'header'=>'Product Name'),
array('name'=>'inventory_id', 'header'=>'Inventory ID'),
array('name'=>'product_price', 'header'=>'Price'),
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'htmlOptions'=>array('style'=>'width: 50px'),
),
),
)); ?>
TbDetailView is supposed to display a single model, you don't need any data provider for it.
Controller code:
public function actionIndex()
{
$id=1;
$this->render('index', array(
'model'=>$this->loadModel($id),
));
}
View:
<?php $this->widget('bootstrap.widgets.TbDetailView', array(
'type'=>'striped bordered condensed',
'data'=>$model,
'attributes'=>array(
'product_name',
'inventory_id',
'product_price',
),
)); ?>
I have following code in action:
$criteria=new CDbCriteria(array(
'condition'=>'status='.Post::STATUS_PUBLISHED,
'order'=>'sortOrder ASC',
'with'=>'commentCount',
));
$criteria->addSearchCondition('tags','home');
$dataProvider=new CActiveDataProvider('Post', array(
'pagination'=>array(
'pageSize'=>Yii::app()->params['postsPerPageHome'],
),
'criteria'=>$criteria,
));
$this->render('home',array(
'dataProvider'=>$dataProvider,
));
home View:
<?php foreach($dataProvider->getData() as $post) { ?>
// ....... displaying $post values
<?php } ?>
How can I display the pagination in above view. I have searched for it, I found only using with zii.widgets.grid.CGridView but here I haven't use the widget.
You need the CPagination from the CActiveDataProvider:
<?php $this->widget('CLinkPager', array(
'pages' => $dataProvider->pagination,
)); ?>
I'm trying to implement a multiple file upload using CMultiFileUpload with CUploadedFile, but it doesn't work. Specifically, _POST is not working even considering that I'm using 'enctype' => 'multipart/form-data' in the options in the view:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'examen-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
and this is the widget and parameters used for CMultiFileUpload:
<div class="row">
<?php echo $form->labelEx($model,'archivo_foto')?>
<?php //echo CHtml::activeFileField($model,'archivo_foto')?>
<?php $this->widget('CMultiFileUpload', array(
'model' => $model,
'name' => 'archivo_foto',
'accept' => 'jpeg|jpg|gif|png|txt', // useful for verifying files
'duplicate' => 'Duplicate file!', // useful, i think
'denied' => 'Invalid file type', // useful, i think
'max' => 10,
'htmlOptions' => array( 'multiple' => 'multiple', 'size' => 25 ),
)); ?>
<?php echo $form->error($model,'archivo_foto')?>
</div>
On the other hand, the controller action is implemented this way:
public function actionUpdateam($id)
{
$model=$this->loadModel($id);
$dir=Yii::getPathOfAlias('application.uploads');
$model->archivo_documento='funciona 0';
if(isset($_POST['Examen'])) {
$model->attributes=$_POST['Examen'];
// THIS is how you capture those uploaded images: remember that in your CMultiFile widget, you set 'name' => 'archivo_foto'
$images = CUploadedFile::getInstancesByName('archivo_foto');
// proceed if the images have been set
$model->archivo_documento='funciona uno';
if (isset($images) && count($images) > 0) {
$model->archivo_documento='funciona dos';
// go through each uploaded image
foreach ($images as $image) {
echo $image->name.'<br />';
$image->saveAs($dir.'/'.$image->name);
$model->archivo_foto = $model->archivo_foto."+".$image->name;
}
// save the rest of your information from the form
if ($model->save()) {
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('update_am',array(
'model'=>$model,
));
}
And at last, I think that is important to mention the rule used for the model (it might be the cause of the problem as well):
array('archivo_foto','file','allowEmpty'=>true,'maxFiles'=>10),
I think that the problem is in post method, because the controller is not uploading the files and is not making any changes in the database. But I'm not sure.
Try changing to 'htmlOptions'=>array('multiple'=>true).
Add following code in the Form
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
Like
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>true,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
)); ?>
I am using the same extension and it is wokring for me with these codes
In the view file
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'adpost-form',
'enableClientValidation'=>true,
'clientOptions' => array(
'validateOnSubmit'=>true,
'validateOnChange'=>false,
'afterValidate'=>'js:submiAjaxForm'
),
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<?php
$this->widget('CMultiFileUpload', array(
'name' => 'photo_name',
'accept' => 'jpeg|jpg|gif|png',
'duplicate' => 'File is not existed!',
'denied' => 'Not images', // useful,
'htmlOptions' => array(
'style' =>'color: transparent;',
),
));
?>
And in the contoller
$images = CUploadedFile::getInstancesByName('photo_name');
foreach ($images as $image => $pic) {
//----------------- Renaming image before uploading
$extension = $pic->getExtensionName();
$newName = "image_".$adModel->id;
$newName .= "_".$imgCount.".".$extension;
$imgCount++;
if ($pic->saveAs($newPath.$newName)) {
// add it to the main model now
$img_add = new AdsPhotosTbl;
$img_add->photo_name = $newName;
$img_add->ad_id = $adModel->id;
$img_add->status = 1;
if(!$img_add->save()){
$error = true;
$upload_error = true;
break;
}
}else{
$upload_error = true;
$error = true;
break;
}
}
I'm a very newbie in Yii framework. I would like to create a page. Which is when the dropdown list change, the listview/gridview will be change by dropdown value.
this is my view
<div class="row">
<?php
$records = Company::model()->findAll();
$company_list = CHtml::listData($records, 'id', 'name');
echo CHtml::dropDownList('company_id','', $company_list,
array(
'onchange'=>"$.fn.yiiListView.update('ajaxListView', {url: '".Yii::app()->createUrl('department/dynamicsectionlist')."?company_id='+$('#company_id option:selected').val()})",
'prompt'=>'Please select a company',
)); ?>
</div>
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view_section',
'id'=>'ajaxListView',
));
?>
This is model
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('p_id',$this->p_id);
$criteria->compare('created',$this->created,true);
$criteria->compare('updated',$this->updated,true);
$criteria->compare('company_id',$this->company_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
This is Controller
public function actionDynamicsectionlist()
{
$company_id = $_POST['company_id'];
$criteria=new CDbCriteria();
$criteria->condition .= 't.id IN (SELECT t2.id, t2.name FROM department t2 WHERE t2.company_id = :company_id)';
$criteria->params[':company_id'] = $company_id;
$dataProvider = new CActiveDataProvider( 'Department', array( 'criteria' => $criteria, ) );
$this->render( 'sectionlist', array( 'dataProvider' => $dataProvider ) );
}
But it is not working. Please help me.
Regads
Tharsoe
I solved it.
This is controller
// Initial view (department/depatmentlist)
public function actionDepartmentlist()
{
$model=new Department('search');
$model->unsetAttributes(); // clear any default values
$model->p_id = 0;
// $dataProvider->getData() will return a list of Post objects
// $dataProvider=new CActiveDataProvider('Department');
$this->render('list_department',array(
'model'=>$model,
));
}
// when the user selected the company from dropdown list
public function actionDynamicsectionlist()
{
$model=new Department('dsearch');
$model->unsetAttributes(); // clear any default values
$model->p_id = 0;
if(isset($_GET['company_id']))
$model->company_id = $_GET['company_id'];
$this->render('sectionlist',array(
'model'=>$model,
));
}
This is model (nothing change)
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('p_id',$this->p_id);
$criteria->compare('created',$this->created,true);
$criteria->compare('updated',$this->updated,true);
$criteria->compare('company_id',$this->company_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
This is view (list_department.php)
<h1>Departments List</h1>
<div class="row">
Company<br />
<?php
$records = Company::model()->findAll();
$company_list = CHtml::listData($records, 'id', 'name');
echo CHtml::dropDownList('company_id','', $company_list,
array('prompt'=>'Please select a company',)); ?>
</div>
<?php
/*
for ListView
$this->widget('zii.widgets.CListView', array(
//'dataProvider'=>$dataProvider,
'dataProvider'=>$model->search(),
'itemView'=>'_view_section',
'id'=>'ajaxListView',
));
*/
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'department-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
//'p_id',
'created',
'updated',
//'company_id',
),
));
?>
<?php
Yii::app()->clientScript->registerScript('search',
"$('#company_id').change(function(){
var companyId = $('#company_id option:selected').val();
$.fn.yiiGridView.update(
'department-grid',
{ type: 'GET',
url: 'http://localhost/mmaig_ceo/ceo-control-system/index.php?r=department/dynamicdepartmentlist&ajax=department-grid&company_id=' + companyId
}
);
});
")
?>