9

In Magento 2's devdocs pages there is an example of time range picker. enter image description here But ,i can not find any examples of how to add it in my backend form for creating/editing of custom model. Have anyone any idea of how to do this?

Also ,i have a question on how to save it in mysql database. Do this picker needs 2 fields ("from" and "to") to store entered time?

3
  • Here is a link to how to add uiComponents devdocs.magento.com/guides/v2.3/ui_comp_guide/howto/… It is along same lines, create the attributes to save in db, create the xml to display.
    – djfordz
    Commented Dec 17, 2018 at 12:57
  • @jurgen did you ever find a solution for this one?
    – fmsthird
    Commented Apr 12, 2019 at 3:17
  • No, I no longer work with Magento2
    – jurgen
    Commented Apr 18, 2019 at 19:13

3 Answers 3

0

Try this,

Under app\code\Namespace\Module\Block\Adminhtml\Custom\Edit\Form.php

$fieldset->addField(
            'from',
            'time',
            [
                'name' => 'from',
                'label' => __('From'),
                'title' => __('From'),
                'required' => true,
                'note' => __('From Timer')
            ]
        );

$fieldset->addField(
            'to',
            'time',
            [
                'name' => 'to',
                'label' => __('To'),
                'title' => __('To'),
                'required' => true,
                'note' => __('To Timer')
            ]
        );

Create the field in database to save the to and from values.

In controller file,

public function saveData()
{
   $data = $this->getRequest()->getPostValue();
   $model = $this->_objectManager->create('Namespace\Module\Model\Custom');
   $model->setData($data);
   $model->save();
}
1
  • 1
    I've tried this earlier. This code adds two time fields positioned vertically. If I need 7 time range pickers (for each day of week) it looks very cumbersome. I wanted to place it at least in one line but much desirable to have it exact as on picture above. My opinion is that this time range picker exists only in Magento 2 design patterns and does not have implementation yet. For now I made it using single text field and Bootstrap Date Range Picker, customized it to show only time range. Result I save in DB in text format ("10:00-15:00" for example) which suits for my tasks.
    – jurgen
    Commented Jun 8, 2016 at 6:33
0

Here you go

class DeliveryTime extends AbstractFieldArray
{
    /**
     * @var string
     */
    /**
     * @var Factory
     */
    protected $elementFactory;

    /**
     * DeliveryTime constructor.
     *
     * @param Context $context
     * @param Factory $elementFactory
     * @param array $data
     */
    public function __construct(
        Context $context,
        Factory $elementFactory,
        array $data = []
    ) {
        $this->elementFactory = $elementFactory;

        parent::__construct($context, $data);
    }

    /**
     * Initialise form fields
     *
     * @return void
     */
    public function _construct()
    {
        $this->addColumn('from', ['label' => __('From')]);
        $this->addColumn('to', ['label' => __('To')]);
        $this->_addAfter = false;

        parent::_construct();
    }

    /**
     * Render array cell for prototypeJS template
     *
     * @param string $columnName
     *
     * @return mixed|string
     * @throws Exception
     */
    public function renderCellTemplate($columnName)
    {
        if (!empty($this->_columns[$columnName]) && in_array($columnName, ['from', 'to'])) {
            $element = $this->elementFactory->create('time');
            $element->setForm($this->getForm())
                ->setName($this->_getCellInputElementName($columnName))
                ->setHtmlId($this->_getCellInputElementId('<%- _id %>', $columnName));

            return str_replace("\n", '', $element->getElementHtml());
        }
         return parent::renderCellTemplate($columnName);
    }

    /**
     * @return string
     */
    public function getAddButtonLabel()
    {
        return $this->_addButtonLabel;
    }
}
0

you can use the below code in the Magento Ui form

<listing>
    <columns>
        <column name="period">
            <settings>
                <filter>dateRange</filter>
                <label translate="true">Period</label>
            </settings>
        </column>
    </columns>
</listing>

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.