So erstellen Sie ein Admin-Raster ohne DB-Sammlung in Magento - 2


7

Meine Anforderung besteht darin, ein Datenraster eines Webdienstes mit Such- und Sortierfunktionen anzuzeigen. Derzeit speichere ich alle Daten des Webdienstes in einer DB-Tabelle und zeige diese Werte im Admin-Grid mit dieser DB-Sammlung an.

In Magento-2 ist es möglich, ein Administrationsraster mit Filter- und Sortierfunktionen zu erstellen, ohne die DB-Sammlung zu verwenden.

Vielen Dank...


Klingt so, als hätten Sie bereits eine anständige Lösung gefunden. Wie oft werden Ihre Daten synchronisiert? Wenn Sie dies effizienter und häufiger erreichen können, ist dies eine solide Lösung.
Dominic Xigen

Antworten:


1

Ihre grid.phpDatei wie folgt:

<?php

namespace Vendor\Modulename\Block\Adminhtml\Report;

/**
 * Adminhtml report grid block
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 * @SuppressWarnings(PHPMD.DepthOfInheritance)
 */
class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{

    /**
     * StoreIds
     *
     * @var array
     */
    protected $_storeIds = [];


    /**
     * Resource collection factory
     *
     * @var \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory
     */
    protected $_resourceFactory;

    /**
     * Table alias
     *
     * @var string
     */
    protected $_tablealias = 'main_table';

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $resourceFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $resourceFactory,
        array $data = []
    ) {
        $this->_resourceFactory = $resourceFactory;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * Constructor
     *
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('orderbyGrid');
        $this->setFilterVisibility(false);
        $this->setDefaultSort('created_at');
    }

    /**
     * Collection
     *
     */
    protected function _prepareCollection()
    {
        //$collection = $this->_resourceFactory->create();

        $collection = 'your collection from web service';
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }


    /**
     * {@inheritdoc}
     *
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    protected function _prepareColumns()
    {
        $this->addColumn(
            'created_at',
            [
                'header' => __('Interval'),
                'index' => 'created_at',
                'sortable' => false,
                'filter'    => false,
                'period_type' => $this->getPeriodType(),
                'renderer' => 'Magento\Reports\Block\Adminhtml\Sales\Grid\Column\Renderer\Date',
                'totals_label' => __('Total'),
                'html_decorators' => ['nobr'],
                'header_css_class' => 'col-period',
                'column_css_class' => 'col-period'
            ]
        );

        $this->addColumn(
            'orders_count',
            [
                'header' => __('Orders'),
                'index' => 'orders_count',
                'type' => 'number',
                'total' => 'sum',
                'sortable' => false,
                'filter'    => false,
                'header_css_class' => 'col-orders',
                'column_css_class' => 'col-orders'
            ]
        );

        $this->addColumn(
            'total_qty_ordered',
            [
                'header' => __('Sales Items'),
                'index' => 'total_qty_ordered',
                'type' => 'number',
                'total' => 'sum',
                'sortable' => false,
                'filter'    => false,
                'header_css_class' => 'col-sales-items',
                'column_css_class' => 'col-sales-items'
            ]
        );

        if ($this->getFilterData()->getStoreIds()) {
            $this->setStoreIds(explode(',', $this->getFilterData()->getStoreIds()));
        }
        $currencyCode = $this->getCurrentCurrencyCode();
        $rate = $this->getRate($currencyCode);

        $this->addColumn(
            'total_base_price',
            [
                'header' => __('Sales Total'),
                'type' => 'currency',
                'currency_code' => $currencyCode,
                'index' => 'total_base_price',
                'total' => 'sum',
                'sortable' => false,
                'filter'    => false,
                'rate' => $rate,
                'header_css_class' => 'col-sales-total',
                'column_css_class' => 'col-sales-total'
            ]
        );

        $this->addColumn(
            'total_row_invoiced',
            [
                'header' => __('Invoiced'),
                'type' => 'currency',
                'currency_code' => $currencyCode,
                'index' => 'total_row_invoiced',
                'total' => 'sum',
                'sortable' => false,
                'filter'    => false,
                'rate' => $rate,
                'header_css_class' => 'col-invoiced',
                'column_css_class' => 'col-invoiced'
            ]
        );

        $this->addColumn(
            'total_price_incl_tax',
            [
                'header' => __('Paid'),
                'type' => 'currency',
                'currency_code' => $currencyCode,
                'index' => 'total_price_incl_tax',
                'total' => 'sum',
                'sortable' => false,
                'filter'    => false,
                'visibility_filter' => ['show_actual_columns'],
                'rate' => $rate,
                'header_css_class' => 'col-paid',
                'column_css_class' => 'col-paid'
            ]
        );

        $this->addColumn(
            'total_amount_refunded',
            [
                'header' => __('Refunded'),
                'type' => 'currency',
                'currency_code' => $currencyCode,
                'index' => 'total_amount_refunded',
                'total' => 'sum',
                'sortable' => false,
                'filter'    => false,
                'rate' => $rate,
                'header_css_class' => 'col-refunded',
                'column_css_class' => 'col-refunded'
            ]
        );

        $this->addColumn(
            'total_tax_amount',
            [
                'header' => __('Sales Tax'),
                'type' => 'currency',
                'currency_code' => $currencyCode,
                'index' => 'total_tax_amount',
                'total' => 'sum',
                'sortable' => false,
                'filter'    => false,
                'rate' => $rate,
                'header_css_class' => 'col-sales-tax',
                'column_css_class' => 'col-sales-tax'
            ]
        );

        $this->addColumn(
            'total_base_shipping_amount',
            [
                'header' => __('Sales Shipping'),
                'type' => 'currency',
                'currency_code' => $currencyCode,
                'index' => 'total_base_shipping_amount',
                'total' => 'sum',
                'sortable' => false,
                'filter'    => false,
                'rate' => $rate,
                'header_css_class' => 'col-sales-shipping',
                'column_css_class' => 'col-sales-shipping'
            ]
        );

        $this->addColumn(
            'total_discount_amount',
            [
                'header' => __('Sales Discount'),
                'type' => 'currency',
                'currency_code' => $currencyCode,
                'index' => 'total_discount_amount',
                'total' => 'sum',
                'sortable' => false,
                'filter'    => false,
                'rate' => $rate,
                'header_css_class' => 'col-sales-discount',
                'column_css_class' => 'col-sales-discount'
            ]
        );

        $this->addExportType('*/*/exportSalesCsv', __('CSV'));
        $this->addExportType('*/*/exportSalesExcel', __('Excel XML'));

        return parent::_prepareColumns();
    }

    /**
     * Get allowed store ids array intersected with selected scope in store switcher
     *
     * @return array
     */
    protected function _getStoreIds()
    {
        $filterData = $this->getFilterData();
        if ($filterData) {
            $storeIds = explode(',', $filterData->getData('store_ids'));
        } else {
            $storeIds = [];
        }
        // By default storeIds array contains only allowed stores
        $allowedStoreIds = array_keys($this->_storeManager->getStores());
        // And then array_intersect with post data for prevent unauthorized stores reports
        $storeIds = array_intersect($allowedStoreIds, $storeIds);
        // If selected all websites or unauthorized stores use only allowed
        if (empty($storeIds)) {
            $storeIds = $allowedStoreIds;
        }
        // reset array keys
        $storeIds = array_values($storeIds);

        return $storeIds;
    }

}

Ok, aber was würden Sie tun, um sie filtrierbar zu machen?
Jrosell
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.