yii data retrival query [duplicate] - yii

I am new to yii framework i have created an yii web application with one module .
Now my question is i have added an sample data into my module in my module i have an columns name,dept and and active ,i want to render the data in the grid view only the data where active=0 , Can anyone help me how to change the data rendering query and how to add the condition
<div class="block">
<div class="content">
<h2 class="title">Users's details</h2>
<div class="inner">
<?php $this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'attributes' => array(
'user_id',
'user_name',
'userpass',
'userdept',
'active',
),
'itemTemplate' => "<tr class=\"{class}\"><td style=\"width: 120px\"><b>{label}</b></td><td>{value}</td></tr>\n",
'htmlOptions' => array(
'class' => 'table',
),
)); ?>
</div>
</div>
Thanks in advance.

Try adding a criteria
$criteria->compare('active', '1');

You have to add this in your search function:
$criteria->compare('active', '1');

Related

Post inside controller not loading into model in Yii2

When I want to get the variable from the form the post action doesn't load .
This is my view:
<?php
$form = ActiveForm::begin();
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?= Html::a(Yii::t('app', 'Start'), ['start', 'link' => $model->link], ['type' => 'button','class' => 'btn btn-primary btn-round']) ?>
</div>
<?php ActiveForm::end(); ?>
This is my controller:
if ($model->load(Yii::$app->request->post())){
exit(var_dump('everything is ok'));
}else {
exit(var_dump('nothing is right'));
}
The result is 'nothing is right'.
Apart from using the anchor link instead of a submit button, you are not using model to create active input hence the field names are without model names or the standard array format that Yii accepts, you should pass empty string to the load method as second parameter which is formName like below
$model->load(Yii::$app->request->post(),'');
So your complete form should look like
<?php
$form = ActiveForm::begin(
[
'action' => 'start',
]
);
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?php echo Html::submitButton(Yii::t('app', 'Start'), ['class' => 'btn btn-primary btn-round']) ?>
</div>
<?php ActiveForm::end();?>
EDIT
and your controller code should look like below, mind the first check it needs to be there so that the code is run when you submit only not on page load
if (Yii::$app->request->isPost) { //this should be here before the rest of the code
if ($model->load(Yii::$app->request->post(), '')) {
exit(var_dump('everything is ok'));
} else {
exit(var_dump('nothing is right'));
}
}
This is because load() method looks for post data inside model name property and you are writing the input yourself instead of using the Yii method for forms.
So your post Yii::$app->request->post() returns:
array(
'username' => 'value of username',
)
And your $model->load looks for
array(
'modelName' => array(
'username' => 'value of username',
)
)
To make your post data too look like that you could do it the right way that is, delete your Input and use this method inside the form:
<?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>
Or the wrong way, modify your input and inside username use:
<input type="text" name="modelName[username]" placeholder="FullName">
Of course where I put username you must put your real model name.
Finally I find the solution
<?php
$form = ActiveForm::begin(
[
'action' => 'start',
]
);
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?= Html::a('submit', Url::to(['start', 'link' => $model->link]), ['data-method' => 'POST']) ?>
</div>
<?php ActiveForm::end();?>
thank you for all

change value in datepicker on selection of combo value in yii

I have a combo box in a form. According to the selected value from combo ,value of date must be changed in datepicker.How to do this?
code goes like this:
<div class="row col2">
<?php $records = CHtml::listData(CodeValue::model()->findAll(array('order' => 'code_lbl','condition'=>"code_type= 'visit_type'")), 'code_id', 'code_lbl');?>
<?php echo $form->labelEx($model,'visit_type'); ?>
<?php echo $form->dropDownList($model,'visit_type',$records,array('empty' => 'Select Visit Type')); ?>
<?php echo $form->error($model,'visit_type'); ?>
</div>
<div class="row col2">
<?php echo $form->labelEx($model,'next_visited_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker',array(
'model' => $model,
'attribute'=>'next_visited_date',
//'flat'=>true,//remove to hide the datepicker
'options'=>array(
'showAnim'=>'drop',//'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop'
'dateFormat' => 'yy-mm-dd',
'showButtonPanel' => true, // show button panel
),
'htmlOptions'=>array(
'style'=>''
),
));
?>
<?php echo $form->error($model,'next_visited_date'); ?>
</div>
I have to change the visit date according to visit type selected.
You'll have to use ajax for this:
Change your drop down to use ajax, something like:
<?php echo $form->dropDownList($model,'visit_type',$records,array(
'empty' => 'Select Visit Type',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('controller/myAction'),//your controller and action name
'update'=>'#Model_next_visited_date', //replace Model with the model name
'success'=> 'function(data) {
$("#Model_next_visited_date").empty(); //replace Model with the model name
$("#Model_next_visited_date").val(data); //replace Model with the model name
} '
)));
?>
and create a new action:
public function actionMyAction()
{
//you will receive drop down value in $_POST['Model']['visit_type']
// Place your date logic here.....
}
Hope that helps

CDetailView don't show properly Chtml::link's HTML data

i use Chtml::link in one CDetailView.
array(
'label'=>'Images',
'type'=>'html',
'value'=> CHtml::link('<i class="fa fa-tag"></i>','#',array('data-target'=>'#myModal','class'=>'tag','data-toggle'=>'modal')),
),
But html properly not generated. it only add few property like below
<a class="tag" href="#"><i class="fa fa-tag"></i></a>
How can i add all property?
array(
'label'=>'Images',
'type'=>'html',
'value'=> CHtml::link('<i class="fa fa-tag"></i>','#',array('data-target'=>'#myModal','class'=>'tag','data-toggle'=>'modal')),
),
change 'type'=>'html', to
'type'=>'raw',
After changing the code its look like below block
array (
'label' => 'Images',
'type' => 'raw',
'value' => CHtml::link('<i class="fa fa-tag"></i>','#',array('data-target'=>'#myModal','class'=>'tag','data-toggle'=>'modal')),
),

Styling button-column in Yii CGridview to wrap content with <div> tag inside <td>

Currently my buttons in table column comes like this
<td>
<a href="#"/>Edit</a>
<a href="#"/>Veiw</a>
<a href="#"/>Delete</a>
</td>
I want it to do like
<td>
<div class="wrapper">
<a href="#"/>Edit</a>
<a href="#"/>Veiw</a>
<a href="#"/>Delete</a>
</div>
</td>
Please suggest some way to configure it. Thanks
You have to customize cgidview code as below:
array(
'class' => 'YouCSSClass',
'template' => '{view} {update} {delete}',
),
Modify the column template of your Grid
'template' => '<div class="wrapper"><a>{view}</a> {update} {delete}</div>'
Finally your grid view will come some thing like
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
..................
..................
'columns'=>array(
...............
...............
'template' => '<div class="wrapper"><a>{view}</a> {update} {delete}</div>',
...............
)
));
Please customize your CGridView buttons on views file like this.
$this->widget('zii.widgets.grid.CGridView', array(
---
'columns'=>array(
---
array(
'class'=>'CButtonColumn',
'template' => '<div class="wrapper">{view}{update}{delete}</div>',
),
),
));

clear inputed data after ajaxsubmit and updating clistview

in views index.php i have
<?php
$this->widget('zii.widgets.CListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'id'=>"post_list",
));
?>
<div id="addtodo">
<?php $this->renderPartial('ajax_page', array('model' => $model, ));?>
</div>
and in ajax_page.php
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'post-todo',
'enableAjaxValidation'=>false,
)); ?>
<?php
echo $form->textFieldRow(
$model,
'title',
array(
'class' => 'input-medium span6 addtodo',
'placeholder'=>'New todo task',
'prepend' => '<i class="glyphicon glyphicon-plus"></i>',
)
);
?>
<div class="row buttons">
<?php echo CHtml::ajaxSubmitButton ("Post",
CHtml::normalizeUrl(array('todo/add')),
array("success"=>'js:function(data){$.fn.yiiListView.update("post_list",{});}'),
array('class'=>'')
); ?>
</div>
<?php $this->endWidget(); ?>
So, All work fine. but after updating CListview, inputed text keeping in input form .
How to clear input form after updating CListview with Ajax?
You can do this by using the javascript .reset() method. You will need to change your ajax button to this;
<?php echo CHtml::ajaxSubmitButton ("Post",
CHtml::normalizeUrl(array('todo/add')),
array("success"=>'js:function(data){
$.fn.yiiListView.update("post_list",{});
$("#post-todo")[0].reset();
}'),
array('class'=>'')
); ?>