Yii2-如何获取下拉列表的ID

时间:2018-07-26 07:07:26

标签: jquery yii2-advanced-app

我有三个依赖的下拉列表,如下所示: Dropdownlist

我想获取每个下拉列表的ID。最初,导入是隐藏的。然后,当单击最后一个下拉列表(程序)时,将显示要导入的表单。

Dropdownlist2

从属下拉列表的ID将用于

            $progMaster->state_office_id = $this->valueReplace($line['A'], Programme::getDepartmentStateOffice()); 
            $progMaster->study_centre_id = $this->valueReplace($line['B'], Programme::getDepartmentStudyCentre()); 
            $progMaster->programme_id = $this->valueReplace($line['C'], Programme::getDepartmentProgramme());

控制器

    public function actionIndex()
{
    $model = new Department();      
    $model->scenario = 'import-department';
    $importResults = [];

    if ($model->load(Yii::$app->request->post())) {         
        $model->importFile = UploadedFile::getInstance($model, 'importFile');
        if($model->saveImportFile()) {
            $importResults = $this->importDepartmentData($model);
        }           
    }

    return $this->render('index', [
        'model' => $model,
        'importResults' => $importResults,
    ]);
}

public function importDepartmentData($model)
{              
    $dispResults = []; 
    $totalSuccess = 0;

    $objPHPExcel = PHPExcel_IOFactory::load($model->importFilePath.$model->importFile);
    $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);

    unset($sheetData[1]);

    foreach($sheetData as $k => $line){
        if(!array_filter($line))
            continue;

        $line = array_map('trim', $line);
        $line = array_map(function($value) { return empty($value) ? NULL : $value; }, $line);

        $progMaster = new StudyCentre();

        //set learner info attributes

        $progMaster->state_office_id = $this->valueReplace($line['A'], Programme::getDepartmentStateOffice()); 
                    $progMaster->study_centre_id = $this->valueReplace($line['B'], Programme::getDepartmentStudyCentre()); 
                    $progMaster->programme_id = $this->valueReplace($line['C'], Programme::getDepartmentProgramme());                     

        $progMaster->created_by = Yii::$app->getid->getId();
        $progMaster->created_at = new \yii\db\Expression('NOW()');

        if($progMaster->save())
                    {
                        $totalSuccess+=1;
                        $dispResults[] = array_merge($line, ['type' => 'S', 'departmentId' => $progMaster->id, 'message' => 'Success']);
        }
                    else
                    {
                        $dispResults[] = array_merge($line, ['type' => 'E', 'message' => Html::errorSummary($progMaster)]);                         
        }   
    } //end foreach
    return ['dispResults' => $dispResults, 'totalSuccess' => $totalSuccess];
}

查看

<?php $form = ActiveForm::begin([
'id' => 'import-department',
'enableAjaxValidation' => false,
'options' => ['enctype' => 'multipart/form-data'],
]); ?>

        <div class="col-xs-12 col-lg-12 no-padding">
                <div class="col-xs-12 col-sm-4 col-lg-4">
                    <?= $form->field($model, 'state_office_id')->widget(Select2::classname(), [
                        'data' => ArrayHelper::map(\common\models\StateOffice::find()->where(['is_status' => 0])->all(),'id','state_name'),
                        'language' => 'en',
                        'options' => ['placeholder' => '--- Select State Office ---', 
                            'onchange'=>'
                                $.get( "'.Url::toRoute('dependent/getstudycentre').'", { id: $(this).val() } )
                                    .done(function( data ) {
                                        $( "#'.Html::getInputId($model, 'study_centre_id').'" ).html( data );
                                    }
                                );' 
                        ],
                        'pluginOptions' => [
                            'allowClear' => true
                        ],
                    ]); ?>                                                        
                </div>
                <div class="col-xs-12 col-sm-4 col-lg-4">
                    <?= $form->field($model, 'study_centre_id')->widget(Select2::classname(), [
                        'data' => [],
                        'language' => 'en',
                        'options' => ['placeholder' => '--- Select Study Centre ---', 
                            'onchange'=>'
                                $.get( "'.Url::toRoute('dependent/getprogramme').'", { id: $(this).val() } )
                                    .done(function( data ) {
                                        $( "#'.Html::getInputId($model, 'programme_id').'" ).html( data );
                                    }
                                );' 
                        ],
                        'pluginOptions' => [
                            'allowClear' => true
                        ],
                    ]); ?>                         
                </div>
                <div class="col-xs-12 col-sm-4 col-lg-4">
                    <?= $form->field($model, 'programme_id')->widget(Select2::classname(), [
                         'data' => [],
                         'language' => 'en',
                         'options' => ['placeholder' => 'Select Programme ...'],
                         'pluginOptions' => [
                             'allowClear' => true
                         ],
                     ]); ?>                          
                </div>                    
    </div> 

    <div class="box box-primary">
<div class="box-header with-border">
    <h3 class="box-title"><i class="fa fa-file-excel-o"></i> <?php echo Yii::t('app', 'Select File'); ?></h3>
</div><!--./box-header-->
<div class="box-body">
    <div class="row">
        <div class="col-sm-12 col-xs-12">
            <?= $form->field($model, 'importFile')->fileInput(['title' => Yii::t('app', 'Browse Excel File')])->label(false) ?>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12 col-xs-12">
            <div class="callout callout-info">
                <h4><?php echo Yii::t('app', 'You must have to follow the following instruction at the time of importing data'); ?></h4>
                <ol>
                    <li><b><?php echo Yii::t('app', 'The field with red color are the required field cannot be blank.'); ?></b></li>
                    <li><?php echo Yii::t('app', 'The file must be CSV format.'); ?></li>
                </ol>
                <h5><?php echo Yii::t('app', 'Download the sample format of Excel sheet.'); ?> <b><?= Html::a(Yii::t('app', 'Download'), ['download-file', 'id' => 'SSF']) ?></b></h5>
            </div><!--./callout-->
        </div><!--./col-->
    </div><!--./row-->
</div><!--./box-body-->
<div class="box-footer">
    <?= Html::submitButton('<i class="fa fa-upload"></i>'.Yii::t('app', ' Import'), ['class' => 'btn btn-primary']) ?>
</div>

<?php if(!empty($importResults)) : ?>
<div class="box box-info">
<div class="box-header">
    <h3 class="box-title"><i class="fa fa-list-ul"></i> <?php echo Yii::t('app', 'Department Import Results'); ?></h3>
</div>
<div class="box-body">
    <div class="row">
        <div class="col-sm-12">
            <?php $totalError = (count($importResults['dispResults'])-$importResults['totalSuccess']); ?>
            <?php $headerTr = $content = ''; $i = 1; ?>

            <?php if(!empty($importResults['totalSuccess'])) : ?>
                <div class="alert alert-success">
                    <h4><i class="fa fa-check"></i> <?php echo Yii::t('app', 'Success!'); ?></h4>
                    <?= "{$importResults['totalSuccess']}". Yii::t('app', ' department importing successfully.') ?>
                </div>
            <?php endif; ?>

            <?php if(!empty($totalError)) : ?>
                <div class="alert alert-danger">
                    <h4><i class="fa fa-ban"></i> <?php echo Yii::t('app', 'Error!'); ?></h4>
                    <?= "{$totalError}". Yii::t('app', ' department importing error.') ?>
                </div>
            <?php endif; ?>
            <?php
                $headerTr.= Html::tag('th', Yii::t('app', 'Sr No'));
                $headerTr.= Html::tag('th', Yii::t('app', 'State Office'));
                $headerTr.= Html::tag('th', Yii::t('app', 'Study Centre'));
                                    $headerTr.= Html::tag('th', Yii::t('app', 'Programme'));

                $headerTr.= Html::tag('th', Yii::t('app', 'Status'));
                $headerTr.= Html::tag('th', Yii::t('app', 'Remarks/Message'));
            ?>
            <table class="table table-striped table-bordered" id = "fixHeader">
                <thead>
                    <?= Html::tag('tr', $headerTr, ['class' => 'active']) ?>
                </thead>
                <tbody>

                <?php foreach($importResults['dispResults'] as $line) :                         
                    $content = '';
                    $content.= Html::tag('td', $i++);
                    $content.= Html::tag('td', $line['A']); //State Office
                    $content.= Html::tag('td', $line['B']); //Study Centre
                                            $content.= Html::tag('td', $line['C']); //Programme

                    $content.= Html::tag('td', ($line['type'] == 'S') ? Yii::t('app', 'ERROR') : Yii::t('app', 'SUCCESS'));
                    $content.= Html::tag('td', ($line['type'] == 'S') ? $line['message'] : Html::a(Yii::t('app', 'View Profile'), ['department/view', 'id' => $line['centreId']], ['class' => 'btn btn-sm btn-info', 'target' => '_BLANK'])); //Remarks/Message

                    echo Html::tag('tr', $content, ['class' => ($line['type'] == 'S') ? 'danger' : 'success']); 
                    ?>  
                <?php endforeach; ?> 
                </tbody>
            </table>
        </div>
    </div>
</div><!--./box-body-->

这些是我的问题:

  1. 如何获取每个Dropdownlist的ID

  2. 如何隐藏文件上传,直到单击最后一个下拉列表

0 个答案:

没有答案