Yii Framework: Pagation Callback Method - php

I have a CListView on a page and every time i navigate to page 2 or any other page I want to call a method that formats the view. But it do not seem to work every time I navigate to another page.
The Javascript method I want to call is called updateDivs()
Here is my list view widget
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_customview',
'id' => 'bannerslist',
'ajaxUpdate' => true,
'afterAjaxUpdate' => ' updateDivs()',
'enablePagination' => true,
'itemsCssClass' => 'row banners-list',
'summaryText' => 'Total ' . $pages->itemCount . ' Results Found',
'pager' => array(
'header' => '',
'prevPageLabel' => '<',
'nextPageLabel' => '>',
),
));

Please try this to test, it should works well.
'afterAjaxUpdate' => 'js:function(id,data){alert("test");}'
So if this code will works perfect then you should search problem in your updateDivs() function

Your problem is that, you missed to write js:function(id, data) which is significant to call a method.
You can use as follows :
'afterAjaxUpdate'=>'js:function(id, data) {updateDivs();}',
Look here for more info.

Related

yii2 pjax in the listview fails in pagination

I have a listview that i would like use pagination using pjax but the pagnation fails to work with pjax
This is what i have tried
<?php Pjax::begin([
'id' => 'w0', //checked id on the inspect element
'enablePushState' => true // i would like the browser to change link
]); ?>
<?= ListView::widget([
'summary' => false,
'dataProvider' => $dataProvider,
'emptyText' => "",//Yii::t('app', 'Sorry we have no items currently.'),
'itemOptions' => ['class' => 'item'],
'options' => ['data-pjax' => true ], //this is for pjax
'layout' => '{items}{pager}',
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
'maxButtonCount' => 5,
'options' => [
'class' => 'pagination col-xs-12'
]
],
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('_items_list', ['model' => $model]); //This shows one model
},
]) ?>
<?php Pjax::end(); ?>
When i click on the items nothing hapens and when i remove the pjax opening and closing tags it works with normal page refresh what else do i need to add
You are close to your solution.
<?php Pjax::begin([
'id' => 'w0', // checked id on the inspect element
'enablePushState' => true // I would like the browser to change link
'timeout' => 10000 // Timeout needed
]); ?>
I have added only 1 line: 'timeout' => 10000. By default timeout is equal to 1000 (one second). Since you do not get back results within that 1 second, browser uses normal page refresh instead of Pjax. By replacing 1000 to 10000, you give 10x more time to get response (it shouldn't take that long but just in case). Now Pjax has enough to retrieve results from server and update ListView without reloading the page.
If you still get nothing, it is likely to be an error on server side. Check network (in browser) to see what server returns (it should return status 200).

Cakephp 3 - CakePDF and Search Plugin - render filtered data to PDF?

I'm using the FriendsOfCake CakePDF Plugin with wkhtmltopdf to render my views as PDF.
I also use their Search plugin to filter view data with a form.
For now, when I print the data in the view it always renders all view data into the PDF and not only the filtered data that is displayed on screen.
Is there any way to do this? I can't find anything that mentions such a case in the Plugin Docs. It seems like the PDF plugin always reloads the page in its default state or rather loads the default query from the index function instead of the filtered data. Since this is my first CakePDF project I don't really get what I have to do to make it render the filtered data instead. Can anybody help with that?
Here is what my main files look like so far:
class PaintingsController extends AppController
{
public function index()
{
$query = $this->Paintings
->find('search',
$this->Paintings->filterParams($this->request->query))
->contain(['Artists',
'Tickets' => function ($q) {
return $q->where(['Tickets.active' => false]);
}
]);
$this->viewBuilder()->options([
'pdfConfig' => [
'orientation' => 'portrait',
'filename' => 'paintings.pdf'
]
]);
$this->set('paintings', $this->paginate($query));
$this->set('_serialize', ['paintings']);
}
}
class PaintingsTable extends Table
{
public function searchConfiguration()
{
$search = new Manager($this);
$search->like('title', [
'before' => true,
'after' => true,
'field' => $this->aliasField('title'),
'filterEmpty' => true
])->value('property', [
'field' => $this->aliasField('property'),
'filterEmpty' => true
])->like('artist_name', [
'before' => false,
'after' => true,
'field' => $this->Artists->target()->aliasField('surname'),
'filterEmpty' => true
])->value('technique', [
'field' => $this->aliasField('technique'),
'filterEmpty' => true
]);
return $search;
}
}
In Template\Paintings\index.ctp
... data in tables ...
<?= $this->Html->link('Save as PDF',[
'action' => 'index',
'_ext' => 'pdf'],[
'class' => 'create-pdf-link',
'target' => 'blank'
]) ?>
Then everything gets rendered in Templates\Paintings\pdf\index.ctp without the applied filtering.
Your PDF link won't contain any filter paramters, so there is no reload or anything, it just won't do any filtering.
The current query is not being incorportated automatically when generating links/URLs, you have to explicitly pass it to the URL array on your own, like
$this->Html->link(
'Save as PDF',
[
'action' => 'index',
'_ext' => 'pdf'
] + $this->request->query, // there it goes
[
'class' => 'create-pdf-link',
'target' => 'blank'
]
);
See also Cookbook > Routing > Generating URLs
FIY, in CakePHP 2, things are a bit different, you would use
$this->request->params['named']
instead of
$this->request->query
like
$this->Html->link(
'Save as PDF',
[
'action' => 'index',
'_ext' => 'pdf'
] + $this->request->params['named'],
[
'class' => 'create-pdf-link',
'target' => 'blank'
]
);

Jquery attr doesnt work on Yii

I'm trying to change the element value using Jquery but it's not working...
This is my widget where I have my element 'tag' which i want to change it to textField on edit...
$this->widget('EditableGrid', array(
'dataProvider' => $dataProvider->searchbyID($invoice_id),
'template' => '{items}{buttonCreateRow}{pager} ',
'id' => 'InvoiceLine-grid',
'rowTemplate' => $row_template,
'columns' => array(
array(
'class' => 'EditableGridColumn',
'header' => '',
'name' => 'InvoiceLine_{gridNum}_{rowNum}_edit',
'imageurl'=> Yii::app()->request->baseUrl.'/images/update.png',
'tag' => 'button',
'tagHtmlOptions' => array(
'onclick'=>'editline(this)',
)
),
array(
'class' => 'EditableGridColumn',
'header' => 'StentysRef',
'name' => '[{gridNum}][{rowNum}]stentysproductref',
'tag' => 'laabel',
'tagHtmlOptions' => array(
'style'=>'background-color:#FFF;border-color:#FFF',
'onkeyup'=>'stentysref(this)',
'readonly'=>true
)
),
My Jquery is,
(as you can see the removeAttr works but attr doesn't)
function editline(obj){
$("#InvoiceLine_1_"+row+"_stentysproductref").attr("tag","textField");
$("#InvoiceLine_1_"+row+"_stentysproductref").removeAttr("readonly");
}
Use .prop() and removeProp() which is added in jQuery 1.6, as the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior.
function editline(obj){
$("#InvoiceLine_1_"+row+"_stentysproductref").prop("tag","textField");
$("#InvoiceLine_1_"+row+"_stentysproductref").removeProp("readonly");
}
API Doc for .prop()
If you are using EditableGrid widget without pointing AR model, then the row id looks like
$('#_1_'+row+'stentysproductref').
I'll take it into consideration for the future updates. Thank you.

how do i change the alignment of yii framework pager?

<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view',
'ajaxUpdate' => true,
'pager' => array(
'class' => 'CLinkPager',
),
'template' => '{pager}{items}',
));
?>
after executing this the pager components are displaying as column format.is their any way to change that into row format?
I guess you getting this widget through renderPartial() method. When using the renderPartial() method controller not sending js and css files with response by default. If you want to get response with these files you should pass true in fourth parameter:
$this->renderPartial('myView',array(), false, true);

Changing page labels (clistview, cgridview) witout changing pager css (bootstrap)

When I set 'pager' in CGridView it change bootstrap look, I try with cssFile to null or false, it doesn't work.
'pager' => array(
'class' => 'CLinkPager',
'firstPageLabel' => '<<',
'prevPageLabel' => 'smthprev',
'nextPageLabel' => 'smthnext',
'lastPageLabel' => '>>',
),
The Bootstrap extension checks if htmlOptions['class'] is set , i.e : isset($htmlOptions['class']), so if we set empty class, then it should work. So add this:
'pager' => array(
// rest of code
'htmlOptions'=>array('class'=>'')
),

Categories