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?

Amit Bera
  • 77,456
  • 20
  • 123
  • 237
jurgen
  • 395
  • 5
  • 13
  • Here is a link to how to add uiComponents https://devdocs.magento.com/guides/v2.3/ui_comp_guide/howto/add_category_attribute.html

    It is along same lines, create the attributes to save in db, create the xml to display.

    – djfordz Dec 17 '18 at 12:57
  • @jurgen did you ever find a solution for this one? – fmsthird Apr 12 '19 at 03:17
  • No, I no longer work with Magento2 – jurgen Apr 18 '19 at 19:13

3 Answers3

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();
}
saravanavelu
  • 3,941
  • 23
  • 35
  • 51
  • 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 Jun 08 '16 at 06: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

mjsachan
  • 11
  • 2