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

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>',
),
),
));

Related

Yii 1.1.21 : is it possible to create two dropdown buttons in the same button group?

I am new to Yii, and I have been LOOKING for documentation on Yii and CMenu. I have used Phalcon and various other frameworks with similar options, but Yii's menu engine is new to me.
I am trying to create a button menu with two drop down menu buttons, each with sub menu items, like this:
Drop Down Button Group
But what is being rendered by the Yii CMenu engine is two drop down menus overlayed on each other and both are being triggered by the same buttons. Like this:
enter image description here
Looking at the rendered code, it looks like the two dropdown menus are being assigned the "dropdown-menu" class by CMenu, (or whatever the bootstrap enabled lib is) and becasue they are in the same button group, when the "open" class is assigned, it's opening BOTH dropdowns at the same time.
So my question is simple, is is even possible, using the CMenu menu arrays, to have two dropdowns in the same. Is there a menu "Item Option" or "HTML Option" I can add to the menu item properties that will all this to reference two different css tags? I know I gotta be missing something.
Here is how the menu's are being built in the view.
$this->menu = array_merge($this->menu, array(
array(
'label' => '<span class="hidden-xs hidden-sm">' . Yii::t('app', 'Export') . '</span>',
'encodeLabel' => false,
'htmlOptions' => array('id' => 'export-or-email-btn', 'class' => 'navbar-btn btn-sm',),
'items' => array(
array(
'label' => Yii::t('app', 'Export'),
'icon' => 'fa fa-file-excel-o',
'visible' => true,
'itemOptions' => array('class' => 'work-order-export-btn'),
),
array(
'label' => Yii::t('app', 'Email Export'),
'icon' => 'fa fa-envelope-o',
'visible' => true,
'itemOptions' => array('id' => $model->getClassName(), 'class' => 'email-export-btn', 'data-grid-id' => 'work-order-grid'),
),
array(
'label' => Yii::t('app', 'Export as Import Template'),
'icon' => 'fa fa-file-excel-o fa-lg',
'visible' => true,
'itemOptions' => array('class' => 'work-order-export-import-btn'),
),),),);
$this->menu = array_merge($this->menu, array(
array(
'label' => '<span class="hidden-xs hidden-sm">' . Yii::t('app', 'Actions') . '</span>',
'encodeLabel' => false,
'htmlOptions' => array(
'id' => 'work-order-actions-btn work-order-actions',
'class' => 'navbar-btn btn-sm',
'style' => 'margin: 0 0 0 15px;',
),
'items' => array(
array(
'icon' => 'fa fa-print fa-lg',
'label' => Yii::t('app', 'Print to PDF'),
'visible' => true,
'itemOptions' => array(
'class' => 'work-order-print-pdf',
),),
array(
'icon' => 'fa fa-print fa-lg',
'label' => Yii::t('app', 'Print'),
'visible' => true,
'itemOptions' => array(
'class' => 'work-order-print-selected',
),),))));
and here is the rendered code snippet:
<div class="btn-toolbar">
<div class="operations btn-group-sm btn-group open">
<button id="export-or-email-btn" class="navbar-btn btn-sm btn btn-primary dropdown-toggle" data-toggle="dropdown" name="yt7" type="button">
<span class="hidden-xs hidden-sm">Export</span>
<span class="caret"></span>
</button>
<ul id="yw6" class="dropdown-menu">
<li class="work-order-export-btn nav-header" data-ol-has-click-handler="">
<i class="fa fa-file-excel-o"></i> Export
</li>
<li id="WorkOrder" class="email-export-btn nav-header" data-grid-id="work-order-grid" data-ol-has-click-handler="">
<i class="fa fa-envelope-o"></i> Email Export
</li>
<li class="work-order-export-import-btn nav-header" data-ol-has-click-handler="">
<i class="fa fa-file-excel-o fa-lg"></i> Export as Import Template
</li>
</ul>
<button id="work-order-actions-btn work-order-actions" class="navbar-btn btn-sm btn btn-primary dropdown-toggle" style="margin: 0 0 0 15px;" data-toggle="dropdown" name="yt8" type="button">
<span class="hidden-xs hidden-sm">Actions</span>
<span class="caret"></span>
</button>
<ul id="yw7" class="dropdown-menu">
<li class="work-order-print-pdf nav-header">
<i class="fa fa-print fa-lg"></i> Print PDF
</li>
<li class="work-order-print-selected nav-header">
<i class="fa fa-print fa-lg"></i> Print Selected
</li>
</ul>
</div>
</div>
I think your problem is you are merging both arrays in the same $this->menu attribute.
Maybe you should use CMenu as widget like in the documentation
$this->widget('zii.widgets.CMenu', array(
'items'=>array(
// Important: you need to specify url as 'controller/action',
// not just as 'controller' even if default action is used.
array('label'=>'Home', 'url'=>array('site/index')),
// 'Products' menu item will be selected no matter which tag parameter value is since it's not specified.
array('label'=>'Products', 'url'=>array('product/index'), 'items'=>array(
array('label'=>'New Arrivals', 'url'=>array('product/new', 'tag'=>'new')),
array('label'=>'Most Popular', 'url'=>array('product/index', 'tag'=>'popular')),
)),
array('label'=>'Login', 'url'=>array('site/login'), 'visible'=>Yii::app()->user->isGuest),
),
));
For more information and attributes check out the official documentation here.

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

CHtml submit button is not working when 'enter' key press in Yii

In my Yii web application, CHtml submit button is not working when 'enter' key press in Login form. My login form is,
<?php echo CHtml::beginForm(); ?>
<h3 class="form-title">Login to your account</h3>
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span> Enter any username and password. </span>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Username</label>
<div class="input-icon">
<i class="fa fa-user"></i>
<?php echo CHtml::activeTextField($model, 'username', array("class" => "form-control placeholder-no-fix", 'placeholder' => "Username")) ?>
</div>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<div class="input-icon">
<i class="fa fa-lock"></i>
<?php echo CHtml::activePasswordField($model, 'password', array("class" => "form-control placeholder-no-fix", 'placeholder' => "Password")) ?>
</div>
</div><br>
<div class="form-group">
<?php echo CHtml::submitButton(UserModule::t("Login "), array('class' => 'btn green pull-right')); ?>
</div>
<div class="text-danger"><?php echo CHtml::errorSummary($model); ?></div>
<?php echo CHtml::endForm(); ?><br><br><br>
<?php
$form = new CForm(array(
'elements' => array(
'username' => array(
'type' => 'text',
'maxlength' => 32,
),
'password' => array(
'type' => 'password',
'maxlength' => 32,
),
'rememberMe' => array(
'type' => 'checkbox',
)
),
'buttons' => array(
'login' => array(
'type' => 'submit',
'label' => 'Login',
),
),
), $model);
?>
In this login form using chtml submit button. I want to submit this login form by press enter key.
How to resolve this problem.
Please help me.
If you want to use onclick:
<?php echo CHtml::submitbutton(UserModule::t("Login "), array('class ' => 'btn green pull-right', 'onclick' => ' '));?>

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')),
),

yii data retrival query [duplicate]

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');