I have a cgridview who allows multiple row selection. On selectionChanged I do an ajax call which do some calculation with the selected rows and put the result in a hidden field. The problem is that my cgridview loses the selection when I do an ajax call even if the ajax call don’t do anything with the grid. How can i keep the selection in the grid?
Keith:
Try this extension.
Is there a simple way to resolve my problem without using an extension?
Keith
(Kburton)
September 30, 2014, 6:02pm
4
Not a simple way. I’ve done something like the following in previous projects:
<? $this->widget('zii.grid.CGridView', array(
...
'beforeAjaxUpdate'=>'itemGridBeforeAjaxUpdate',
'afterAjaxUpdate'=>'itemGridAfterAjaxUpdate',
...
)); ?>
<script type="text/javascript">
var itemGridSelection = [];
function itemGridBeforeAjaxUpdate(id, options)
{
itemGridSelection = $('#itemGridView').yiiGridView('getSelection');
}
function itemGridAfterAjaxUpdate(id, data)
{
for (i = 0; i < itemGridSelection.length; i++)
$('#itemGridView tbody :checkbox[value=' + itemGridSelection[i] + ']').attr('checked', true);
}
</script>
I can’t remember how well it worked, but it might give you a starting point.
Keith:
Not a simple way. I’ve done something like the following in previous projects:
<? $this->widget('zii.grid.CGridView', array(
...
'beforeAjaxUpdate'=>'itemGridBeforeAjaxUpdate',
'afterAjaxUpdate'=>'itemGridAfterAjaxUpdate',
...
)); ?>
<script type="text/javascript">
var itemGridSelection = [];
function itemGridBeforeAjaxUpdate(id, options)
{
itemGridSelection = $('#itemGridView').yiiGridView('getSelection');
}
function itemGridAfterAjaxUpdate(id, data)
{
for (i = 0; i < itemGridSelection.length; i++)
$('#itemGridView tbody :checkbox[value=' + itemGridSelection[i] + ']').attr('checked', true);
}
</script>
I can’t remember how well it worked, but it might give you a starting point.
I wish I could understand why I lose selection when i don’t renderPartial the grid or anything like that, any ideas?
I don’t use checkbox, but maybe i’ll have to so i can implement your suggestion. Thanks for the help
Keith:
Not a simple way. I’ve done something like the following in previous projects:
<? $this->widget('zii.grid.CGridView', array(
...
'beforeAjaxUpdate'=>'itemGridBeforeAjaxUpdate',
'afterAjaxUpdate'=>'itemGridAfterAjaxUpdate',
...
)); ?>
<script type="text/javascript">
var itemGridSelection = [];
function itemGridBeforeAjaxUpdate(id, options)
{
itemGridSelection = $('#itemGridView').yiiGridView('getSelection');
}
function itemGridAfterAjaxUpdate(id, data)
{
for (i = 0; i < itemGridSelection.length; i++)
$('#itemGridView tbody :checkbox[value=' + itemGridSelection[i] + ']').attr('checked', true);
}
</script>
I can’t remember how well it worked, but it might give you a starting point.
Your solution didn’t work for me, beforeAjaxUpdate and afterAjaxUpdate are never call because i don’t have an ajax update at the grid
Keith
(Kburton)
October 1, 2014, 12:25pm
7
Can you show the code you’re using to do the AJAX call?
In my grid I have
'selectionChanged' => "function(id)
{
console.log('Changed');
selecoesAlteradas();
}",
and selecoesAlteradas I have
$.ajax(
{
url: '/compromissoFinanceiro/atualizarGridRenegociacao',
type: 'POST',
dataType: 'json',
data: {idsCompromissosRenegociados: idsCompromissosRenegociados},
}).done(function(retorno)
{
console.log(retorno);
})
.fail(function()
{
alert("Erro ao atualizar o grid de compromissos renegociados");
})
If i comment the ajax call the grid does not lose selection. If I do any ajax call, doesn’t matter which, the grid loses selection
Keith
(Kburton)
October 1, 2014, 1:58pm
9
Your selection shouldn’t be changing unless you’re refreshing the grid.
Try refactoring your selectionChanged code into a function and see if that helps:
'selectionChanged' => 'gridSelectionChanged', // <- no brackets after the function name
<script type="text/javascript">
function gridSelectionChanged(id)
{
console.log('Changed');
selecoesAlteradas();
}
function selecoesAlteradas()
{
...
}
</script>
Found the problem, it was an internal script from our company who was removing the selection after every ajax request.
Thanks for all the help man