My code on a custom delivery time module works all fine but it shows the given text outside the dropdown box , I racked my brain trying to fix this and I still couldn't , please do help!!
WorkingOff.php
<?php
namespace Shl\DeliveryTime\Block;
use Exception;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\Data\Form\Element\Factory;
use Shl\DeliveryTime\Helper\Data;
use Shl\DeliveryTime\Model\Api\Data\DeliveryTime;
class WorkingOff extends AbstractFieldArray
{
protected $_template = 'Shl_DeliveryTime::system/config/form/field/working-off.phtml';
protected $elementFactory;
public $_tslotList;
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('working_off', [
'label' => __('Day'),
'size' => __('40'),]);
$this->addColumn('tslot1', [
'label' => __('Time Slot'),
'size' => __('40'),
'renderer' => $this->getTslotList(),
]);
$this->_addAfter = false;
parent::_construct();
}
public function getTslotList()
{
$db =$this->db1('core_config_data');
$sql = "SELECT value FROM core_config_data where path='mpdeliverytime/general/delivery_time'";
$result = ($db['connection']->fetchAll($sql));
if (empty($result)) {
}
else{
$base = json_decode($result[0]['value'],true);
$output = array_chunk($base,1);
$from = array();
$bri = array();
foreach ($output as $key => $value) {
//$from[] = $value ['0'];
//foreach ($from as $key => $value) {
//for($i=0;$i<=3; $i++){
$timeArray[] = $value['0']['from']['0'].":".$value['0']['from']['1']."-".$value['0']['to']['0'].":".$value['0']['to']['1'] ;
}
}
}
public function db1($tbl_name){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName($tbl_name);
return $db = [
'connection' => $connection,
'tableName' => $tableName,
];
}
/**
* 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, ['working_off', 'tslot1'])) {
switch ($columnName) {
case 'working_off':
$element = $this->elementFactory->create('select');
$element->setForm($this->getForm())
->setName($this->_getCellInputElementName($columnName))
->setHtmlId($this->_getCellInputElementId('<%- _id %>', $columnName));
return str_replace("\n", "<option value=Sunday>Sunday</option><option value=Monday>Monday</option><option value=Tuesday>Tuesday</option><option value=Wednesday>Wednesday</option><option value=Thursday>Thursday</option><option value=Friday>Friday</option><option value=Saturday>Saturday</option>", $element->getElementHtml());
case 'tslot1':
$db =$this->db1('core_config_data');
$sql = "SELECT value FROM core_config_data where path='mpdeliverytime/general/delivery_time'";
$result = ($db['connection']->fetchAll($sql));
if (empty($result)) {
$count=1;
$timeArray[]=0;
$newstring="";
}
else{
$base = json_decode($result[0]['value'],true);
$output = array_chunk($base,1);
$from = array();
$option[] = array();
$c = array();
$count = 0;
$newstring="";
foreach ($output as $key => $value) {
//$c = $value[$key];
$timeArray[] = $value['0']['from']['0'].":".$value['0']['from']['1']."-".$value['0']['to']['0'].":".$value['0']['to']['1'] ;
$option[] = "<option value=$timeArray[$key]>$timeArray[$key]</option>";
$count++;
}
}
$element = $this->elementFactory->create('select');
$element->setForm($this->getForm())
->setName($this->_getCellInputElementName($columnName))
->setHtmlId($this->_getCellInputElementId('<%- _id %>', $columnName));
for($i = 0; $i < $count; $i++) {
$newstring .= "<option value=$timeArray[$i]>$timeArray[$i]</option>";}
return str_replace("\n", $newstring , $element->getElementHtml());
break;
default:
break;
}
}
return parent::renderCellTemplate($columnName);
}
/**
* @return string
*/
// public function getAddButtonLabel()
// {
// return $this->_addButtonLabel;
// }
}?>