One function is overriding other in javascript - yii2

自闭症网瘾萝莉.ら 提交于 2019-12-23 17:33:11

问题


I'm trying to get productname,productiondate and prodqty from production model in gridview and also the sum(prodqty) in a textbox when a particular product is selected. The gridview is populating fine. I can see the sum(prodqty) javascript alert in a flash. But it's not passing to the textbox. The error message is - uncaught exception: unknown (can't convert to string) Again if I comment out the code for the gridview, the sum(prodqty) is shown in the textbox but I don't get the gridview.

My Controller Actions -

public function actionIndex()
    {
        $catId = yii::$app->request->get('catId');
        $searchModel = new ProductionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $catId);
        $searchModel2 = new ProductsalesSearch();
        $dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams, $catId);
        $model = new Productnames();
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'searchModel2' => $searchModel2,
            'dataProvider2' => $dataProvider2,
            'model' => $model,
        ]);
    }
public function actionGetForProduction($catid)
    {
        $production = Production::find()->select('sum(prodqty) as totalproduction')->where(['productname'=>$catid])->asArray()->one();
        echo Json::encode($production);
    }

Javascript code in index.php -

<?php
/* start getting the textboxes */
$script = <<< JS
$(function(){
    $('#catid').change(function(){   
        getIndexpage();
        getTotalproduction();
    });

    var catid = $(this).val();

    var getIndexpage = function(){        
        var catid = String($('#catid').val());
        window.location = 'index.php?r=productstockbook/production/index&catId='+catid;       

    } ;
    var getTotalproduction = function(){        
        var catid = String($('#catid').val());
        $.get('index.php?r=productstockbook/production/get-for-production',{ catid : catid }, function(data){
        alert(data);
        var data = $.parseJSON(data);
        $('#production').attr('value',data.totalproduction);
    });

    } ;
});


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

The error screenshot -

What happens if I comment out the window.location code - Code -

<?php
/* start getting the textboxes */
$script = <<< JS
$(function(){
    $('#catid').change(function(){   
        getIndexpage();
        getTotalproduction();
    });

    var catid = $(this).val();

    var getIndexpage = function(){        
        var catid = String($('#catid').val());
        //window.location = 'index.php?r=productstockbook/production/index&catId='+catid;       

    } ;
    var getTotalproduction = function(){        
        var catid = String($('#catid').val());
        $.get('index.php?r=productstockbook/production/get-for-production',{ catid : catid }, function(data){
        alert(data);
        var data = $.parseJSON(data);
        $('#production').attr('value',data.totalproduction);
    });

    } ;
});


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

Screenshot if I comment out window.location code -

We can see that the alert is staying but the gridview is obviously not populating.

Complete Controller Code -

<?php

namespace frontend\modules\productstockbook\controllers;

use Yii;
use frontend\modules\productstockbook\models\Production;
use frontend\modules\productstockbook\models\ProductionSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use frontend\modules\productstockbook\models\ProductsalesSearch;
use yii\helpers\Html;
use frontend\modules\productstockbook\models\Productnames;
use yii\helpers\Json;

/**
 * ProductionController implements the CRUD actions for Production model.
 */
class ProductionController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Production models.
     * @return mixed
     */
    public function actionIndex()
    {
        $catId = yii::$app->request->get('catId');
        $searchModel = new ProductionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $catId);
        $searchModel2 = new ProductsalesSearch();
        $dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams, $catId);
        $model = new Productnames();
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'searchModel2' => $searchModel2,
            'dataProvider2' => $dataProvider2,
            'model' => $model,
        ]);
    }

    /**
     * Displays a single Production model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Production model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Production();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->productionid]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Production model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->productionid]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Production model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Production model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Production the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Production::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    public function actionGetForProduction($catid)
    {
        $production = Production::find()->select('sum(prodqty) as totalproduction')->where(['productname'=>$catid])->asArray()->one();
        echo Json::encode($production);
    }

}

Complete index.php code -

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use frontend\modules\productstockbook\models\Productnames;
use yii\helpers\Json;

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

$this->title = 'Product Stock Book';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="production-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?php
        echo Select2::widget([
        'model' => $model,
        'attribute' => 'productnames_productname',
        'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
        'options' => ['placeholder' => 'Select Product', 'id' => 'catid'],
        'pluginOptions' => [
            'allowClear' => true
        ],
        ]);
    ?>
    <div class="row-fluid">
        <div class="form-group">
            <div class="col-xs-4 col-sm-4 col-lg-4" >
                    <input type="text" class="form-control" id="production" readonly placeholder ="Production">                
            </div>
            <div class="col-xs-4 col-sm-4 col-lg-4" >
                    <input type="text" class="form-control" id="sell" readonly placeholder ="Sell">                
            </div>
            <div class="col-xs-4 col-sm-4 col-lg-4" >
                    <input type="text" class="form-control" id="stock" readonly placeholder ="Stock">                
            </div>
        </div>
    </div>
    <div class= 'col-md-6'>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            //'productionid',
            'productiondate',
            //'itemid',
            'productname',
            //'batchno',
            'prodqty',

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

<div class='col-md-6'>

    <?php
        echo GridView::widget([
        'dataProvider' => $dataProvider2,
        'filterModel' => $searchModel2,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'billdate',
            'productsales_partyname',
            'productname',
            'total',

        ], 
        ]); 
      ?>
  </div>
</div>


<?php
/* start getting the textboxes */
$script = <<< JS
$(function(){
    $('#catid').change(function(){   
        getIndexpage();
        getTotalproduction();
    });

    var catid = $(this).val();

    var getIndexpage = function(){        
        var catid = String($('#catid').val());
        //window.location = 'index.php?r=productstockbook/production/index&catId='+catid;       

    } ;
    var getTotalproduction = function(){        
        var catid = String($('#catid').val());
        $.get('index.php?r=productstockbook/production/get-for-production',{ catid : catid }, function(data){
        alert(data);
        var data = $.parseJSON(data);
        $('#production').attr('value',data.totalproduction);
    });

    } ;
});


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

Update -

I got that after window.location the page is redirected to a new page and all the javascript code after new page load should be written in the new page. Please let me know how to write codes in the new file. As this page is loaded dynamically I'm not so sure where to call the javascript functions.

来源:https://stackoverflow.com/questions/38949563/one-function-is-overriding-other-in-javascript-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!