Is it possible to convert GridView from table format to div in the index so I can customize the look of my index; my View is
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'Useravatar',
'format' => 'html',
'label' => 'Avatar',
'value' => function ($data) {
return Html::img('http://localhost:8585/yii45/wfp/web/' . $data['Useravatar'],
['width' => '80px', 'height' => '80px']);
},
],
'User_id',
'Usermode',
'Username',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
if It's possible to make it something like, not exactly but somthing that use div and column
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
]); ?>
<div>
<div class="row">
<div class="col-md-3 text-center"> Username </div>
<div class="col-md-6"> Usermode </div>
<div class="col-md-3 text-right"> Useravatar </div>
</div>
<div class="row">
<div class="col-md-12"> Info </div>
</div>
<div class="row">
<div class="col-md-6"> edit </div>
<div class="col-md-6"> delete </div>
</div>
</div>
<?php Pjax::end(); ?>
I found this code about ListView :
https://www.yiiframework.com/doc/guide/2.0/en/output-data-widgets#list-view
use yii\widgets\ListView;
use yii\data\ActiveDataProvider;
$dataProvider = new ActiveDataProvider([
'query' => Post::find(),
'pagination' => [
'pageSize' => 20,
],
]);
echo ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_post',
]);
But I don't know where I have to add this code
For someone who Have the same problem like me the solution is :
in Controller Create a function Example : actionUserlist
public function actionUserlist()
{
$Model_User = new User;
return $this->render('widgetUserList', [
'Model_User' => $Model_User,
]);
}
2 - Create a php file in view " widgetUserList.php "
<?php
use yii\widgets\ListView;
use yii\data\ActiveDataProvider;
$dataProvider = new ActiveDataProvider([
'query' => $Model_User::find(),
'pagination' => [
'pageSize' => 15, //the number of items in a page : 15
],
]);
echo ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_userList',
],
]);
?>
3- Create a view file " _userList.php "
<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
?>
<div class="userlist">
<h2><?= Html::encode($model->Username) ?></h2>
<?= $model->Useremail ?>
</div>
Related
What I want to do is to change from bootstrap vertical to horizontal form and this is what I've tried:
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'action' => ['index'],
'method' => 'get',
'fieldConfig' => [
'horizontalCssClasses' => [
'label' => 'col-sm-2',
'offset' => 'col-sm-offset-2',
'wrapper' => 'col-sm-4',
],
],
]); ?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'firstname') ?>
<?= $form->field($model, 'lastname') ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'bla') ?>
</div>
</div>
<?php ActiveForm::end() ?>
but it gave me an error Setting unknown property: yii\widgets\ActiveForm::layout
Please help!!!
For layout option to work ActiveForm must be instance of yii\bootstrap\ActiveForm instead of yii\widgets\ActiveForm.
Change your use statement.
In boostrap you can use
<form class="form-inline"> // for inline fields form
or
<form class="form-horizontal"> // for horizonatal fields form
eg:
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'action' => ['index'],
'method' => 'get',
'class' => 'form-horizontal',
]); ?>
I have a form field "dob[]" with array input like
<?php $form = ActiveForm::begin();
for($i=0;$i<= 3;$i++):
echo $form->field($model, 'dob[]')->widget(DatePicker::classname(), [
'options' => ['placeholder' => 'Date Of Birth'],
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'format' => 'mm/dd/yyyy',
'autoclose' => true,
]
]);
endfor; ?>
<div class="form-group">
<?= Html::button('Submit', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
Datepicker working only on first "dob" field but rest of the field having only button format of datepicker but calendar not working.
This is because the javascript cannot determine the correct input fields, after the first one. Take a look in your source code. All widgets have properly the same id and/or name. You have to setup a unique ID for each of the generated widgets.
By the way, it is always a good approach to name form data accordingly.
That is documented at the demo page.
The following should work:
<?php
$form = ActiveForm::begin();
for ($i=0; $i < 3; $i++) {
echo $form->field($model, 'date_end')->widget(DatePicker::classname(), [
'options' => [
'placeholder' => 'Date Of Birth',
'name' => 'DOB' .$i,
'id' => 'DOB-ID' . $i,
],
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'format' => 'mm/dd/yyyy',
'autoclose' => true,
],
]);
}
?>
<div class="form-group">
<?= Html::button('Submit', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
Compare validation is action strange - no matter what, emails are never the same, and error is shown all the time.
This is the code for validation:
public function rules()
{
return [
[['first_name', 'last_name', 'email', 'conf_email'], 'required'],
['title', 'match', 'pattern' => '/^[a-zA-Z]{0,100}$/',
'message' => 'Title must contain only letters.'],
[['first_name', 'last_name'], 'match', 'pattern' => '/^[a-zA-Z]{0,45}$/',
'message' => 'The {attribute} must contain only letters.'],
[['email', 'first_name', 'last_name'], 'trim'],
[['email', 'conf_email'], 'email'],
['email', 'string', 'max' => 255],
['email', 'UniqueValidator'],
['conf_email', 'compare', 'compareAttribute'=>'email', 'skipOnEmpty' => false,
'message' => 'Emails do not match.'],
];
}
And code for the form:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
$form = ActiveForm::begin([
'action' => Url::toRoute([
'team/ajax-add-team-member',
'id' => $team->idteam,
'portalID' => $portal->idportal
]),
'enableAjaxValidation' => true,
'validationUrl' => '/team/team-validation',
'id' => 'team-invite-form',
'options' => [
'class' => 'clearfix'
]
]) ?>
<div class="col-md-12">
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-12">
<?php echo $form->field($model, 'title')->textInput() ?>
</div>
<div class="col-md-5 col-sm-5 col-xs-12">
<?php echo $form->field($model, 'first_name')->textInput() ?>
</div>
<div class="col-md-5 col-sm-5 col-xs-12">
<?php echo $form->field($model, 'last_name')->textInput() ?>
</div>
</div>
</div>
<div class="col-md-12">
<?php
echo $form->field($model, 'email')->textInput();
echo $form->field($model, 'conf_email')->textInput();
echo $form->field($model, 'is_medical_professional')->checkbox();
?>
</div>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<?php
echo Html::submitButton('Send', ['id' => 'add-team-member-form-btn', 'class' => 'btn btn-default']);
ActiveForm::end();
?>
What is going on here o.O?
Thiis line looks strange:
['email', 'UniqueValidator'],
Try using ['email', 'unique'], instead
If this will not help - provide more details. What exact error do you faced?
My application have to have multiple language, so I decided to separate each language by using tab (Yii2 gui), but how can I render the form in side the 'content' key?
<?php
$language_tab=[];
$increment=0;
$content="I love you";
foreach($language as $obj){
$language_tab[$increment] = array('label' => $obj->name ,'content' => $content);
$increment++;
}
echo Tabs::widget([
'items' => $language_tab,
'options' => ['tag' => 'div'],
'itemOptions' => ['tag' => 'div'],
'headerOptions' => ['class' => 'my-class'],
'clientOptions' => ['collapsible' => false],
]);
?>
<div class="status-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'date_created')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
I just wanna change from $content to the form below.
Please help!!!
You may create separate view for the form and render it:
...
'content' => $this->render('_language_form', ['language' => $obj, 'model' => $model]),
...
I'm creating a form view and I want to organize the form fields with tabs structure, using the official Tabs widget.
Is it possible init the Tabs widget with the id (or class) of the div elements that contains the active form fields?
One example of how you can manage it is doing like this:
First, divide your contact-form into one view-file for each tab.
Place the ActiveForm::begin() and ActiveForm::end() around the Tabs::widget()
Render the contact-form pages into content, with parameters $model and $form
Example code:
views/site/contact.php
<?php
/* #var $this yii\web\View */
$this->title = 'Contact';
use yii\bootstrap\Tabs;
use yii\bootstrap\ActiveForm;
?>
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<?= Tabs::widget([
'items' => [
[
'label' => 'One',
'content' => $this->render('contact_form1', ['model' => $model, 'form' => $form]),
'active' => true
],
[
'label' => 'Two',
'content' => $this->render('contact_form2', ['model' => $model, 'form' => $form]),
],
]]);
?>
<?php ActiveForm::end(); ?>
views/site/contact_form1.php
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'subject') ?>
views/site/contact_form2.php
<?php
use yii\helpers\Html;
use yii\captcha\Captcha;
?>
<?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
]) ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
</div>
Hope this helps!
Just add at the top of your contact.php global $form; and all works fine.
I have another solution:
When we call $form->field($model, 'name')->textInput(), it will return the model of class yii\widgets\ActiveField, so just continue calling a method of this class as $form->field($model, 'name')->textInput()->render(). It will return a string then you can use it for the tab's content.
I have an example code in my application for translating multi languages as the following code:
<?php
$items = [];
foreach ($translateModels as $translateModel) {
$tabContent = $form->field($translateModel, "[{$translateModel->code}]name")->textInput()->render();
$items[] = [
'label' => $translateModel->language->name,
'content' => $tabContent,
];
}
?>
<?= Tabs::widget([
'options' => [
'class' => 'nav-tabs',
'style' => 'margin-bottom: 15px',
],
'items' => $items,
]) ?>
Maybe it's help.