Yii Cgridview Erases JS On Refresh - yii

I'm using bootstrap and Bootstrap Editable extension in my GridView to edit data fast. It's awesome, but when I somehow refresh my gridview with AJAX (e.g. apply some fiter or delete a row) it dissapears from the page and if I want to edit-in-place again I have to refresh page manually (F5). This is how I apply editable plugin in my views/admin.php file:
Yii::app()->clientScript->registerScriptFile(
Yii::app()->assetManager->publish(
Yii::getPathOfAlias('ext.editable.assets.js').'/bootstrap-editable.min.js'
),
CClientScript::POS_HEAD
);
Yii::app()->clientScript->registerCssFile(
Yii::app()->assetManager->publish(
Yii::getPathOfAlias('ext.editable.assets.css').'/bootstrap-editable.css'
),
CClientScript::POS_HEAD
);
Yii::app()->clientScript->registerScript('editable', '$(".editable").editable();', CClientScript::POS_END);
Yii::app()->clientScript->registerCss('popover', '.popover{width: 600px !important;} .editable-popover{width: auto !important;}', CClientScript::POS_HEAD);
It's kind of messy, but I'm just trying to get it working.
Appreciate any help.

Try looking at http://www.yiiframework.com/doc/api/1.1/CGridView#afterAjaxUpdate-detail
ie.
'afterAjaxUpdate' => 'function() { $(".editable").editable(); }'

Related

Yii2 Gridview get all selected row for all pagination

I wrapped my gridview with Pjax widget like this
\yii\widgets\Pjax::begin();
gridview
\yii\widgets\Pjax::end();
in order to make the gridview make ajax request when I click on each pagination.
I also use ['class' => 'yii\grid\CheckboxColumn'], in column as well.
and I find that when I'm on first pagination I checked some rows and then go to second page and check some rows but when I go back to first page what I've checked is gone.
My question is how can I keep all checkedrow for all pagination
With current conditions (Pjax, multiple pages, yii\grid\CheckboxColumn) it's impossible because of the way it works.
When you click on the pagination links all GridView html content is replaced by new one that comes from the AJAX response.
So obviously all selected checkboxes on the previous page are gone.
Few possible ways to solve that:
1) Write custom javascript and server side logic.
As one of the options, you can send AJAX request to server with parameter meaning that user has chosen to select all data for the bulk delete operation (or use separate controller action for bulk deletion). In this case actually we don't need to get the selected data from user, because we can simply get them from database (credits - Seng).
2) Increase number of displayed rows per page.
3) Use infinite scroll extension, for example this.
4) Break desired action in several iterations:
select needed rows on first page, do action (for example, delete).
repeat this again for other pages.
You can get selected rows like that:
$('#your-grid-view').yiiGridView('getSelectedRows');
[infinite scroll] : http://kop.github.io/yii2-scroll-pager/ will work good if you do not have any pjax filters. If you have filters also in play, do not use this plugin as it does not support pjax filters with it. For rest of the applications it is perfect to use.
Update1 : it seems to be straight forward than expected, here is the how I accomplished it
Add following lines to the checkbox column
'checkboxOptions' => function($data){
return ['id' => $data->id, 'onClick' => 'selectedRow(this)'];
}
Now add following JS to the common js file you will have in your project of the page where this datagrid resides
var selectedItems=[]; //global variable
/**
* Store the value of the selected row or delete it if it is unselected
*
* #param {checkbox} ele
*/
function selectedRow(ele){
if($(ele).is(':checked')) {
//push the element
if(!selectedItems.includes($(ele).attr('id'))) {
selectedItems.push($(ele).attr('id'));
}
} else {
//pop the element
if(selectedItems.includes($(ele).attr('id'))) {
selectedItems.pop($(ele).attr('id'));
}
}
}
Above function will store the selected row ids in the global variable array
Now add following lines to pjax:end event handler
$(document).on('pjax:end', function () {
//Select the already selected items on the grid view
if(!empty(selectedItems)){
$.each(selectedItems, function (index,value) {
$("#"+value).attr('checked',true);
});
}
});
Hope it helps.
I just solved this problem and it works properly with Pjax.
You may use my CheckboxColumn. I hope this can help. The checked items are recorded with cookies.
You can read the part with //add by hezll to understand how to fix it, because I didn't provide a complete general one.
Hope it works for you.
https://owncloud.xiwangkt.com/index.php/s/dGH3fezC5MGCx4H

yii efullcalendar widget and external events drag and drop not working

Hi I am trying to use drag and drop external events for fullcalendar and get working with the yii extension full calender - which seems to be just a wrapper.
The part that is not working (no errors just does not work) is dragging the external event onto the calendar and it staying there. It drags over but it just returns home.
Reading the fullcalendar docs - it looks like I need to provide a callback function to 'drop' attribute. I've been using the external-event example which is part of full calendar. I did discover the example was using the object name '#calendar' and that yii is creating name '#yw0' but it still didn't work after I updated.
I cannot find a way to get it work. I tried a simple alert which sort of works, it is called on page load - not after a drag operation.
So I declared a variable with the function in View
//$dropcallback=new CJavaScriptExpression("alert('hi')");
$dropcallback=new CJavaScriptExpression(
"function(date, allDay) {
var originalEventObject = $(this).data('eventObject');
var copiedEventObject = $.extend({}, originalEventObject);
copiedEventObject.start = date;
copiedEventObject.allDay = allDay;
$('#yw0').fullCalendar('renderEvent', copiedEventObject, true);
if ($('#drop-remove').is(':checked')) {
// if so, remove the element from the Draggable Events list
$(this).remove();
}
}
");
Then I create the widgit like this
$this->widget('ext.EFullCalendar.EFullCalendar', array(
'themeCssFile'=>'cupertino/jquery-ui.min.css',
'options'=>array(
'header'=>array(
'left'=>'prev,next',
'center'=>'title',
'right'=>'today'
),
'editable'=>true,
'dropable'=>true,
'drop'=>$dropcallback,
'events'=>Game::model()->gameCalendarData(),
)));
My yii experiance is little and same with JS - so any help appreciated in how to get this working.
I understand that in JS you need to provide a callback to allow the drag op to succeed. But what sort of call back do I need when it is wrapped in a yii widgit? I tried a PHP callback and again it is only called on page load.
The result I wish is that I can build the external events list from the DB - allow users to drag them onto the calendar - and save them in the DB.
I did manage to get data from DB displayed in the calendar.
Thanks
Droppable is spelt with two p's. So
'dropable'=>true,
Should be
'droppable'=>true,

Hidden Pagination summary text

I use CGridView in Yii to create tables.
I would like to show my table with pagination but hide the summary text at the top, which indicates the number of page restance (Displaying 1-4 of 4 results.)
Is that possible?
thank you
Sorry for my English
There is a template option. By default it equals {summary}\n{items}\n{pager}
If you override it in your gridview config, you'l be able to remove summary section:
$this->widget(
'zii.widgets.CGridView',
array(
Your options here ...
'template' => '{items}\n{pager}',
)
);
Another option is to set the CGridView summaryText value to false
Because YII uses CListView, it also add a certain summary class CSS in the asset folder. So to combat this, simply override the css.
Add this to your CSS.
#yw0>.summary{
display:none;
}

How to specify page size in dynamically in yii view?

How to specify page size in dynamically in yii view?(not a grid view)
In my custom view page there is a search option to find list of users between 2 date
i need 2 know how to specify the page size dynamically? If number of rows less than 10 how to hide pagination ?
please check screen-shoot
I dont want the page navigation after search it confusing users..
my controller code
$criteria=new CDbCriteria();
$count=CustomerPayments::model()->count($criteria);
$pages=new CPagination($count);
// results per page
$pages->pageSize=10;
$pages->applyLimit($criteria);
$paymnt_details=CustomerPayments::model()->findAll($criteria);
$this->render('renewal',array('paymnt_details'=>$paymnt_details,'report'=>$report,'branch'=>$branch,'model'=>$model,'pages' => $pages,));
}
my view
Link
I'm assuming you want the entire list of search result be shown on a single page, no-matter how long it is?
If so, this is quite easy to achive.
As I can see in your code, you are now defining your pageSize to have a size of 10. You can update it to be dynamically be doing this:
$pages->pageSize = $count;
To remove the pagesizes you can change the css of your view file, but for that we would need your view file to see how you defined it.

JUI Autocomplete html-encoded suggestions

jQuery UI starting from version 1.8.4 html-encodes Autocomplete suggestions (according to this issue).
This became a problem for me now. I used to theme the output for the suggestions, but now (if I use version 1.8.4 or higher) Autocomplete just html-encodes my theming. All tags like <b>, <span> are being printed to the user instead of displaying the actual styling.
So the suggestions now look like:
<b>su<b>suggestion
another <b>su<b>suggestion
instead of:
suggestion
another suggesion
I've read about custom data, but I use Yii framework and the output is being generated from certain actions (PHP code).
So, how do I theme the output now?
Thank you!
Better use a HTML plugin
You can use open function from jQuery UI to replace the encoded text.
Here's an example:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>"bug",
'source'=>$this->createUrl('/autocomplete'),
// additional javascript options for the autocomplete plugin
'options'=>array(
'open'=> 'js:function(event, ui){
$("ul.ui-autocomplete li a").each(function(){
var htmlString = $(this).html().replace(/</g, "<");
htmlString = htmlString.replace(/>/g, ">");
$(this).html(htmlString);
});
}'
),
));