Grid (Serializer Grid) skip one record while loading
-
19-02-2021 - |
Question
I am loading a serializer grid with record but it skips the very first record it is returning array correct
Code for my grid is working like this ->it loads the answer related to a question. ->the helper function returns an array related to a question the grid is suppose to load those answers in it. ->I have tried other ids it is working for other but when i pass id=1 helper data returns correct array but only one record is loaded but not the other one.
i.e for id=1
it returns =[1][2]
but grid only loads the [2] and skips [1]
My Grid Code is
namespace Vendor\NameSpace\Block\Adminhtml\Answer\Tab;
use Magento\Backend\Block\Widget\Grid;
use Magento\Backend\Block\Widget\Grid\Column;
use Magento\Backend\Block\Widget\Grid\Extended;
use Magento\Framework\App\ObjectManager;
use Vendor\NameSpace\Model\Topics\Attribute\Source\Status;
class Answer extends \Magento\Backend\Block\Widget\Grid\Extended
{
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;
protected $_answerFactory;
protected $_HelperData;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Backend\Helper\Data $backendHelper,
\Vendor\NameSpace\Model\AnswerFactory $answerFactory,
\Magento\Framework\Registry $coreRegistry,
\Vendor\NameSpace\Helper\HelperData $HelperData,
\Vendor\NameSpace\Model\ResourceModel\Answer $resourceAnswer,
Status $status = null,
array $data = []
) {
$this->_answerFactory = $answerFactory;
$this->_coreRegistry = $coreRegistry;
$this->resourceAnswer = $resourceAnswer;
$this->_HelperData = $HelperData;
$this->status = $status ?: ObjectManager::getInstance()->get(Status::class);
parent::__construct($context, $backendHelper, $data);
}
/**
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->setId('answer_related_answer');
$this->setDefaultSort('answer_id');
$this->setUseAjax(true);
}
/**
* @return array|null
*/
/**
* @param Column $column
* @return $this
*/
protected function _addColumnFilterToCollection($column)
{
if($column->getId() == 'in_answer_related_answer')
{
$answerIds = $this->_getSelectedAnswer();
if (empty($answerIds)) {
$answerIds = 0;
}
if ($column->getFilter()->getValue()) {
$this->getCollection()->addFieldToFilter('answer_id', ['in' => $answerIds]);
$this->getCollection()->addFieldToFilter('answer_id', ['nin' => $this->getRequest()->getParam('id')]);
} elseif (!empty($answerIds)) {
$this->getCollection()->addFieldToFilter('answer_id', ['nin' => $answerIds]);
$this->getCollection()->addFieldToFilter('answer_id', ['nin' => $this->getRequest()->getParam('id')]);
}
}
else
{
parent::_addColumnFilterToCollection($column);
}
return $this;
}
/**
* @return Grid
*/
protected function _prepareCollection()
{
$collection = $this->_answerFactory->create()->getCollection();
$collection->addFieldToFilter('answer_id', ['nin' => $this->getRequest()->getParam('id')]);
$this->setCollection($collection);
return parent::_prepareCollection();
}
/**
* @return Extended
*/
protected function _prepareColumns()
{
if ($this->getRequest()->getParam('id')) {
$this->setDefaultFilter(['in_answer_related_answer' => 1]);
}
$this->addColumn(
'in_answer_related_answer',
[
'header' => __('ID'),
'type' => 'hidden',
'name' => 'in_answer_related_answer',
'values' => $this->_getSelectedAnswer(),
'index' => 'answer_id',
'header_css_class' => 'col-select col-massaction',
'column_css_class' => 'col-select col-massaction'
]
);
// $this->addColumn(
// 'answer_id',
// [
// 'header' => __('ID'),
// 'sortable' => true,
// 'index' => 'answer_id',
// 'header_css_class' => 'col-id',
// 'column_css_class' => 'col-id'
// ]
// );
$this->addColumn(
'answer',
[
'header' => __('Answer'),
'index' => 'answer'
]
);
$this->addColumn(
'answer_likes',
[
'header' => __('Answer Likes'),
'index' => 'answer_likes'
]
);
$this->addColumn(
'answer_dislikes',
[
'header' => __('Answer Dislikes'),
'index' => 'answer_dislikes'
]
);
$this->addColumn(
'answer_author',
[
'header' => __('Answer Author'),
'index' => 'answer_author'
]
);
$this->addColumn(
'answer_status',
[
'header' => __('Status'),
'index' => 'answer_status',
'type' => 'options',
'options' => $this->status->getOptionArray()
]
);
$this->addColumn(
'action', [
'header' => __('Action'),
'type' => 'action',
'getter' => 'getId',
'actions' => [
[
'caption' => __('Edit'),
'url' => ['base' => 'faq/answer/edit'],
'field' => 'id',
],
[
'caption' => __('Delete'),
'url' => ['base' => 'faq/answer/delete'],
'field' => 'id',
],
],
'filter' => false,
'sortable' => false,
'index' => 'stores',
'header_css_class' => 'col-action',
'column_css_class' => 'col-action',
]
);
return parent::_prepareColumns();
}
/**
* @return string
*/
public function getGridUrl()
{
return $this->getUrl('faq/answer/answergrid', ['_current' => true]);
}
/**
* @return array
*/
protected function _getSelectedAnswer()
{
$answer = $this->getRequest()->getPost('selected_answer');
if ($answer === null)
{
return $answer = $this->_HelperData->getRelatedAnswerArray($this->getRequest()->getParam('id'));
}
return $answer;
}
}
Solution
Found my solution I removed the
$collection->addFieldToFilter('answer_id', ['nin' => $this->getRequest()->getParam('id')]);
from my code now its working fine
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange