Search code examples
phpyii2widgetdropdown

Yii2: The Depdrop widget form Kartik keeps loading


I am using the Depdrop Widget from Kartik, but a field keeps loading when it tries to find the data to populate the dropdown.

Field not loading...

I recently updated my widgets via Composer and got this error (i am not sure if this is the cause).

The field in the view:

        <?= $form->field($model, 'IdCamion')->widget(DepDrop::classname(), [
            'type' => DepDrop::TYPE_SELECT2,
            'pluginOptions' => [
                'depends' => ['produccion-idcliente', 'produccion-tipotarifa'],
                'placeholder' => 'Seleccione...',
                'url' => Url::to(['/operaciones/camiones'])
            ]
        ]); ?>

and the action to populate the dropdown:

public function actionCamiones()
{
    $out = [];
    if (isset($_POST['depdrop_parents'])) {

        $parents = $_POST['depdrop_parents'];
        $data_lista = array();
        if ($parents != null) {
            $cliente = $parents[0];
            $tarifa = $parents[1];
            $array = Tarifa::find()->where(['IdCliente' => $cliente])->andWhere(['TipoTarifa' => $tarifa])
                ->andWhere(['eliminado' => 0])->andWhere(['estado' => 5])->groupby('TamanoCamion')->all();
            foreach ($array as $key => $value) {
                $camiones = Camiones::find()->where(['Tamano' => $value->TamanoCamion])->andwhere('Estatus = 1 or Estatus = 18')->all();
                foreach ($camiones as $key => $camion) {
                    $data_lista[] = array('id' => $camion->NumeroCamion, 'name' => $camion->NumeroCamion);
                }
            }
            $out = $data_lista;
            echo Json::encode(['output' => $out, 'selected' => '']);
            return;
        }
    }
    echo Json::encode(['output' => '', 'selected' => '']);
}

i am getting the data like this (the output I'm putting is longer but i cut it short to not make it tedious):

{"output":[{"id":"124","name":"124"},{"id":"161","name":"161"},        
{"id":"163","name":"163"},{"id":"125","name":"125"}, 
{"id":"112","name":"112"},{"id":"113","name":"113"}, 
{"id":"114","name":"114"},{"id":"115","name":"115"}, 
{"id":"492","name":"492"},{"id":"493","name":"493"}],"selected":""}
An Error occurred while handling another error:
yii\web\HeadersAlreadySentException: Headers already sent in 
/var/www/html/sat2-panama/controllers/OperacionesController.php on line 
1618. in /var/www/html/sat2- 
panama/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /var/www/html/sat2-panama/vendor/yiisoft/yii2/web/Response.php(339): 
yii\web\Response->sendHeaders()
#1 /var/www/html/sat2- 
panama/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response- 
>send()
#2 /var/www/html/sat2- 
panama/vendor/yiisoft/yii2/base/ErrorHandler.php(111): 
yii\web\ErrorHandler- 
>renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler- 
>handleException(Object(yii\web\HeadersAlreadySentException))
#4 {main}
Previous exception:
yii\web\HeadersAlreadySentException: Headers already sent in 
/var/www/html/sat2-panama/controllers/OperacionesController.php on line 
1618. in /var/www/html/sat2- 
panama/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /var/www/html/sat2-panama/vendor/yiisoft/yii2/web/Response.php(339): 
yii\web\Response->sendHeaders()
#1 /var/www/html/sat2- 
panama/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response- 
>send()
#2 /var/www/html/sat2-panama/web/index.php(12): yii\base\Application- 
>run()
#3 {main}

It is expected to have the data in this way:

{output: [{id: "124", name: "124"}, {id: "161", name: "161"}, {id: "163", 
name: "163"},…],…}
output: [{id: "124", name: "124"}, {id: "161", name: "161"}, {id: "163", 
name: "163"},…]
selected: ""

The weird part comes when you select another option from the parents dropdowns, it populates the dropdown correctly with the data.

Field populated.

The problem is with the option "Viaje" in the field "Tarifa", i don't know if this is a problem with data handling or data size, but with the other options, the data is smaller and populate the dropdown correctly.

EDIT: i did another test:

i used another function to populate the dropdown with the problem:

public function PruebaDataDrop()
 {
    for ($i = 0; $i < 194; $i++) {
        $data_lista[] = array('id' => $i, 'name' => $i);
    }
    return $data_lista;
}

it works only with 195 positions in the Array (0-194), when i tried 195 or more, it gives me the same problem (the dropdown keeps loading).

Thankyou in advance.


Solution

  • You should use

    return Json::encode(['output' => $out, 'selected' => '']); 
    

    and

    return Json::encode(['output' => '', 'selected' => '']);
    

    That will solve your issue.