如何在不提交btn的情况下将yii2中的输入值发送给控制器

时间:2018-06-20 22:21:29

标签: ajax yii2

我如何将输入值(日期选择器)发送到控制器,而不是操作。我有一些动作,需要更改日期值,我必须从datepicker的输入中获取日期值。我是yii2的新手,但我没意识到。我发现了有关通过ajax发送数据的信息,但是ajax仅通过一个动作发送数据(imho)。提前致谢。 (对不起,我的英语不好 )。目前,动作按date()起作用。

这是我的行动示例。

public function actionPara_foiz($fac, $cour, $smena, $lesson_id)
{
    if($cour == "")
    {
        if(Attend::find()->where(['faculty_id' => $fac, 'course_id' => 1, 'lesson_id' => $lesson_id, 'shift_id' => $smena, 'attend' => 0, 'date' => date('Y-m-d')])->count() != 0 && Student::find()->where(['faculty_id' => $fac, 'course_id' => 1, 'shift_id' => $smena])->count() != 0)
        {
            $course1['percent'] = (Attend::find()->where(['faculty_id' => $fac, 'course_id' => 1, 'lesson_id' => $lesson_id, 'shift_id' => $smena, 'attend' => 0, 'date' => date('Y-m-d')])->count() / Student::find()->where(['faculty_id' => $fac, 'course_id' => 1, 'shift_id' => $smena])->count()) * 100;
        }
        else
        {
            echo "";
        }
        if(Attend::find()->where(['faculty_id' => $fac, 'course_id' => 2, 'lesson_id' => $lesson_id, 'shift_id' => $smena, 'attend' => 0, 'date' => date('Y-m-d')])->count() != 0 && Student::find()->where(['faculty_id' => $fac, 'course_id' => 2, 'shift_id' => $smena])->count() != 0)
        {
            $course2['percent'] = (Attend::find()->where(['faculty_id' => $fac, 'course_id' => 2, 'lesson_id' => $lesson_id, 'shift_id' => $smena, 'attend' => 0, 'date' => date('Y-m-d')])->count() / Student::find()->where(['faculty_id' => $fac, 'course_id' => 2, 'shift_id' => $smena])->count()) * 100;
        }
        else
        {
            echo "";
        }
        if(Attend::find()->where(['faculty_id' => $fac, 'course_id' => 3, 'lesson_id' => $lesson_id, 'shift_id' => $smena, 'attend' => 0, 'date' => date('Y-m-d')])->count() != 0 && Student::find()->where(['faculty_id' => $fac, 'course_id' => 3, 'shift_id' => $smena])->count() != 0)
        {
            $course3['percent'] = (Attend::find()->where(['faculty_id' => $fac, 'course_id' => 3, 'lesson_id' => $lesson_id, 'shift_id' => $smena, 'attend' => 0, 'date' => date('Y-m-d')])->count() / Student::find()->where(['faculty_id' => $fac, 'course_id' => 3, 'shift_id' => $smena])->count()) * 100;
        }
        else
        {
            echo "";
        }
        if(Attend::find()->where(['faculty_id' => $fac, 'course_id' => 4, 'lesson_id' => $lesson_id, 'shift_id' => $smena, 'attend' => 0, 'date' => date('Y-m-d')])->count() != 0 && Student::find()->where(['faculty_id' => $fac, 'course_id' => 4, 'shift_id' => $smena])->count() != 0)
        {
            $course4['percent'] = (Attend::find()->where(['faculty_id' => $fac, 'course_id' => 4, 'lesson_id' => $lesson_id, 'shift_id' => $smena, 'attend' => 0, 'date' => date('Y-m-d')])->count() / Student::find()->where(['faculty_id' => $fac, 'course_id' => 4, 'shift_id' => $smena])->count()) * 100;
        }
        else
        {
            echo "";
        }

        $courses = [$course1, $course2, $course3, $course4];

        $childPers = array_column($courses, 'percent');

        if(count($childPers) != 0)
        {
            $parentItem['percent'] = array_sum($childPers)/(count($childPers));

            $sum_para1 = $parentItem['percent'];
        }
        else
        {
            echo "";
        }
    }
    else if($cour != "")
    {
        $para1 = Attend::find()->where(['faculty_id' => $fac, 'lesson_id' => $lesson_id, 'course_id' => $cour, 'shift_id' => $smena, 'attend' => 0, 'date' => date('Y-m-d')])->count();

        $fac_stud_count = Student::find()->where(['faculty_id' => $fac, 'course_id' => $cour, 'shift_id' => $smena])->count();

        if($fac_stud_count != 0)
        {
            $sum_para1 = $para1/$fac_stud_count *100;
        }
        else
        {
            echo "";
        }
    }
    else
    {
        echo "";
    }
    return $sum_para1;
}

这是教师(视图)

use kartik\date\DatePicker;
?>
<div class="faculty-index">

    <?php  Pjax::begin(['enablePushState' => true]); ?>
    <div class="col-md-4">
        <?= DatePicker::widget([
        'name' => 'date',
        'type' => DatePicker::TYPE_COMPONENT_APPEND,
        'value' => date("Y-m-d"),
        'removeButton' => false,
        'pluginOptions' => [
            'autoclose'=>true,
            'format' => 'yyyy-mm-dd'
        ]
        ]);?>
    </div>
    <div class="card">
        <div class="card-header" data-background-color="orange">
            <h4 class="title"><?= Html::encode($this->title) ?></h4>
        </div>
        <div class="col-md-6">
        <div class="card-content table-responsive">
            <table class="table table-hover">
                <thead class="text-warning">
                    <th colspan="12" style="text-align: center;">1-smena</th>
                </thead>
                <thead class="text-warning">
                    <th style="text-align: center;">Fakultet</th>
                    <th style="text-align: center;">Kurslar</th>
                    <th style="text-align: center;">Talabalar soni</th>
                    <th style="text-align: center;">1-para</th>
                    <th style="text-align: center;">Foizda</th>
                    <th style="text-align: center;">2-para</th>
                    <th style="text-align: center;">Foizda</th>
                    <th style="text-align: center;">3-para</th>
                    <th style="text-align: center;">Foizda</th>
                    <th style="text-align: center;">4-para</th>
                    <th style="text-align: center;">Foizda</th>
                    <th style="text-align: center;">Jami foizda</th>
                </thead>
                <tbody>
            <?= ListView::widget([
                            'dataProvider' => $dataProvider,
                            'itemView' => '_1smena',
                            'summary'=>'',
                ]) ?>
                </tbody>
            </table>
        </div>
    </div>

这是_1smena(_view)

<?php 
        $fac_stud_count = Student::find()->where(['faculty_id' => $model->id])->count();
        $smena = 1;
?>
    <tbody style="text-align: center;">

        <tr>
            <td rowspan="4"><a href="<?php echo Url::to(['a/c','id'=>$model->id]) ?>"><?= $model->name;?></a></td>
            <?php for ($i=1; $i <= 4; $i++) {?>
            <td><?= $i;?></td>
            <td><?php if(Yii::$app->runAction('attend/studentcount', ['fac' => $model->id, 'cour' => $i, 'smena' => $smena]) != 0) { echo Yii::$app->runAction('attend/studentcount', ['fac' =>  $model->id, 'cour' => $i, 'smena' => $smena]);} else { echo "";}?></td>
            <?php for ($j=1; $j <= 4; $j++) {?>
            <td><?php if(Yii::$app->runAction('attend/para', ['fac' => $model->id, 'cour' => $i, 'smena' => $smena, 'lesson_id' => $j]) != 0) { echo Yii::$app->runAction('attend/para', ['fac' =>  $model->id, 'cour' => $i, 'smena' => $smena, 'lesson_id' => $j]);} else { echo "";}?></td>
            <td id="per_id<?= $j?>_<?= $i;?>"><?php if(Yii::$app->runAction('attend/para_foiz', ['fac' => $model->id, 'cour' => $i, 'smena' => $smena, 'lesson_id' => $j]) != 0) { echo round(Yii::$app->runAction('attend/para_foiz', ['fac' =>  $model->id, 'cour' => $i, 'smena' => $smena, 'lesson_id' => $j]), 2)."%";} else { echo "";}?></td>
            <?php }?>
            <td id="res<?= $i;?>"></td>
        </tr>
        <?php }?>
        <tr> 
            <td colspan="2">Jami <?= $model->name?></td>
            <td><?php if(Yii::$app->runAction('attend/studentcount', ['fac' => $model->id, 'cour' => "", 'smena' => $smena]) != 0) { echo Yii::$app->runAction('attend/studentcount', ['fac' =>  $model->id, 'cour' => "", 'smena' => $smena]);} else { echo "";}?></td>
            <?php for ($j=1; $j <= 4; $j++) {?>
            <td><?php if(Yii::$app->runAction('attend/para', ['fac' => $model->id, 'cour' => "", 'smena' => $smena, 'lesson_id' => $j]) != 0) { echo Yii::$app->runAction('attend/para', ['fac' =>  $model->id, 'cour' => "", 'smena' => $smena, 'lesson_id' => $j]);} else { echo "";}?></td>
            <td id="id<?= $j?>"><?php if(Yii::$app->runAction('attend/para_foiz', ['fac' => $model->id, 'cour' => "", 'smena' => $smena, 'lesson_id' => $j]) != 0) { echo round(Yii::$app->runAction('attend/para_foiz', ['fac' =>  $model->id, 'cour' => "", 'smena' => $smena, 'lesson_id' => $j]), 2)."%";} else { echo "";}?></td>
            <?php }?>
            <td id="jami"></td>
            <script type="text/javascript">
                var jami1 = document.getElementById ("id1").textContent;
                var jami2 = document.getElementById ("id2").textContent;
                var jami3 = document.getElementById ("id3").textContent;
                var jami4 = document.getElementById ("id4").textContent;
                jami1 = jami1.slice(0, -1);
                jami2 = jami2.slice(0, -1);
                jami3 = jami3.slice(0, -1);
                jami4 = jami4.slice(0, -1);

                jami1 = [Number(jami1)];
                jami2 = [Number(jami2)];
                jami3 = [Number(jami3)];
                jami4 = [Number(jami4)];
                var all = [];

                if(jami1 > 0)
                {
                    all.push(jami1);
                }
                if(jami2 > 0)
                {
                    all.push(jami2);
                }
                if(jami3 > 0)
                {
                    all.push(jami3);
                }
                if(jami4 > 0)
                {
                    all.push(jami4);
                }
                var sum = 0;
                for (var i = 0; i < all.length; i++) {
                  sum += parseInt(all[i]);
                }
                var res = sum/all.length; 

                document.getElementById("jami").innerHTML = res.toFixed(2) + "%";
            </script>
        </tr>
    </tbody>

2 个答案:

答案 0 :(得分:0)

为什么不尝试使用jquery / ajax来获取设置日期时间选择器的时间,然后通过发布或获取请求将日期数据传递给控制器​​。

答案 1 :(得分:0)

我用queryParams解决了这个问题;

$params = Yii::$app->request->queryParams;
$params[date] // this given value when user request date

我不知道这是否是正确的决定,但是我使用了它。