Grid (Serializer Grid) skip one record while loading

I am loading a serializer grid with record but it skips the very first record it is returning array correct

Grid

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;
    }
}

Solutions

Found my solution I removed the

        $collection->addFieldToFilter('answer_id', ['nin' => $this->getRequest()->getParam('id')]);

from my code now its working fine

Tags: Magento 2.x / Grid / PHP 7

Similar questions

how to add order grid serializer in custom module
i want to create the Order Grid Serializer functionality. If Anyone create that type functionality then please guide me..
Grid Serializer issue in Magento
This issue find magento admin area grid serializer. When i selected row on grid. grid serializer hidden field not updating. But if i unchecked selected row that will changed the hidden field value. I just followed following examples http://inchoo.net/magento/magento-grid-serializer-ajax-grids/ http://magebase.com/magento-tutorials/understanding-the...
Magento 2.1 products grid serializer's values are not sending in form post
I am working on admin form where i have a product's grid in edit form.It shows products in grid but when i check products and save form data product's checked values are not posted along with other fields. Here is code. in ui component i have added this code in companyname\modulename\Block\Adminhtml\Faqs\AssignProducts and in company\module\Block\A...
Magento 2 : Product grid serializer issue
Inserted a product grid in my custom admin form using the below code: UPDATED view/adminhtml/layout/mymodule_form.xml Company/Mymodule/Block/Adminhtml/Productgrid/AssignProducts.php Company/Mymodule/Block/Adminhtml/Mymodule/Tab/Product.php web/templates/mymodule/category/edit/assign_products.phtml assign-products.js Now the product grid appears on ...
how to Store Values In Database for Custom Grid Serializer (Customer)?
I am Created Custom Module for Customer Grid Serializer and now I want to Store That Values in Diffrent Table. Can Any one Help me? Any Kind of help will appreiciated. My Grid Code : app/code/local/Sigmasolve/Recipe/Block/Adminhtml/Recipe/Edit/Tab
Clear GRID Serializer Values in hidden field
I have a working Grid Serializer. Now I want to clear the Grid Serializer values without refreshing page. So, I added a jquery script to clear the hidden field: It works fine when I am on the first page, but when I go to the next page and clear the hidden Grid Serializer it will not clear for the previous page. I can able to see that the Grid Seria...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.