javascript 中一个函数覆盖另一个函数 - yii2

2023-12-25

我正在尝试从 gridview 中的生产模型以及sum(prodqty)当选择特定产品时在文本框中显示。 gridview 填充得很好。我可以看到sum(prodqty)javascript 警报一闪而过。但它没有传递到文本框。错误消息是 -uncaught exception: unknown (can't convert to string)再次,如果我注释掉 gridview 的代码,总和(prodqty)将显示在文本框中,但我没有得到 gridview。

我的控制器操作 -

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);
    }

index.php 中的 JavaScript 代码 -

<?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 - enter image description here

如果我注释掉 window.location 代码会发生什么 -代码 -

<?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 - enter image description here

我们可以看到警报仍然存在,但网格视图显然没有填充。

完整的控制器代码 -

<?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);
    }

}

完整的index.php代码 -

<?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 -

我得到了之后window.location页面被重定向到新页面,新页面加载后的所有 javascript 代码都应写入新页面中。请让我知道如何在新文件中编写代码。由于此页面是动态加载的,我不太确定在哪里调用 javascript 函数。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

javascript 中一个函数覆盖另一个函数 - yii2 的相关文章

  • Angular - 如何从 DOM 中删除我使用过 $compile 的元素?

    我需要的是两个 ng views 的功能 因为我不能 我想更改某些内容的innerHTML 并编译它 我遇到的问题是 当我再次更改内容时 我可以编译 但是 Angular 是否会自行删除绑定 或者我必须手动执行此操作 如果是这样 怎么办 编
  • Javascript 函数查找数字的倍数

    创建一个名为的函数multiplesOf 它将接受两个参数 第一个参数是数字数组 第二个参数是数字 该函数应返回一个新数组 该数组由参数数组中的每个数字组成 该数字是参数数字的倍数 So multiplesOf 5 6 7 8 9 10 3
  • Three.js:缩放几何图形后错误的 BoundingBox

    在我的场景中 我有一个简单的立方体 var test new THREE Mesh new THREE CubeGeometry 10 10 10 new THREE MeshBasicMaterial scene add test 该立方
  • Chrome 中的性能问题

    我目前正在从事一个相对较大的项目 使用 AngularJs 构建 应用程序的一部分是一个表单 您可以向其中添加任意数量的页面 不幸的是 添加了很多不必要的垃圾 即表示表单模型的对象可能会变得非常大 在某些时候 Chrome 基本上无法处理它
  • 如何更改 Google Maps v3 API for Directions 中的开始和结束标记图像

    我使用 DirectionsRender 绘制了一条路线 但我不知道如何用我自己的标记替换通用的 Google 标记 我知道并在正常的谷歌地图情况下使用它 但发现很难用开始和结束的方向标记来做到这一点 如果这是一个愚蠢的问题 感谢您的任何建
  • 摩卡 - Chai Karma“套件未定义”

    我对 jscript tdd 很陌生 遇到了问题 希望有人能告诉我我在做什么 在浏览器中运行测试 通过 HTML 文件 一切正常 通过节点和业力运行它们我得到以下异常 我想在 node js 主机的 karma 中使用 Mocha 和 Ch
  • 可以在初始 DOM 解析期间/之前修改 DOM 吗?

    是否可以在初始 DOM 解析期间或之前修改 DOM 或者我是否必须等到 DOM 被解析和构建之后才能与其交互 更具体地说 是否有可能阻止 DOM 中的脚本元素使用用户脚本 内容脚本或 Chrome 或 Firefox 中的类似脚本运行 在解
  • Snap.svg - 停止在可悬停元素的子元素上重新触发悬停事件

    对于一个项目 我使用的 SVG 形状由背景多边形和背景多边形上方的一些文本 我已将其转换为路径 组成 我正在使用 Snap svg 为我的形状设置动画 当我将鼠标悬停在多边形上时 形状应该缩放到特定尺寸 包括其中的所有内容 鼠标移开时 形状
  • 防止 iOS 键盘在 cordova 3.5 中滚动页面

    我正在使用 Cordova 3 5 和 jQuery mobile 构建 iOS 应用程序 我在大部分应用程序中禁用了滚动功能 但是 当我选择输入字段时 iOS 键盘会打开并向上滚动页面 我不想要这个功能 由于输入足够高 键盘不会覆盖它 我
  • Angular - CSS - 自定义类型=文件输入,如何使用按钮而不是标签?

    我制作了一个类型为 file 的自定义输入字段 因为我不喜欢默认的输入字段 为了实现这一目标 我做了
  • 使用 CSS 或 Javascript 填充动画

    我只是想知道是否可以使用 CSS 或 javascript 创建填充动画 基本上我想创建一个填充动画 如下图所示 http i40 tinypic com eit6ia png http i40 tinypic com eit6ia png
  • Javascript split 不是一个函数

    嘿朋友们 我正在使用 javascript sdk 通过 jQuery facebook 多朋友选择器在用户朋友墙上发布信息 但是我收到此错误friendId split 不是函数 这是我的代码 function recommendToFr
  • 将 UMD Javascript 模块导入浏览器

    你好 我正在对 RxJS 进行一些研究 我可以通过在浏览器中引用它来使用该库 如下所示 它使用全局对象命名空间变量 Rx 导入 我可以制作可观察的东西并做所有有趣的事情 当我将 src 更改为指向最新的 UMD 文件时 一切都会崩溃 如下所
  • 有没有办法在 onclick 触发时禁用 iPad/iPhone 上的闪烁/闪烁?

    所以我有一个有 onclick 事件的区域 在常规浏览器上单击时 它不会显示任何视觉变化 但在 iPad iPhone 上单击时 它会闪烁 闪烁 有什么办法可以阻止它在 iPad iPhone 上执行此操作吗 这是一个与我正在做的类似的示例
  • Firebase 函数 onWrite 未被调用

    我正在尝试使用 Firebase 函数实现一个触发器 该触发器会复制数据库中的一些数据 我想观看所有添加的内容votes user vote 结构为 我尝试的代码是 const functions require firebase func
  • 正则表达式 - 从 markdown 字符串中提取所有标题

    我在用灰质 https www npmjs com package gray matter 以便将文件系统中的 MD 文件解析为字符串 解析器产生的结果是这样的字符串 n Clean er ReactJS Code Conditional
  • 在 Javascript 中连接空数组

    我正在浏览一些代码 我想知道这有什么用处 grid push concat row 根据我的理解 它等同于 grid push row 为什么要大惊小怪 连接 你想使用 concat当您需要展平数组并且没有由其他数组组成的数组时 例如 va
  • 如何隐藏/禁用 Highcharts.js 中的图例框?

    我想问是否可以使用 HighCharts js 库隐藏图表中的所有图例框 var chart object chart renderTo render to type graph type colors graph colors title
  • 带参数的事件监听器

    我想将参数传递给 JavaScript 中的事件侦听器 我已经找到了解决方案 但我无法理解它们为什么或如何工作以及为什么其他解决方案不起作用 我有 C C 背景 但是 Javascript 函数的执行有很大不同 您能否帮助我理解以下示例如何
  • Jquery - 选择选项后如何获取选项的特定数据类型?

    我将直接跳到标记 然后解释我想要做什么 HTML 选择选项

随机推荐

  • 使用 tf.estimator.Estimator 时步数不匹配

    我正在研究 TensorFlow 估计器框架 我终于有了训练模型的代码 我使用简单的 MNIST 自动编码器进行测试 我有两个问题 第一个问题是为什么训练报告的步数与我在估计器 train 方法中指定的步数不同 第二个问题是如何使用训练钩子
  • 如何直接从SQL Server备份文件中读取元数据?

    一般来说 要从 SQL Server 备份文件中获取元数据 我们需要使用 TSQL 命令 例如restore headeronly or restore filelistonly 但是 有一些第三方工具可以直接从备份文件中读取此信息 例如这
  • 将数据和架构从 MySQL 迁移到 SQL Server

    是否有任何免费的解决方案可以将数据库从 MySQL 自动迁移到 正常 的 SQL Server Server 我一整天都在尝试这个简单的任务 至少我是这么认为的 我试过了 SQL Server Management Studio 的导入数据
  • 在 xlsxwriter 中模拟自动调整列

    我想在Python的xlsxwriter中模拟Excel的自动调整功能 根据这个url 不直接支持 http xlsxwriter readthedocs io worksheet html http xlsxwriter readthed
  • 教初学者编程的最佳方法? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • scipy.interpolate.make_interp_spline 如何检索所有系数?

    我有以下程序 nknots 4 x i 0 1 2 3 y i 1 np exp 1 np exp 2 np exp 3 coeff interpolate make interp spline x i y i bc type natura
  • 应用因违反 iCloud 存储准则而被拒绝

    我的应用程序最近被拒绝 因为它在将备份到 iCloud 的目录中安装了数据库 由于数据库附带了大量预先填充的数据 并且应用程序将用户生成的数据存储到同一文件中 因此 将用户生成的内容与预先填充的数据混合起来并不是苹果希望我们做的 到目前为止
  • 在 Eclipse 中生成项目时出错

    我安装了 eclipse 和 SDK 大约一周了 但即使当我打开一个新的 Hello World 项目并且我想运行 eclipse 时也会显示此错误 Error generating final archive Failed to crea
  • Heroku 数据库恢复问题

    已经尝试过不同的解决方案stackoverflow以及在不同的论坛上 但没有一个能够解决确切的问题 根据文档 https devcenter heroku com articles heroku postgres import export
  • Get-ChildItem -Exclude 参数如何工作?

    Get ChildItem Exclude 参数如何工作 它遵循什么规则 Get ChildItem 的 Get Help 根本不详细 省略指定的项目 该参数的值限定 路径参数 输入路径元素或模式 例如 txt 允许使用通配符 在 Stac
  • C# 单例模式和 MEF

    我有一个关于单例模式和 MEF 的问题 我是实施 MEF 插件的新手 但还没有找到答案 是否可以通过 MEF 实现的插件仅提供一个类的一个实例 我的旧课程是这样的 region Singleton This class provide a
  • 如何使用 Azure 资源管理器在 Azure Web 应用程序中设置应用程序日志

    有谁知道如何使用 Azure 资源管理器 Powershell 或 JSON 在 Azure Web 应用程序上设置以下诊断设置 使用 json 我只能找到这些设置 requestTracingEnabled true Failed req
  • 正确允许 bash 中命令替换的分词

    我编写 维护和使用大量的 bash 脚本 我认为自己是一名 bash 黑客 并努力有一天成为一名 bash 忍者 需要了解更多awk第一的 bash 需要理解的最重要的功能 挫折之一是引号和随后的参数扩展如何工作 这是有据可查 http m
  • 从 php 在计算机上运行脚本

    我尝试从 php 执行 shell 命令失败 目标是通过互联网 例如我的手机 打开 关闭我的计算机 服务器的音乐播放器 这是我能做的 我有一个非常简单的文件 play sh 代码 xdotool key XF86AudioPlay echo
  • Spring Boot 安全性的 CORS 问题

    我使用 Spring Boot Spring Security 并希望避免任何 Cors 操作 我正在尝试这里的第二个答案 可以在 Spring 中完全禁用 CORS 支持吗 https stackoverflow com question
  • 如何使用 Django ORM 将表情符号插入 MYSQL 5.5 及更高版本

    我正在尝试将表情符号插入到我的 mysql 表中的某个字段中 我运行了 alter 命令并将排序规则更改为 utf8mb4 general ci ALTER TABLE XYZ MODIFY description VARCHAR 250
  • 计算地图上的最短路径(Google 地图、Openstreetmaps 等)

    我想计算某种已经存在的地图 API 路线上的最短路径 然后绘制它们 话虽这么说 我需要能够提取 获取尽可能多的数据 即路线的坐标 以便能够对其进行操作 我的第一个想法是使用 Google 地图 但据我了解 Google 地图 API 为我的
  • ValueError:对已关闭文件的 I/O 操作

    import csv with open v csv w as csvfile cwriter csv writer csvfile delimiter quotechar quoting csv QUOTE MINIMAL for w c
  • 带适配器回收单元的 android gridview header 解决方案

    我见过很多关于如何实现与网格的其余部分一起滚动的 gridview 标题的解决方案 其中大多数包括创建一个列表布局或相对布局 其中标题视图和网格视图全部位于滚动视图内 该解决方案存在以下问题 滚动视图不知道网格的大小 因此要克服这个问题 您
  • javascript 中一个函数覆盖另一个函数 - yii2

    我正在尝试从 gridview 中的生产模型以及sum prodqty 当选择特定产品时在文本框中显示 gridview 填充得很好 我可以看到sum prodqty javascript 警报一闪而过 但它没有传递到文本框 错误消息是 u