I have few tables. position(to holds all positions), applicants(to hold all applicants) and assign_applicant(to say which position is held by what applicant - many to many reln).
So data is like for assign_applicant table
pos applicant
2 1
2 3
3 4
3 5
I want to display the records like this. but instead it shows :
pos applicant
2 1,3 (array)
3 4,5 (array)
The reason is (many) relations:
public function getAssign()
{
return $this->hasMany(\admin\models\AssignApplicant::className(), ["job_position_id" => "id"]);
}
public function getApplicant()
{
return $this->hasOne(\admin\models\Applicant::className(), ["id" => "applicant_id"]) ->via('assign');
}
Here is the grid view:
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => "{pager}\n{summary}\n{items}\n{pager}",
'filterPosition' => \yii\grid\GridView::FILTER_POS_HEADER,
'responsive' => true,
'hover' => true,
'resizableColumns' => true,
'floatHeader' => true,
'autoXlFormat' => true,
'export' => [
'fontAwesome' => true,
'showConfirmAlert' => true,
'target' => GridView::TARGET_BLANK
],
'panel' => [
'type' => 'primary',
'heading' => ''
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'position',
'format' => 'raw',
'value' => function ($model)
{
return Html::a($model->positionName, yii\helpers\Url::toRoute(['position/update', 'id' => $model->position_id]));
}
],
[
'attribute' => 'applicantName',
'value' => 'applicantName'
],
],
]);
?>
My query (raw form):
SELECT `job_positions` . * , applicant.first_name, assign_applicant.id
FROM `job_positions`
LEFT JOIN `position` ON `job_positions`.`position_id` = `position`.`id`
INNER JOIN `assign_applicant` ON `job_positions`.`id` = `assign_applicant`.`job_position_id`
INNER JOIN `applicant` ON `assign_applicant`.`applicant_id` = `applicant`.`id`
WHERE (
`client_id` = '1'
)
AND (
`shift_id` = '1'
)
AND (
`date` = '2016-12-08'
)
ORDER BY `position`.`name`
LIMIT 20
Any way to show the data like I want
yii2 intentionally removes duplicated models when processing results.
your can either use applicants (or assign_applicant) as primary model as #Bizley suggested
or process the resulting models beforehand (using clone and populateRelation to obtain the desired structure)
Related
I'm trying to create summary row for my GridView which sums my quantity for current page and for all records seperately.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'layout' => "{summary}\n{items}\n<div align='right'>{pager}</div>",
//'filterModel' => $searchModel,
'showPageSummary' => true,
'pageSummaryFunc' => GridView::F_SUM,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'class' => DataColumn::className(),
'attribute' => 'updated_at',
'format' => 'raw',
'value' => function ($model, $key, $index, $column){
return date('Y-m-d', $model->updated_at);
},
],
[
'label' => 'Ilość',
'attribute'=>'quantity',
'pageSummary' => true,
'value'=> function ($model, $key, $index, $column) {
return ($model->quantity) ? $model->quantity : '';
},
],
],
]); ?>
At the end i want to export sum for all records to my excel sheet at the bottom of my gridColumns. My export widget looks like:
$dataProviderAll->setSort([
'defaultOrder' => ['updated_at' => SORT_ASC]
]);
echo ExportMenu::widget([
'container' => ['class' => 'btn-group pull-right', 'role' => 'group'],
'dataProvider' => $dataProviderAll,
'columns' => $gridColumns,
'filename' => date('Y-m-d') . '_raport',
'target' => ExportMenu::TARGET_SELF,
'showConfirmAlert' => false,
'showColumnSelector' => false,
'fontAwesome' => true,
'dropdownOptions' => [
'label' => 'Eksportuj dane',
'class' => 'btn btn-primary btn-md'
],
'exportConfig' => [
ExportMenu::FORMAT_HTML => null,
ExportMenu::FORMAT_TEXT => null,
ExportMenu::FORMAT_EXCEL => null,
ExportMenu::FORMAT_PDF => null
]
]);
?>
All columns of model are already exported, but i need to add summary row for some of them into created excel file.
Im getting now error like this: Setting unknown property: kartik\grid\GridView::pageSummaryFunc
Does someone know how can I get this result?
I have fixed part where I wanted to get sum per page by replacing class column from yii to kartik. –
I've createCommand in controller
$receipts=new receipts();
$query1 = new Query;
$query1 ->select(['price','date','id'])
->from('paymentreceipts')
->join( 'INNER JOIN','patient','patient.patient_id =paymentreceipts.patient_id' )
->where('patient.patient_id=:id', array(':id'=>$id))
->orderby ('paymentreceipts.date');
$command1 = $query1->createCommand();
$dataProvider1 = $command1->queryAll();
$gridViewDataProvider3 = new \yii\data\ArrayDataProvider([
'allModels' => $dataProvider1,
'sort' => [
'attributes' => ['price','reg_date','id'],
],
]);
return $this->render('view', [
'model' => $this->findModel($id),
'gridViewDataProvider2' => $gridViewDataProvider2,
]);
in view.php
<?=
GridView::widget([
'dataProvider' => $gridViewDataProvider2,
// 'pjax'=>true,
'summary'=>'',
'showFooter' => true,
'columns' => [
['label' => 'رقم الخدمة',
'attribute' => 'id',
],
[
// 'label'=>'url',
'format' => 'raw',
'value'=>function ($data) {
return Html::a('$data->date','#',['class' => 'receipts','id'=>'id']);
},
],
]
]) ?>
what i need is : add the id in 'attribute' => 'id' number to the link that i created here ['class' => 'receipts','id'=>'id']);
i used 'id'=> $gridViewDataProvider2->id but it doesn't work !
My English is not good , so maybe my question not be clear .
JOIN statement is the reason why you don't receive "id". Because both of your tables have "id" column and it becomes ambiguous to read it. Change you query like below,
$query1 ->select(['price','date','id as receiptId'])
->from('paymentreceipts')
->join( 'INNER JOIN','patient','patient.patient_id =paymentreceipts.patient_id' )
->where('patient.patient_id=:id', array(':id'=>$id))
->orderby ('paymentreceipts.date');
$command1 = $query1->createCommand();
and then you can make the link like below.
return Html::a('$data->date','#',['class' => 'receipts','id'=>$data->receiptId]);
I want to display total of row vaules in footer.
I have tried several ways but doesn't show proper result.
Any help will be highly appriciated.
I am using kartik grid.
I have tried this Yii2: Kartik Gridview sum of a column in footer
but doesn't work in my case.
$gridColumns = [
[
'class' => 'kartik\grid\SerialColumn',
'contentOptions' => ['class' => 'kartik-sheet-style'],
'width' => '36px',
'header' => '',
'headerOptions' => ['class' => 'kartik-sheet-style']
],
[
'attribute' => 'vno',
'footer'=>'Total'
],
[
'attribute' => 'fliters',
'label' => 'Total Fuel Consumption Liters',
'footer'=> true // Here i want to display the sum of column filer ( Note : the value in grid of filter is a sum(fuel_liters) )
],
[
'attribute' => 'fuel_rate',
'label' => 'Fuel Rate (Per Ltr.)',
'pageSummary'=>true,
'footer'=>true // Here i want to display the sum of column fuel_rate ( Note : the value in grid of filter is a sum(fuel_rate) )
],
[
'attribute' => 'famt',
'label' => 'Total Fuel Consumption Amount',
'pageSummary'=>true,
'footer'=>true // Here i want to display the sum of column fuel_amount ( Note : the value in grid of filter is a sum(fuel_amount) )
],
];
echo GridView::widget([
'dataProvider'=> $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true
]);
So far I manage to print total in footer
Im my Controller i have wriiten this
$total_ltrs = FuelConsumption::find()->sum('fuel_liters');
$total_amt = FuelConsumption::find()->sum('fuel_amount');
$total_rate = FuelConsumption::find()->sum('fuel_rate');
return $this->render('petrol-report', [
'total_ltrs' => $total_ltrs,
'total_amt' => $total_amt,
'total_rate' => $total_rate,
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
And passed 'footer'=>$total_ltrs 'footer'=>$total_rate and 'footer'=>$total_amt Respectively in all footers.
Now when i do search throgh From Date and TO Date the result is dislaying in grid as per filter.
But the Result of Total in footer keeps unchanged.
I want to do some of only those rows which are in grid.
Can anybody help me on this ??
in your GridView, enable showFooter
in your column, set the footer text
now you will need a function to return your sum and set is a the footer's text.
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'showFooter' => true,
'columns' => [
[
'class' => '\common\grid\CheckboxColumn'
],
[
'attribute' => 'myfield1',
'footer' => 'my footer'
],
],
]);
if you want a function, use a static helper:
class MyHelper {
public static function footer() { time(); }
}
'footer' => MyHelper::footer()
I have load select2 data like this :
$data = ArrayHelper::map(ContactGroups::find()->where(['group_status'=>'ACTIVE'])->asArray()->all(),'group_id', 'group_name');
echo $form->field($model, 'group_id')->widget(Select2::classname(), [
'data' => $data,
'model' => $model,
'language' => 'en',
'options' => ['placeholder' => Yii::t('modules','Pilih Kelompok')],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
],
])->label('Kelompok');
$data variable returning result :
Array
(
[1] => Tanpa Kategori
[3] => Bisnis
[4] => Kawan
[5] => Bisnis Kerang
[6] => Bisnis Selang
[99] => Keluarga
)
and select2 working properly, but I can't show selected value or initial value. is I've missed something ?
you add tags property in pluginOptions for multiple selection like....
$data = ArrayHelper::map(ContactGroups::find()->where(['group_status'=>'ACTIVE'])->asArray()->all(),'group_id', 'group_name');
foreach($data as $d)
$row[]=$d;
echo $form->field($model, 'group_id')->widget(Select2::classname(), [
'language' => 'en',
'name' => 'group_id[]',
'options' => ['placeholder' => ''],
'pluginOptions' => [
'tags' => $row,
'allowClear' => true,
'multiple' => true
],
])->label('Kelompok');
You show Demo
Try using like this.. At the time of updating we need to take already selected values in 1 variable and all values in 1 variable.. and send this to select2.
$query = NewsTags::find()->where(['news_id' => $model->id])->all();
$services = array();
$services_id_list = array();
foreach ($query as $ds) {
$tag_id = $ds->tag_id;
$tag_name = Tags::findOne($tag_id)->tag_name;
$services[$ds->tag_id] = $tag_name;
array_push($services_id_list, $ds->tag_id);
}
$data= ArrayHelper::map(Tags::find()->where([])->all(),'id','tag_name');
echo Select2::widget([
'name' => 'NewsTags[tag_id][]',
'id' => 'newstags-tag_id',
'value' => $services_id_list,
'data' => $data,
'maintainOrder' => true,
'options' => [
'placeholder' => 'Select a Service ...',
'multiple' => true
],
'pluginOptions' => [
'tags' => true,
'maximumInputLength' => 10
],
]);
here NewsTags[tag_id][] is the model and its column. we are not directly calling $model->attribute here
Having a look at the code of kartik\base\InputWidget line 190 :
if ($this->hasModel()) {
$this->name = !isset($this->options['name']) ? Html::getInputName($this->model, $this->attribute) : $this->options['name'];
$this->value = !isset($this->options['value'])? Html::getAttributeValue($this->model, $this->attribute) : $this->options['value'];
}
I've found out that, when loading data with AJAX, initial multiple values should be set in options[value] like this:
<?= $form->field($model, $attribute)->widget(Select2::className(), [
'initValueText' => $initText, // array of text to show in the tag for the selected items
'options' => [
'placeholder' => 'Any text you want ...',
'multiple' => true,
'class' => 'form-control',
'value' => $initIds, // array of Id of the selected items
],
whereas setting value next to initValueText leads to an array_combine error
So, I finally have a progress bar in my GridView, thanks to the BootProgressColumn. However, I have a rating system, and I'd like to show the percentage inside the progress bar. I can set it the hard way in the columns array, but that'll be for all the rows.
$rawData = Item::model()->findAllByAttributes(array('special_id' => $specialId));
$dataProvider = new CArrayDataProvider($rawData, array());
The GridView:
'dataProvider' => $dataProvider,
'columns' => array(
array(
'class' => 'application.components.BootProgressColumn',
'name' => 'Rating',
'animated' => true,
'striped' => true,
'percent' => '44',
),
)
Now, how do I get the sum of all columns where each respective $data->id matches rows in the Rating model?
$countRatings = count(Rating::model()->findAllByAttributes(array('item_id' => $data->id));
But what to do for sum? And how to divide sum by count? That all having done, how to get it in each column? Or would it be best to just create a table by myself?
If I understand your question well, you want to get the average rates for each item, here is what you can do:
in the Item model create the relation avgRating:
public function relations()
{
return array(
'avgRating' => array(self::STAT, 'Item', 'item_id', 'group' => 'item_id', 'select' => 'ROUND(AVG(rate),1)'),
);
}
and then you can call this for each item to get the rating in your table:
'dataProvider' => $dataProvider,
'columns' => array(
array(
'class' => 'application.components.BootProgressColumn',
'name' => 'Rating',
'animated' => true,
'striped' => true,
'percent' => '$data->avgRating',
),
)