我使用的是来自Kartik的Depdrop Widget,但是当它试图找到要填充下拉列表的数据时,该字段会继续加载。
我最近通过Composer更新了我的窗口小部件,并收到此错误(我不确定这是否是原因)。
视图中的字段:
<?= $form->field($model, 'IdCamion')->widget(DepDrop::classname(), [
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions' => [
'depends' => ['produccion-idcliente', 'produccion-tipotarifa'],
'placeholder' => 'Seleccione...',
'url' => Url::to(['/operaciones/camiones'])
]
]); ?>
以及填充下拉菜单的操作:
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' => '']);
}
我正在获取这样的数据(我输入的输出是更长的,但我将其切短以免变得乏味):
{"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}
期望以这种方式获取数据:
{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: ""
当您从父级下拉列表中选择另一个选项时,就会出现一个奇怪的部分,它将用数据正确填充下拉列表。
问题出在“ Tarifa”字段中的选项“ Viaje”,我不知道这是否是数据处理或数据大小方面的问题,但是使用其他选项,数据会更小并填充下拉列表正确。
编辑:我做了另一个测试:
我使用了另一个函数来填充下拉菜单中的问题:
public function PruebaDataDrop()
{
for ($i = 0; $i < 194; $i++) {
$data_lista[] = array('id' => $i, 'name' => $i);
}
return $data_lista;
}
它仅适用于Array(0-194)中的195个位置,当我尝试195个或更多位置时,它给我同样的问题(下拉列表不断加载)。
先谢谢您
答案 0 :(得分:1)
您应该使用
return Json::encode(['output' => $out, 'selected' => '']);
和
return Json::encode(['output' => '', 'selected' => '']);
那将解决您的问题。