This question already has answers here:
URL in Yii2 GridView
(8 answers)
Closed 6 years ago.
An ajax request calls a below action whose response is JSON:
\Yii::$app->response->format = 'json';
if($userId){
$dataProvider = new ArrayDataProvider([
'allModels' => Templates::getTemplates($userId,'n'),
]);
$response = $this->renderAjax('index', ['dataProvider' => $dataProvider,]);
return ['status'=>true,'data'=>$response,'total'=>count($dataProvider)];
}
In View of this action, there is a GridView Widget:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute'=> 'template_name',
'label'=>'Test Name',
'value' => function($data){
$url = Yii::$app->urlManager->createUrl('templates/get-tests')."&id=".$data->id;
return ''.Html::encode($data->template_name).'';
}
],
[
'attribute'=> 'template_date',
'label'=>'Beginning Date'
],
[
'attribute'=> 'template_expire_time',
'label'=>'End Date'
],
'user_id',
],
]); ?>
But this encodes html value of template name. For Eg: test to test
and this renders at browser:
I do not need this encoding. Please help me to solve this.
you should use format => raw
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute'=> 'template_name',
'label'=>'Test Name',
'format' => 'raw',
'value' => function($data){
$url = Yii::$app->urlManager->createUrl('templates/get-tests')."&id=".$data->id;
return ''.Html::encode($data->template_name).'';
}
],
[
'attribute'=> 'template_date',
'label'=>'Beginning Date'
],
[
'attribute'=> 'template_expire_time',
'label'=>'End Date'
],
'user_id',
],
]); ?>
Related
I have install the miloschuman\highcharts\Highcharts, website : https://www.yiiframework.com/extension/yii2-highcharts-widget. I have a database with the table columns lme_price and lme_name which I want to display the price of the copper in the highcharts. I'm using PHP.
Below is my code which I have done. This is the code in my models. I create a static function with the query inside it to find the data that I want from the database.
public static function getCopperPrice()
{
$getCopperPrices = Lme::find()
->select('lme_price')
->where(['lme_name' => 'LME Copper'])
->all();
return $getCopperPrices;
}
Here is my code in the view. I have a table show in the view which show every data from the database.
<div class="lme_index">
<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'searchModel'=> $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label'=> 'DATE',
'attribute' => 'lme_title',
],
[
'label'=> 'MATERIAL',
'attribute'=> 'lme_name',
],
[
'label'=> 'PRICE (US$)',
'attribute'=> 'lme_price',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Here is the code for the highcharts graph.
<div class = "graph">
<?= Highcharts::widget([
'options' => [
'title' => ['text' => 'Copper Price'],
'xAxis' => [
'categories' => []
],
'yAxis' => [
'title' => ['text' => 'Price $US']
],
'plotOption' => [
'series' => [
['name' => 'Copper',
'data' => [lme::getCopperPrices()]],
]
]
]
]);?>
I want to display only 1 material price in the highcharts. I call the function from the model inside the series but the graph shows nothing on it. The coding there didn't show any error to me.
Can someone help me or tell me how to solve this? Thanks.
Chart require integer values to display
public static function getCopperPrice()
{
$cooperPrices = [];
$copperPriceList = Lme::find()
->select('lme_price')
->where(['lme_name' => 'LME Copper'])
->column();
foreach($copperPriceList as $price) {
$cooperPrices[] = (int) price;
}
return $cooperPrices;
}
This is my grid :
I made a query and created a dataprovider to view in a gridview, and now I want to view each row in a particular view. How do I get all line data to send when clicking preview?
on my controller
$model = new Aviso();
if ($model->load(Yii::$app->request->post())) {
$seg = $model->seguradora;
$seguradora = SeguradoraSearch::getNomeSeg($seg);
$query = new Query;
$query->select(['apolice_idapolice','nome','email', 'contacto','premio','data_final','situacao'])
->from('seguro')
->innerJoin('cliente', 'cliente_idcliente = idcliente')
->where(['between','data_final' ,$model->data_inicio,$model->data_final])
->andWhere(['situacao'=> "Pendente"])
->andWhere(['seguradora_idseguradora'=>$seg]);
$dataAviso = new ActiveDataProvider([
'query' => $query,
]);
return $this->render('index',[
'dataProvider' => $dataAviso,'segName'=>$seguradora,
]);
}
return $this->render('create', [
'model' => $model,
]);
}
On My index
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['attribute'=>'nome',
'label'=> 'Cliente',
],
['attribute'=>'email',
'label'=> 'E-mail',
],
['attribute'=>'apolice_idapolice',
'label'=> 'Apolice',
],
'contacto',
['attribute'=>'premio',
'format' => ['decimal', 2],
'label'=> 'Valor (Mts)',
],
'data_final',
['attribute'=>'situacao',
'label'=> 'Situação',
],
['class' => 'yii\grid\ActionColumn', 'template' => '{view}','header' => 'visualizar' ],
],
]); ?>
<?php Pjax::end(); ?>
I wamt to click on the icon visualizar and render on particular view.
You could configure the ActionColumn in proper way using template and urlCreator
assuming $model->id is you target id for the view
['class' => 'yii\grid\ActionColumn',
'template' => '{view}',
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'view') {
return \yii\helpers\Url::to(['your-controller/your-action', 'id' => $model->id]);
}
}
],
Here is my view page view.php
<div class="dataTables_wrapper form-inline dt-bootstrap">
<?= GridView::widget([
'dataProvider' => $dataProvider1,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' =>'Date',
'value' => 'date',
],
[
'label' =>'Reminder',
'value' => function($model){
return Yii::$app->session->get('').' '.$model['type'].' '.'reminder';
}
],
[
'label' =>'To',
'value' => 'recipients',
],
]
]); ?>
</div>
This is controller
$dataProvider1 = new ActiveDataProvider([
'query' => DomainReminders::find()
->where(['domain_id'=>$model['id']])
]);
$totalCount = $dataProvider1->getTotalCount();
return $this->renderAjax('view', [
'totalCount' => $totalCount,
'dataProvider' => $dataProvider,
'dataProvider1' => $dataProvider1,
true,
true
);
This is database with column name 'type' I want print like 1st reminder, 2nd reminder, 3rd reminder in grid view
Now here is my output i want print like 1st, 2nd reminder
You may use Inflector::ordinalize() for this:
[
'label' => 'Reminder',
'value' => function ($model) {
return Inflector::ordinalize($model['type']) . ' reminder';
},
],
i have Gridview in index i want to show width and height both in one column how can i do it
here is the view code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'fld_id',
'fld_name',
[
'label' => 'Material Name',
'attribute' => 'fld_material_id',
'value' => 'fldMaterial.fld_name',
],
[
'label' => 'Size',
'attribute' => 'fld_size_id',
'value' => 'fldSize.fld_width',
],
// 'fld_size_id',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
i have relation fldSize in model here it is just only displaying fld_width i want to show it in the format fld_width."x".fld_height how can i do it in Yii2
You should simply use value callback, e.g. :
[
'label' => 'Size',
'attribute' => 'fld_size_id',
'value' => function ($model) {
return $model->fldSize->fld_width . 'x' . $model->fldSize->fld_height;
},
],
Sorry that after more than one year, but it works (not a dropdown but Select2).
Here is the code for the form
<?= $form->field($model, 'ID_MACH')->widget(Select2::classname(), [
'data'=> ArrayHelper::map(Maschines::find()->all(),'ID_MACH','fullname'),
'language'=>'ru',
'theme'=>'krajee',
'options'=>['placeholders'=>'suda...',
'prompt'=>'10-'],
'pluginOptions'=>[
'allowclear'=>true
],
Next is the Model for Mashines:
public function getFullName()
{
return $this->customer.','.$this->Manufacturer.','.$this->type.','.$this->serial;}
This question already has answers here:
Changing value of an attribute in DetailView widget
(2 answers)
Closed 6 years ago.
I get an error when I use a function to get the value of an attribute and it's working normally using Gridview. What I'm doing wrong?
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'subject_type',
'value' => function ($data) {
return Lookup::item("SubjectType", $data->subject_type);
},
'filter' => Lookup::items('SubjectType'),
],
'id',
'subject_nature',
],
]) ?>
I have experienced this kind of problem. The error was
PHP Warning – yii\base\ErrorException
htmlspecialchars() expects parameter 1 to be string, object given
what I did was transfer the function in the model like this.
function functionName($data) {
return Lookup::item("SubjectType", $data->subject_type);
},
and then in your view.php file..
[
'label'=>'subject_type',
'value'=>$model->functionName($data),
],
Just use call_user_func()
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'subject_type',
'value' => call_user_func(function ($data) {
return Lookup::item("SubjectType", $data->subject_type);
}, $model),
'filter' => Lookup::items('SubjectType'),
],
'id',
'subject_nature',
],
]) ?>
Fabrizio Caldarelli, on 05 January 2015 - 03:53 PM, said:
Yes because 'value' attribute is a real value or attribute name, as doc says
So your code should be:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'subject_type',
'value' => Lookup::item("SubjectType", $model->subject_type),
'filter' => Lookup::items('SubjectType'),
],
'id',
'subject_nature',
],
]) ?>