Yii2 - 将子数据保留在uclead表格输入中从父级依赖下拉列表中选择选项中



我做了一个表格输入,我将一次将多条记录输入到数据库的一个表中。 我使用此扩展名:https://github.com/unclead/yii2-multiple-input/wiki/Usage#tabular-input 从这个扩展中,我将其与依赖下拉列表相结合。 这是代码。

public function actionCreateMulti() {
$request = Yii::$app->request;
$models = [new MaterialLocations()];
if ($request->isAjax) {
/**
*   Process for ajax request
*/
Yii::$app->response->format = Response::FORMAT_JSON;
if ($request->isGet) {
return [
'title' => "Create new MaterialLocations",
'content' => $this->renderAjax('_form_create_multi', [
'models' => $models,
]),
'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
Html::button('Order', ['class' => 'btn btn-primary', 'type' => "submit"])
];
} else if ($eglData = $request->post('MaterialLocations', [])) {
foreach (array_keys($eglData) as $index) {
$models[$index] = new MaterialLocations();
}
if (Model::loadMultiple($models, $request->post()) && ActiveForm::validateMultiple($models)) { // if failed
return [
'title' => "Create new MaterialLocations",
'content' => $this->renderAjax('_form_create_multi', [
'models' => $models,
]),
'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
Html::button('Order', ['class' => 'btn btn-primary', 'type' => "submit"])
];
} else { // success
foreach ($models as $single) {
$single->save(false);
}
return [
'forceReload' => '#crud-datatable-pjax',
'title' => "Create new MaterialLocations",
'content' => '<span class="text-success">Create MaterialLocations success</span>',
'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
Html::a('Create More', ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
];
}
} else {
return [
'title' => "Create new MaterialLocations",
'content' => '<pre>' . VarDumper::dumpAsString($request->post()) . '</pre>',
'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
Html::button('Order', ['class' => 'btn btn-primary', 'type' => "submit"])
];
}
} else {
/**
*   Process for non-ajax request
*/
new ForbiddenHttpException(); // just for test
}
}

以下是表格输入的视图。

<?php $form = ActiveForm::begin(); ?>
<?php try {
echo TabularInput::widget([
'id' => 'some-id',
'models' => $models,
'cloneButton' => true,
'addButtonPosition' => MultipleInput::POS_FOOTER,
'sortable' => true,
'columns' => [
[
'name' => 'cupboard_id',
'title' => $models[0]->getAttributeLabel('cupboard_id'),
'enableError' => true,
'type' => Select2::className(),
'options' => [
'data' => $cupBoards,
'options' => [
'prompt' => '== Choose a vendor ==',
'onChange' => $jsOnChange, // this is handle onChange to find list row
'class' => 'form-control cupboard-id',
]
],
],
[
'name' => 'row',
'title' => $models[0]->getAttributeLabel('row'),
'type' => 'dropDownList',
'items' => [],
'value' => function ($data) {
return $data->row;
},
'options' => [
'class' => 'form-control row-cupboard',
],
'columnOptions' => [
'style' => 'width: 100px;',
]
],
],
]);
} catch (Exception $e) {
echo $e->getMessage();
} ?>
<?php ActiveForm::end(); ?>

如您所见,第一个输入列是:"cupboard_id", 如果有onChange,它将为"行"列生成数据。

<?php
$urlOnChangeCupboard = Url::toRoute(['cupboards/find-row-column-available']);
$jsOnChange = <<<JS
var cupboard = jQuery(this);
var row = cupboard.closest('tr');
var rowCupboard = row.find('.row-cupboard');
var columnCupboard = row.find('.column-cupboard');
jQuery.post('$urlOnChangeCupboard', { id : cupboard.val() }, function(response){
rowCupboard.find('option').remove().end();
columnCupboard.find('option').remove().end();
jQuery.each(response.data.rows, function(index, value){
rowCupboard.append('<option value=' + value + '>' + value + '</option>'); 
});
});
JS;
?>
<?php
function callBack($cupboardID) {
$data = Cupboards::find()->where(['id' => $cupboardID])->one();
$rowsRaw = range($data->rows_start, $data->rows_amount);
$rows = array_combine($rowsRaw, $rowsRaw);
return $rows;
}
?>

问题是,当开机自检(提交(数据,然后验证数据时,仍然存在错误, 列行中的数据仍应基于橱柜选择。

此时,发布后,来自"行"的数据为空 请帮忙。

使用列选项关闭修复

[
'name' => 'row',
'title' => $models[0]->getAttributeLabel('row'),
'type' => 'dropDownList',
'items' => function ($model) {
$data = [];
if (isset($model->row)) {
$cupBoards = Cupboards::find()->where(['id' => $model->cupboard_id])->one();
$rowsRaw = range($cupBoards->rows_start, $cupBoards->rows_amount);
$data = array_combine($rowsRaw, $rowsRaw);
}
return $data;
},
'value' => function ($data) {
return $data->row;
},
'options' => [
'class' => 'form-control row-cupboard',
],
'columnOptions' => [
'style' => 'width: 100px;',
]
],

相关内容

  • 没有找到相关文章

最新更新