<?php
echo $form->dropDownList($model,'department', CHtml::listData(Departments::model()->findAll("activation=1"), 'name', 'name'), array('empty'=>'--please select--'));
?>
Monday, May 21, 2012
Dynamic dropDownList with condition
Thursday, May 17, 2012
Default selected value in a dropdownlist in Yii
Dynamic dropDownList
Static dropDownList
<?php echo $form->dropDownList($model,'clientid', CHtml::listData(Client::model()->findAll(), 'code', 'name'), array('empty'=>'--please select--'));
?>
Static dropDownList
<?php echo $form->dropDownList($model, 'active',
array('1' => 'Active', '2' => 'Inactive'),
array('empty' => 'Select Activation')
); ?>
Wednesday, May 16, 2012
Yii dropDownList onChange
<div class="row">
<?php echo $form->labelEx($model,'clientid'); ?>
<?php //echo $form->textField($model,'projectid'); ?>
<?php echo $form->dropDownList($model,'clientid', CHtml::listData(Client::model()->findAll(), 'code','name'),
array(
'prompt' => '--Please Select --',
'value' => '0',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('task/projects'),
'update' => '#Task_projectid', //selector to update value
'data' => array('clientid'=>'js:this.value'),
)
)
); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'projectid'); ?>
<?php echo $form->dropDownList($model,'projectid', array('empty'=>'--please select--')); ?>
<?php echo $form->error($model,'projectid'); ?>
</div>
{
$giatUnit = (!empty($_POST['clientid'])) ? $_POST['clientid']: '0';
$data=Project::model()->findAll('clientid=:clientid',
array(':clientid'=>$giatUnit));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
}
}
<?php echo $form->labelEx($model,'clientid'); ?>
<?php //echo $form->textField($model,'projectid'); ?>
<?php echo $form->dropDownList($model,'clientid', CHtml::listData(Client::model()->findAll(), 'code','name'),
array(
'prompt' => '--Please Select --',
'value' => '0',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('task/projects'),
'update' => '#Task_projectid', //selector to update value
'data' => array('clientid'=>'js:this.value'),
)
)
); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'projectid'); ?>
<?php echo $form->dropDownList($model,'projectid', array('empty'=>'--please select--')); ?>
<?php echo $form->error($model,'projectid'); ?>
</div>
The controller action
public function actionProjects(){
$giatUnit = (!empty($_POST['clientid'])) ? $_POST['clientid']: '0';
$data=Project::model()->findAll('clientid=:clientid',
array(':clientid'=>$giatUnit));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
}
}
Yii CMenu with Submenu
<?php $this->widget('zii.widgets.CMenu',array(
'linkLabelWrapper' => 'span',
'items'=>array(
array('label'=>'Dashboard', 'url'=>array('/site/index'),'linkOptions'=>array('class'=>'dashboard')),
// array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Clients', 'url'=>array('/client/admin'),'linkOptions'=>array('class'=>'tables menudrop'),
'items'=>array(
array('label'=>'Create Client', 'url'=>array('client/create')),
array('label'=>'Manage Client', 'url'=>array('client/admin')),
)),
array('label'=>'Projects', 'url'=>array('/project/admin'),'linkOptions'=>array('class'=>'tables menudrop'),
'items'=>array(
array('label'=>'Create Project', 'url'=>array('project/create')),
array('label'=>'Manage Projects', 'url'=>array('project/admin')),
)),
array('label'=>'Task', 'url'=>array('/task/admin'),'linkOptions'=>array('class'=>'tables menudrop'),
'items'=>array(
array('label'=>'Create Task', 'url'=>array('task/create')),
array('label'=>'Manage Tasks', 'url'=>array('task/admin')),
)),
array('label'=>'Employies', 'url'=>array('/employee/admin'),'linkOptions'=>array('class'=>'users menudrop'),
'items'=>array(
array('label'=>'Create Employee', 'url'=>array('employee/create')),
array('label'=>'Manage Employies', 'url'=>array('employee/admin')),
)),
// array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
//array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
)); ?>
<ul id="yw2">
<li><a href="/taskmnger/index.php/site/index" class="dashboard"><span><em>Dashboard</em></span></a></li>
<li><a href="/taskmnger/index.php/client/admin" class="tables menudrop active"><span><em>Clients</em></span></a>
<ul style="display: block;">
<li class="active"><a href="/taskmnger/index.php/client/create"><span><em>Create Client</em></span></a></li>
<li><a href="/taskmnger/index.php/client/admin"><span><em>Manage Client</em></span></a></li>
</ul>
</li>
<li style="border-top: 1px solid rgb(221, 221, 221);"><a href="/taskmnger/index.php/project/admin" class="tables menudrop active"><span><em>Projects</em></span></a>
<ul style="display: block;">
<li><a href="/taskmnger/index.php/project/create"><span><em>Create Project</em></span></a></li>
<li><a href="/taskmnger/index.php/project/admin"><span><em>Manage Projects</em></span></a></li>
</ul>
</li>
<li style="border-top: 1px solid rgb(221, 221, 221);"><a href="/taskmnger/index.php/task/admin" class="tables menudrop active"><span><em>Task</em></span></a>
<ul style="display: block;">
<li><a href="/taskmnger/index.php/task/create"><span><em>Create Task</em></span></a></li>
<li><a href="/taskmnger/index.php/task/admin"><span><em>Manage Tasks</em></span></a></li>
</ul>
</li>
<li style="border-top: 1px solid rgb(221, 221, 221);"><a href="/taskmnger/index.php/employee/admin" class="users menudrop"><span><em>Employies</em></span></a>
<ul>
<li><a href="/taskmnger/index.php/employee/create"><span><em>Create Employee</em></span></a></li>
<li><a href="/taskmnger/index.php/employee/admin"><span><em>Manage Employies</em></span></a></li>
</ul>
</li>
</ul>
'linkLabelWrapper' => 'span',
'items'=>array(
array('label'=>'Dashboard', 'url'=>array('/site/index'),'linkOptions'=>array('class'=>'dashboard')),
// array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Clients', 'url'=>array('/client/admin'),'linkOptions'=>array('class'=>'tables menudrop'),
'items'=>array(
array('label'=>'Create Client', 'url'=>array('client/create')),
array('label'=>'Manage Client', 'url'=>array('client/admin')),
)),
array('label'=>'Projects', 'url'=>array('/project/admin'),'linkOptions'=>array('class'=>'tables menudrop'),
'items'=>array(
array('label'=>'Create Project', 'url'=>array('project/create')),
array('label'=>'Manage Projects', 'url'=>array('project/admin')),
)),
array('label'=>'Task', 'url'=>array('/task/admin'),'linkOptions'=>array('class'=>'tables menudrop'),
'items'=>array(
array('label'=>'Create Task', 'url'=>array('task/create')),
array('label'=>'Manage Tasks', 'url'=>array('task/admin')),
)),
array('label'=>'Employies', 'url'=>array('/employee/admin'),'linkOptions'=>array('class'=>'users menudrop'),
'items'=>array(
array('label'=>'Create Employee', 'url'=>array('employee/create')),
array('label'=>'Manage Employies', 'url'=>array('employee/admin')),
)),
// array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
//array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
)); ?>
<ul id="yw2">
<li><a href="/taskmnger/index.php/site/index" class="dashboard"><span><em>Dashboard</em></span></a></li>
<li><a href="/taskmnger/index.php/client/admin" class="tables menudrop active"><span><em>Clients</em></span></a>
<ul style="display: block;">
<li class="active"><a href="/taskmnger/index.php/client/create"><span><em>Create Client</em></span></a></li>
<li><a href="/taskmnger/index.php/client/admin"><span><em>Manage Client</em></span></a></li>
</ul>
</li>
<li style="border-top: 1px solid rgb(221, 221, 221);"><a href="/taskmnger/index.php/project/admin" class="tables menudrop active"><span><em>Projects</em></span></a>
<ul style="display: block;">
<li><a href="/taskmnger/index.php/project/create"><span><em>Create Project</em></span></a></li>
<li><a href="/taskmnger/index.php/project/admin"><span><em>Manage Projects</em></span></a></li>
</ul>
</li>
<li style="border-top: 1px solid rgb(221, 221, 221);"><a href="/taskmnger/index.php/task/admin" class="tables menudrop active"><span><em>Task</em></span></a>
<ul style="display: block;">
<li><a href="/taskmnger/index.php/task/create"><span><em>Create Task</em></span></a></li>
<li><a href="/taskmnger/index.php/task/admin"><span><em>Manage Tasks</em></span></a></li>
</ul>
</li>
<li style="border-top: 1px solid rgb(221, 221, 221);"><a href="/taskmnger/index.php/employee/admin" class="users menudrop"><span><em>Employies</em></span></a>
<ul>
<li><a href="/taskmnger/index.php/employee/create"><span><em>Create Employee</em></span></a></li>
<li><a href="/taskmnger/index.php/employee/admin"><span><em>Manage Employies</em></span></a></li>
</ul>
</li>
</ul>
Tuesday, May 15, 2012
Yii CAutoComplete or autosuggestion to display one value and submit another
In your view file, embed the following code within your form:
<?php
$this->widget('CAutoComplete',
array(
//name of the html field that will be generated
'model'=>$model,
'attribute'=>'titile',
//replace controller/action with real ids
'url'=>array('task/AutoCompleteLookup'),
'max'=>10, //specifies the max number of items to display
//specifies the number of chars that must be entered
//before autocomplete initiates a lookup
'minChars'=>2,
'delay'=>500, //number of milliseconds before lookup occurs
'matchCase'=>false, //match case when performing a lookup?
//any additional html attributes that go inside of
//the input field can be defined here
'htmlOptions'=>array('size'=>'40'),
'methodChain'=>".result(function(event,item){\$(\"#Task_titile\").val(item[1]);})",
));
?>
In your controller, create a new action to handle AutoCompleteLookup queries
<?php
public function actionAutoCompleteLookup()
{
if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
{
/* q is the default GET variable name that is used by
/ the autocomplete widget to pass in user input
*/
$titile = $_GET['q'];
// this was set with the "max" attribute of the CAutoComplete widget
$limit = min($_GET['limit'], 50);
$criteria = new CDbCriteria;
$criteria->condition = "titile LIKE :titile";
$criteria->params = array(":titile"=>"%$titile%");
$criteria->limit = $limit;
$taskArray = Task::model()->findAll($criteria);
$returnVal = '';
foreach($taskArray as $usertask)
{
$returnVal .= $usertask->getAttribute('titile').'|'
.$usertask->getAttribute('titile')."\n";
}
echo $returnVal;
}
}
?>
<?php
$this->widget('CAutoComplete',
array(
//name of the html field that will be generated
'model'=>$model,
'attribute'=>'titile',
//replace controller/action with real ids
'url'=>array('task/AutoCompleteLookup'),
'max'=>10, //specifies the max number of items to display
//specifies the number of chars that must be entered
//before autocomplete initiates a lookup
'minChars'=>2,
'delay'=>500, //number of milliseconds before lookup occurs
'matchCase'=>false, //match case when performing a lookup?
//any additional html attributes that go inside of
//the input field can be defined here
'htmlOptions'=>array('size'=>'40'),
'methodChain'=>".result(function(event,item){\$(\"#Task_titile\").val(item[1]);})",
));
?>
In your controller, create a new action to handle AutoCompleteLookup queries
<?php
public function actionAutoCompleteLookup()
{
if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
{
/* q is the default GET variable name that is used by
/ the autocomplete widget to pass in user input
*/
$titile = $_GET['q'];
// this was set with the "max" attribute of the CAutoComplete widget
$limit = min($_GET['limit'], 50);
$criteria = new CDbCriteria;
$criteria->condition = "titile LIKE :titile";
$criteria->params = array(":titile"=>"%$titile%");
$criteria->limit = $limit;
$taskArray = Task::model()->findAll($criteria);
$returnVal = '';
foreach($taskArray as $usertask)
{
$returnVal .= $usertask->getAttribute('titile').'|'
.$usertask->getAttribute('titile')."\n";
}
echo $returnVal;
}
}
?>
Subscribe to:
Posts (Atom)