Yii2 How to Use Html::tag or Html::beginTag - html

i want to echo like this
<li>
<img src="img/h4-slide.png" alt="Slide">
<div class="caption-group">
<h2 class="caption title">some_title
</h2>
<h4 class="caption subtitle">Dual SIM</h4>
<a class="caption button-radius" href="some_route"><span class="icon"></span>check</a>
</div>
</li>
here my code for render image carousel :
$slides = [];
foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) {
/** #var $slide common\models\Slide */
$slides[] = [
'content' => Html::img(Yii::$app->urlManager->baseUrl . '/uploads/slide/' . $slide->id . '.jpg'),
'caption' => Html::tag('content-group', $slide->title)."<br>".$slide->body,
];
}
And my carousel :
<div class="slider-area">
<div class="block-slider block-slider4">
<?= Carousel::widget([
'id' => 'bxlider-home',
'items' => $slides,
'options' => [
'class' => 'slide',
'data-interval' => 3000,
],
'controls' => [
'<span class="bx-next fa fa-angle-left"></span>',
'<span class="bx-prev fa fa-angle-right"></span>',
],
]) ?>
</div>
</div>
how to Slide->title, slide->body, and some links can be in class caption-group ?

I think it would be better to create new partial file.
Create a new file called _slider.php
Call $this->render('_slider') inside configuration of the slider. Please check below code.
$slides = [];
foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) {
/** #var $slide common\models\Slide */
$slides[] = [
'content' => $this->render("_slider"),
'caption' => Html::tag('content-group', $slide->title)."<br>".$slide->body,
];
}
You can write html inside _slider.php easily now. Don't need to use Html::beginTag() etc.

Generating Tags
The code generating a tag looks like the following:
<?= Html::tag('p', Html::encode($user->name), ['class' => 'username']) ?>
<p class="username">samdark</p>
$options = ['class' => ['btn', 'btn-default']];
echo Html::tag('div', 'Save', $options);
// renders '<div class="btn btn-default">Save</div>'
Hyperlinks:
<?= Html::a('Profile', ['user/view', 'id' => $id], ['class' => 'profile-link']) ?>
Images:
<?= Html::img('#web/images/logo.png', ['alt' => 'My logo']) ?>
generates
<img src="http://example.com/images/logo.png" alt="My logo" />
Lists:
<?= Html::ul($posts, ['item' => function($item, $index) {
return Html::tag(
'li',
$this->render('post', ['item' => $item]),
['class' => 'post']
);
}]) ?>
Even more?
visit

Related

Strategi must be a string

please help me..
when data input arises such problems "Strategi must be a string."
this is my controller :
$isistrategi = $_POST['FormNarasi'];
$fn = FormNarasi::find()->where([
'kriteria_id' => $model->id,
'form_spmi_id' => $formSpmi->id,
])->one();
if(empty($fn))
$fn = new FormNarasi;
$fn->kriteria_id = $model->id;
$fn->form_spmi_id = $formSpmi->id;
$fn->strategi = $isistrategi;
this is my _form :
<?php
$fn = FormNarasi::find()->where([
'kriteria_id' => $model->id,
'form_spmi_id' => $formSpmi->id
])->one();
echo $form->field($fn, 'strategi')->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'preset' => 'advance'
])
?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success','value'=>'1','name'=>'btn-submit']) ?>
</div>
<?php ActiveForm::end(); ?>
please help, master
Here you are assigning an array to the model
$isistrategi = $_POST['FormNarasi'];
...
$fn->strategi = $isistrategi; // HERE
and in the following code you are accessing it. There is an array assigned. So you should assign there a string (the content of the CKEditor)
echo $form->field($fn, 'strategi')->widget(CKEditor::className(), [ // HERE
...
])
As #Sfili_81 mentioned, do not access the $_POST directly and rather use $model->load(Yii::$app->request->post())

Yii2 Pagination with Tabs

I have 5 tabs on one page. All the tabs have different content, but on one of them i need to have pagination. When click on pagination the page is refreshing and the current opened tab is closed and show by default first tab ... I want when click on pagination, the current tab to be open and the refresh only part with data information.
here is my code:
<?php
Pjax::begin([
'id' => 'w0',
'enablePushState' => true, // I would like the browser to change link
'timeout' => 10000 // Timeout needed
]);
$spec = Specifications::find()->where('active = 1')->orderBy(['sort' => SORT_ASC]);
$count = $spec->count();
$pagination = new Pagination(['totalCount' => $count, 'defaultPageSize' => 20]);
$models = $spec->offset($pagination->offset)
->limit($pagination->limit)
->all();
echo LinkPager::widget([
'pagination' => $pagination,
'hideOnSinglePage' => true,
'prevPageLabel' => 'Предишна',
'nextPageLabel' => 'Следваща'
]);
if ($spec) { ?>
<div class="form-group">
<label>Спецификации</label></br>
<?php
foreach ($models as $singleSpec) {
echo $singleSpec->id." ".$singleSpec->title;
}
?>
</div>
<?php } ?>
<?php Pjax::end() ?>
remove 'id'=>'w0' from Pjax, it is refreshing your page

Yii2 Boostrap ActiveForm Horizontal Layout

Good afternoon,
I am new to programming and struggling to understand Yii2 layout with Bootstrap.
What I am after is really simple, so I thought, but I can't seem to get it to remotely work. I want to create a horizontal form with the labels in front of each input and be able to control to the width of each input.
In my current code, I have 2 simple fields, I want the first to span half of the form (col-md-6) and the second should span the totality of the form (col-md-12), but this just isn't working and I don't understand the why so I'm struggling to fix it.
Below is my view
<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;;
use yii\bootstrap\ActiveForm; //used to enable bootstrap layout options
/* #var $this yii\web\View */
/* #var $model backend\models\Projects */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="projects-form">
<?php
$form = ActiveForm::begin([
'id'=>$model->formName(),
'layout' => 'horizontal',
'class' => 'form-horizontal',
'fieldConfig' => [
'enableError' => true,
]
]);
?>
<h2>Project Information</h2>
<h3>General Information</h3>
<div class="form-group row">
<div class="col-md-6">
<?php
echo $form->field(
$model,
'ProjNo'
)->textInput(['maxlength' => true]);
?>
</div>
<div class="col-md-6">
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<?php
echo $form->field(
$model,
'ProjName'
)->textInput(['maxlength' => true]);
?>
</div>
</div>
<p></p>
<p></p>
<?php
ActiveForm::end();
?>
</div>
This is the _form view which is called within the create and update views.
What I don't quite get is why the label alignment isn't consistent because I specify a different width for the overall field and why even though I specified col-md-12, which should be full width from my understanding, it only seems to take about half of the available width.
Any help is greatly appreciated!
Thank you.
current example of what is generated
I just want the labels to line up and be able to have fields with different widths. In the above, when I change the class, the labels change alignment.
You can use the template option under the form's fieldConfig option like below to specify the order of the input, label, and error-block, and these settings would be applied throughout the form for all inputs, in below configurations I am placing the label after the input you can change that if you want.
$form = yii\bootstrap\ActiveForm::begin ( [ 'id' => $model->formName () ,
'layout' => 'horizontal' ,
'class' => 'form-horizontal' ,
'fieldConfig' => [
'enableError' => true ,
'template' => '{input}{error}{label}',
] ] );
you can wrap the {label} and {input} with div like
'template' => '<div class="col-sm-6">{input}{error}</div>
<div class="col-sm-3">{label}</div>',
and remove all the extra HTML from your view just wrap the $form->field() with row see below
$form = yii\bootstrap\ActiveForm::begin ( [ 'id' => $model->formName () ,
'layout' => 'horizontal' ,
'class' => 'form-horizontal' ,
'fieldConfig' => [
'enableError' => true ,
'template' => '<div class="col-sm-6">{input}{error}</div>{label}',
] ] );
?>
<h2>Project Information</h2>
<h3>General Information</h3>
<div class="row">
<?php
echo $form->field (
$model , 'ProjNo'
)->textInput ( [ 'maxlength' => true, ] );
?>
</div>
<div class="row">
<?php
echo $form->field (
$model , 'ProjName'
)->textInput ( [ 'maxlength' => true, ] );
?>
</div>
EDIT
as per discussion you do not want equally aligned labels and inputs but instead you want variable inputs and labels within each row and for doing so you need to configure the template part of the input fields separately and it can look like below if i understood correctly
you should configure your form options and field template options like below and remove the extra class applied on the label col-sm-3 by assigning it control-label class manually
$form = yii\bootstrap\ActiveForm::begin ( [ 'id' => $model->formName () ,
'layout' => 'horizontal' ,
'class' => 'form-horizontal' ,
'fieldConfig' => [
'enableError' => true ,
'options' => [
'class' => ''
]
] ] );
?>
<h2>Project Information</h2>
<h3>General Information</h3>
<div class="row">
<?php
echo $form->field (
$model , 'name' , [ 'template' => '<div class="col-sm-2">{label}</div><div class="col-sm-4">{input}{error}</div>' , ]
)->textInput ( [ 'maxlength' => true ] )->label ( null , [ 'class' => 'control-label' ] )
?>
<?php
echo $form->field (
$model , 'price' , [ 'template' => '<div class="col-sm-2">{label}</div><div class="col-sm-4">{input}{error}</div>' , ]
)->textInput ( [ 'maxlength' => true ] )->label ( null , [ 'class' => 'control-label' ] );
?>
</div>
<div class="row">
<?php
echo $form->field (
$model , 'product_subcategory' , [ 'template' => '<div class="col-sm-2">{label}</div><div class="col-sm-10">{input}{error}</div>' , ]
)->textInput ( [ 'maxlength' => true , ] )->label ( null , [ 'class' => 'control-label' ] );
?>
</div>
<?php
echo yii\bootstrap\Html::submitButton ( 'Submit' );
yii\bootstrap\ActiveForm::end ();
Hope this helps you out

Get data from textbox and pass to Controller in yii2

Itis the same question as Pass data from a form to a controller in yii2.
I want to get data from user in 3fields and pass it to a controller action.
I'm trying to do this by javascript but it seems that the javascript function is not being called.
Nothing happens when I click on the button. No error in the console as well.
I'm attaching the code of 3 fields,button and javascript below.
index2.php
<?php
use yii\helpers\Html;
//use yii\grid\GridView;
use kartik\grid\GridView;
use kartik\export\ExportMenu;
use frontend\modules\stock\models\Sellitem;
use dosamigos\datepicker\DatePicker;
use dosamigos\datepicker\DateRangePicker;
use kartik\form\ActiveForm;
/* #var $this yii\web\View */
/* #var $searchModel frontend\modules\stock\models\SellitemSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Stock';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="sellitem-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<div class="row">
<div class="form-group">
<div class="col-xs-4 col-sm-4 col-lg-4">
<label for="upc" class="control-label"><p class="text-info">Product Code <i class="icon-star"></i></p></label>
<input type="text" class="form-control" id="upc" class="span3">
</div>
<div class="col-xs-4 col-sm-4 col-lg-4">
<label for="upc" class="control-label"><p class="text-info">Start Date <i class="icon-star"></i></p></label>
<?= DatePicker::widget([
//'label' => 'Startdate',
'name' => 'startdate',
'id' => 'startdate',
//'value' => '02-16-2012',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
<div class="col-xs-4 col-sm-4 col-lg-4">
<label for="upc" class="control-label"><p class="text-info">End Date <i class="icon-star"></i></p></label>
<?= DatePicker::widget([
//'label' => 'Startdate',
'name' => 'enddate',
'id' => 'enddate',
//'value' => '02-16-2012',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
</div>
</div>
<p>
<div class="form-group pull-right">
<button class="btn btn-primary" onclick="getValue()">Seach</button>
</div>
</p>
</div>
<?php
/* start getting the itemid */
$this->registerJs('
function getValue()
{
var uPc = $(".upc").val();
var startDate = $(".startdate").val();
var endDate = $(".enddate").val();
alert(uPc);
}
');
/* end getting the itemid */
?>
The registerJs use some jQuery assigning so seems that the function getValue is not visible by the button call but you could use jQuery for assign the onclick and code.
Assuming your button has a id named yourButton you could do this way
<div class="form-group pull-right">
<button id="yourButton" class="btn btn-primary" >Seach</button>
</div>
$this->registerJs (
"$('#yourButton').on('click', function() {
var uPc = $('#upc').val();
var startDate = $('#startdate').val();
var endDate = $('#enddate').val();
alert(uPc);
});"
);
In your javascript code you have '.upc' '.startdate' '.enddate' this mean that you are looking for class upc, startdate endate ... but in your html there is not this class associated to the input field .. you have id then you should search in jQuery using #udc #startdate ... an so
you can do it using ajax by serializing your data and send it to controll action like
$('body').on('click','#btn_id',function(){
var data = $('form#id').serialize();
$.ajax({
'type' : 'get/post',
'dataType' : 'html/json',
'url' : 'controller/action',
'data' : {variable:data},
success : function(data){
console.log('Data Pass Successfully');
},
error : function(){
console.log('There is an Error....!!!');
}
});
});

How to add content inside link in cakephp

I would like to have content inside my link with cakephp. I know I can use this syntax to have an image inside my link
$this->Html->image("recipes/6.jpg", [
"alt" => "Brownies",
'url' => ['controller' => 'Recipes', 'action' => 'view', 6]
]);
For an HTML output like this :
<a href="/recipes/view/6">
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
But the above code idn't quite what I'm looking for. I would like to have the following HTML output
<a href="/recipes/view/6">
<div>
<img src="/img/recipes/6.jpg" alt="Brownies" />
</div>
</a>
I came with this code witch could work but I will have to figure out the full path link.
<a href="<?= '/recipes/view/6' ?>">
<div>
<?= $this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]); ?>
</div>
</a>
Is there a more robust way of doing what I want with cakephp?
I am not sure if this is a more robust way, but you could possibly do something like this:
<?php
echo $this->Html->link(
$this->Html->div(null, $this->Html->image('/img/recipe/6.jpg')),
array('controller' => 'recipes','action' => 'view', 6),
array('escape' => false)
);
?>
And the output would be this:
<a href="/recipes/view/6">
<div>
<img alt="" src="/img/recipes/6.jpg">
</div>
</a>
You can use Link or URL mechanism.
Using Link
echo $this->Html->link(
$this->Html->div(null,
$this->Html->image('recipes/6.jpg', ['alt' => 'Brownies'])),
array('controller' => 'Recipes', 'action' => 'view', 6),
array('escape' => false)
);
Using URL
Adding the URL inside the HTML
<a href="<?= $this->Html->url( array('controller' => 'Recipes', 'action' => 'view', 6),
array('escape' => false)); ?>" class="light_blue">
<div>
<?= $this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]); ?>
</div>
</a>