javascript - yii2 文本框中 gridview 列的总和

标签 javascript yii2

我想在文本框中显示 gridview 列的总和。当页面加载时,总和应该传递到文本框。 我在我的index.php 文件中编写了以下JavaScript 代码。但没有得到总数。 JavaScript 代码可能不正确。请让我知道该怎么做 -

index.php-

<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\sgledger\models\SellitemsgSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Sunglass Ledger';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="sellitemsg-index">

    <h1>Sunglass Ledger</h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <div class= 'col-md-6'>
        <?= GridView::widget([
        'id' => 'purchasetable',
        'dataProvider' => $dataProvider2,
        'filterModel' => $searchModel2,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            //'poisg_id',
            [
             'attribute' => 'Date',
             'value' => 'posg.posg_date'
            ],
            'posg_invno',
            [
             'attribute' => 'Vendor',
             'value' => 'posg.posg_vname'
            ],

            //'poisg_sgname',
            'poisg_qty',

            //['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
    </div>

    <div class= 'col-md-6'>
    <?= GridView::widget([
        'id' => 'selltable',
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            //'ssgi_id',
            [
             'attribute' => 'Date',
             'value' => 'sellsg.ssg_date'
            ],
            'ssgi_invoiceno',
            [
             'attribute' => 'Customer',
             'value' => 'sellsg.ssg_customer'
            ],
            //'ssgi_sgname',
            //'ssgi_price',

            //['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
</div>
</div>
<?php
/* start getting the textboxes */
$script = <<< JS
$(document).on('ready', function(e) {

        var purgrid = purchasetable.getElementsByTagName('poisg_qty');
        var total0 = 0;


        for (var i = 0; i < purgrid.length; i++) {

            var totp = purgrid[i].value
            total0 = parseInt(total0) + parseInt(totp);
        }

        // ^ This number takes (n+1)th column
        console.log('First table total value: ' + total0);

    })
JS;
$this->registerJs($script);
/* end getting the textboxes */
?>

当前输出 - enter image description here

最佳答案

也许你可以使用 gridView 的 showFooter

首先您可以使用 dataProvider 计算值

        $yourTotal =0;
        $numRow = 0;
        foreach ($dataProvider->models as $key => $value) {
            $yourTotal += $value['your_attribute'];
            $numRow++;
        }

然后在 GridView 中,您可以设置 showFooter => TRUE,并在您需要的列中添加页脚(最终添加页脚选项)

        echo GridView::widget([
        'dataProvider' => $dataProvider,
        ......
        'showFooter'=>TRUE,
        ..... 
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            ......
            [   'attribute' => 'your_attribute',
                'label' => 'Your Lable',
                'footer' => $yourTotal,
                'footerOptions' => ['style' => 'text-align:right;'],        
            ],            
            ........

您可以使用 input 添加文本

<?= Html::textInput('your_name', $yourTotal , options[]); ?>

或者你可以添加一个简单的div

<div id='my_id'> <?=  $yourTotal ?> </div>

关于javascript - yii2 文本框中 gridview 列的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39253611/

相关文章:

javascript - 如何绘制具有补间动画效果的三个js线几何体?

javascript - 我想在 JavaScript 中为 img src 实现即发即弃请求

php - 即使有 allowedIP 条目,Yii2 调试栏也不会显示在登台服务器上

php - 如何在 Yii2 的动态模型中添加自定义验证功能?

checkbox - 如何将 Yii2 的 ActiveForm 复选框设置为选中状态?

javascript - 如何垂直对齐 HTML 对象以覆盖页面

javascript - JS 在传递之前绑定(bind)一个函数的上下文

带有函数原型(prototype)的 Javascript 命名空间声明

php - 如何通过Ajax改变 GridView ? yii2

php - 如何在yii2中显示关系数据