首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不提交yii2的情况下将输入值发送给控制器

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

Stack Overflow用户
提问于 2018-06-20 22:21:28
回答 2查看 465关注 0票数 0

如何将输入值(数据报警器)发送给控制器,而不是操作。我有一些动作,需要更改日期的值,我必须从datepicker的输入中得到日期的值。我是yii2的新手,我无法意识到这一点。我发现了用ajax发送数据的方法,但是ajax只发送了一个操作(Imho)。提前谢谢。(对不起,我的英语很差)。目前,行动按日期()生效。

这是我行动的例子。

代码语言:javascript
复制
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;
}

这是教员(见)

代码语言:javascript
复制
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)

代码语言:javascript
复制
<?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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-09 18:58:10

我用queryParams解决了这个问题;

代码语言:javascript
复制
$params = Yii::$app->request->queryParams;
$params[date] // this given value when user request date

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

票数 0
EN

Stack Overflow用户

发布于 2018-06-23 08:06:54

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50957876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档