I want to sort a column in TbJSONGridView by date , I have this code
$this->widget(
'bootstrap.widgets.TbJsonGridView', array(
'dataProvider' => $model->searchPending(),
'type' => 'striped bordered condensed',
'summaryText' => false,
'cacheTTL' => 10, // cache will be stored 10 seconds (see cacheTTLType)
'cacheTTLType' => 's', // type can be of seconds, minutes or hours
'enablePagination' => true,
'columns' => array(
array(
'name' => 'Pickup Date',
'value' => '$data->carShippeds[0]->pickup_date',
'type'=>'date',
),
))
but the column I get it is from a related model, and the sorting is not working, when I clicked on the column header it does nothing.
what's wrong with the code???
Gabriel
$data->carShippeds[0]->
This shows us that the current model has many cars shipped. You are only showing the first one in there. While that works, Yii has no way of knowing that you are only showing the first value so your sorting has no chance of working. Create a view that only selects your first pickup date, make a join in your criteria with the main model, show the field without [0]-> and it will work.
Change the below section:
array(
'name' => 'Pickup Date',
'value' => '$data->carShippeds[0]->pickup_date',
'type'=>'date',
),
To this:
array(
'name' => 'pickup_date',
'header' => 'Pickup Date',
'value' => '$data->carShippeds[0]->pickup_date',
'type'=>'date',
),
Then in your 'form' when you want to return DataProvider write the specific sorting code for this association like below:
return new DataProvider($query, [
'pagination' => [
'pagesize' => 20,
],
'sort' => [
'sortVar' => 'sort',
'defaultOrder' => ['t.carShippeds.pickup_date asc'],
'attributes' => [
'pickup_date' => [
'asc' => 't.carShippeds.pickup_date',
'desc' => 't.carShippeds.pickup_date DESC',
],
'*',
],
],
]);
Please notice that I did not know the join alias you have for your t.carShippeds or even whether it is t or another entity. replace it or give me more information about your DataProvider and I can help more.
Related
['header'=>'Bill No',
'attribute'=>'billid',
'filter'=>ArrayHelper::map(Bills::find()->all(),'id','billno'),
'format' =>'text',
'value'=> function($data){
return ($data->billid)?$data->bill->billno:'Nope';
}]
By using this code, I am getting a drop-down box as a filter, but bill number is going to be in 10000's. So I want an input type text as a filter for my foreign key.
Relationship:
DB Table:
Cashbook
Column: billed
On
DB Table:
Bills
Column:id
And I want to extract bill no from the id for this filter. Please Help
I want to search and sort for billno.
A better alternative is to use kartik\Select2 so that you can have the dropdown and search both 2 in 1.
There is an option for the minimum number of character that you have to type in to open the drop-down rather than opening the drop-down with 1000's of options at once by clicking on it.
minimumInputLength: Minimum number of characters required to start a search.
[
'header' => 'Bill No',
'attribute' => 'billid',
'filter' => \kartik\widgets\Select2::widget([
'data' => ArrayHelper::map(Bills::find()->all(),'id','billno'),
'model' => $searchModel,
'attribute' => 'billid',
'options' => [
'placeholder' => 'Bill No',
'id' => 'bill_id',
],
'theme' => \kartik\widgets\Select2::THEME_BOOTSTRAP,
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => '3',
],
]),
'format' =>'text',
'value'=> function($data){
return ($data->billid)?$data->bill->billno:'Nope';
}
],
Hope that helps you out
how can I add an virtual column to the TCA (TYPO3 8)? I have in a 1:n table with data and I want to display the count of the data in the backend to current element.
I need something like this:
$fields = [
'counts7d' => [
'exclude' => false,
'label' => 'last 7 days',
'config' => [
'type' => 'none',
'procFunc' => '\Namespace\MyClass->MyMethod',
'readOnly' => true,
'params' => [
'period => '7d'
]
]
],
'counts30d' => [
'exclude' => false,
'label' => 'last 30 days',
'config' => [
'type' => 'none',
'procFunc' => '\Namespace\MyClass->MyMethod',
'readOnly' => true,
'params' => [
'period => '30d'
]
]
],
];
pseudo function:
public function myMethod($element, $params){
$sql = "SELECT count(*) FROM TABLE WHERE pid=$element[uid] and date > $params[period]";
return sql_count…
}
The field should only be informative for the backend users.
Does anyone have an idea?
Thanks
Oliver
The TCA field type user is exactly what you are looking for:
'counts7d' => [
'exclude' => false,
'label' => 'last 7 days',
'config' => [
'type' => 'user',
'userFunc' => \Namespace\MyClass::class . '->MyMethod',
'parameters' => [
'period => '7d',
],
],
],
The TCA field type none is exactly what you are looking for. Type none is the only type that does not necessarily need a database field. To manipulate it you can use userFunc which allow you to use custom php function.
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',
),
)
Im trying change my GridView column width. This is my code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'prefixs-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'id',
'header' => 'No.',
'htmlOptions' => array('class' => 'center'),
),
array(
'name' => 'pfx',
'htmlOptions' => array('width' => 30),
),
array(
'class' => 'CButtonColumn',
),
),
)); ?>
When i run 'pfx' columns width must be changed to 30, but width is same. How i can change width of column? Any ideas?
If you do not want to use a separated CSS definition you should do:
'htmlOptions' => array('style' => 'width: 30px;'),
'filterHtmlOptions' => array('style' => 'width: 30px;'),
'columns' => [
[
'headerOptions' => ['width' => '150'],
'attribute' => 'name',
'format' => 'raw',
'value' => 'name'
],
]
There are multiple ways to approach this issue.
You can widen the cell's contents (cells of individual rows):
$columns[] = array(
'header'=>'Name',
'value'=>'$data["name"]',
'htmlOptions'=>array('style'=>'width: 150px')
);
Sometimes you may want to widen the column header instead of the individual rows' cells:
$columns[] = array(
'header'=>'Name',
'value'=>'$data["name"]',
'headerHtmlOptions'=>array('style'=>'width: 150px')
);
Widening the header is the preferred option, but even if you do that, sometimes it doesn't work as expected.
If this is the case, you can go my personal preferred way and widen the contents of the header cell. The widening will be inherited by the rows below it as well.
$columns[] = array(
'header'=>'<div style="width:150px;">Name</div>',
'value'=>'$data["name"]',
);
i noticed with cgridview in the tags there is an id attribute id="table-name_c0" id="table-name_c1" etc. you could hook into this and the .filters class for the filter width. e.g.
#table-name table-name_c0 {
width: 30px;
}
#table-name .filters:nth(0) {
width:30px;
}
where c0 and :nth(0) are the same number.
it's slightly hacky but what you are trying to achieve is also slightly hacky imho
Simply add
'contentOptions' =>['style' => 'width:30px'],
-- For example:
'columns' => [
[
'attribute' => 'product.name',
'contentOptions' =>['style' => 'width:30px'],
],
That should do the trick!
I am trying to validate a multiply select using input filter, but every time I see a error. The error is "notInArray":"The input was not found in the haystack".(I use ajax but it doesn`t metter).
I will show part of my code to be more clear.
in Controller:
if ($request->isPost()) {
$post = $request->getPost();
$form = new \Settings\Form\AddUserForm($roles);//
$form->get('positions')
->setOptions(
array('value_options'=> $post['positions']));
//.... more code...
When I put print_r($post['positions']); I see:
array(0 => 118, 1 => 119)
in ..../form/UserForm.php I create the multiply element
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
and in the validation file the code is:
$inputFilter->add($factory->createInput(array(
'name' => 'positions',
'required' => false,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => array(118,119),
'messages' => array(
'notInArray' => 'Please select your position !'
),
),
),
),
What can be the reason every time to see this error, and how I can fix it?
By default selects have attached InArray validator in Zend Framework 2.
If you are adding new one - you will have two.
You should disable default one as follow:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
'disable_inarray_validator' => true, // <-- disable
),
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
And you should get rid of the additional error message.
Please let us know if that would helped you.