[Solved] Uncaught Reference Error For Js Method When I Use Yii::app()->Clientscript->Registerscript

Hi,

I have a problem about the JS.




<?php

$this->widget('zii.widgets.grid.CGridView', array(

    'id' => 'bet-grid',

    'dataProvider' => $model->search(),

    'filter' => $model,

    'columns' => array(

        array(

            'header' => Yii::t('strings', 'team_name'),

            'name' => 'team_id',

            'value' => '$data->team->name',

            'filter' => CHtml::listData(Team::model()->findAll(), 'id', 'name'),

            'cssClassExpression' => '$data->id ? "result-cell-$data->id" : "" '

        ),

        array(

            'header' => ( isset($racers[0]->name) ? $racers[0]->name : ""),

            'name' => 'bet1',

            'type' => 'raw',

            'value' => 'CHtml::textField("bet1-$data->id",$data->bet1, array("class"=>"value-input", "style"=>"width:90px"))',

            'cssClassExpression' => '$data->id ? "result-cell-$data->id" : "" '

        ),

        array(

            'header' => ( isset($racers[1]->name) ? $racers[1]->name : ""),

            'name' => 'bet2',

            'type' => 'raw',

            'value' => 'CHtml::textField("bet2-$data->id",$data->bet2, array("class"=>"value-input", "style"=>"width:90px"))',

            'cssClassExpression' => '$data->id ? "result-cell-$data->id" : "" '

        ),

        array(

            'header' => ( isset($racers[2]->name) ? $racers[2]->name : ""),

            'name' => 'bet3',

            'type' => 'raw',

            'value' => 'CHtml::textField("bet3-$data->id",$data->bet3, array("class"=>"value-input", "style"=>"width:90px"))',

            'cssClassExpression' => '$data->id ? "result-cell-$data->id" : "" '

        ),

        array(

            'header' => ( isset($racers[3]->name) ? $racers[3]->name : ""),

            'name' => 'bet4',

            'type' => 'raw',

            'value' => 'CHtml::textField("bet4-$data->id",$data->bet4, array("class"=>"value-input", "style"=>"width:90px"))',

            'cssClassExpression' => '$data->id ? "result-cell-$data->id" : "" '

        ),

        array(

            'header' => ( isset($racers[4]->name) ? $racers[4]->name : ""),

            'name' => 'bet5',

            'type' => 'raw',

            'value' => 'CHtml::textField("bet5-$data->id",$data->bet5, array("class"=>"value-input", "style"=>"width:90px"))',

            'cssClassExpression' => '$data->id ? "result-cell-$data->id" : "" '

        ),

        array(

            'class' => 'CLinkColumn',

            'labelExpression' => 'Yii::t("strings","save")',

            'urlExpression' => '"javascript:save($data->id);"',

            'cssClassExpression' => '$data->id ? "result-cell-$data->id" : "" '

        ),

    ),

    'rowCssClassExpression' => 'DataGridViewExtend::rowCssCustomClass($row, $data->saved, "saved")'

));


Yii::app()->clientScript->registerScript('betSave', '

function save(id){

        

var bet1 = jQuery("#bet1-"+id).val();

var bet2 = jQuery("#bet2-"+id).val();

var bet3 = jQuery("#bet3-"+id).val();

var bet4 = jQuery("#bet4-"+id).val();

var bet5 = jQuery("#bet5-"+id).val();

        

jQuery.ajax({

    url: "' . CController::createUrl("bet/ajaxSave") . '",

    data: { "bet1": bet1, "bet2": bet2, "bet3": bet3, "bet4": bet4, "bet5": bet5, "id": id },

    success: function(data) {

                   if(data=="false"){ 

                        alert("Mentési Hiba!");  

                   }else{

                        jQuery(".result-cell-"+id).addClass("saved");

                   }

              }

    });

}');

?>



In the last column there is


'urlExpression' => '"javascript:save($data->id);"',

and when I call this, the Console give me: Uncaught ReferenceError: save is not defined

But when I don’t use the RegisterScript() method. The javascript will work:




$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'result-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		//'id',

		array(

                    'header'=>Yii::t('strings','team_name'),

                    'name' => 'team_id',

                    'value'=>'$data->team->name',

                    'filter'=> CHtml::listData(Team::model()->findAll(), 'id', 'name'),

                    'cssClassExpression'=> '$data->id ? "result-cell-$data->id" : "" '

                ),

		array(

                    'header'=>Yii::t('strings','value'),

                    'name' => 'value',

                    'type'=>'raw',

                    'value'=>'CHtml::textField("result-value-$data->id",$data->value, array("class"=>"value-input"))',

                    'cssClassExpression'=> '$data->id ? "result-cell-$data->id" : "" '

                ),

		array(

                    'class'=>'CButtonColumn',

                    'template'=>'{delete}',

                    'cssClassExpression'=> '$data->id ? "result-cell-$data->id" : "" '

		),

                array(

			'class'=>'CLinkColumn',

                        'labelExpression'=>'Yii::t("strings","save")',

                        'urlExpression'=>'"javascript:save($data->id);"',

                        'cssClassExpression'=> '$data->id ? "result-cell-$data->id" : "" '

		),

	),

    'rowCssClassExpression'=>'DataGridViewExtend::rowCssCustomClass($row, $data->saved, "saved")'

)); ?>


<script type="text/javascript">

    $("yt1").click(function(){

       alert( $("#yt2[disabled]").val() );

    });

    function save(id){

        

        var resultValue = jQuery("#result-value-"+id).val();

        //alert(resultValue);

        $.ajax({

            url: "<?php echo CController::createUrl("result/ajaxSave"); ?>",

            data: { "value": resultValue, "id": id },

            success: function(data) { if(data=="false"){ alert("Mentési Hiba!"); jQuery("#result-value-"+id).val('0') }

                    else{jQuery(".result-cell-"+id).addClass("saved");}

                    }

        });

    }

    

</script>



You have to define that function before you define gridView.

And how can I do this? :)

The default script position is CClientScript::POS_READY, which wraps your code inside a $(document).ready() call. Most likely this breaks your function, since it’s not in the global scope. So pass CClientScript::POS_END at the end of registerScript().




Yii::app()->clientScript->registerScript('betSave', '

function save(id){

        

var bet1 = jQuery("#bet1-"+id).val();

var bet2 = jQuery("#bet2-"+id).val();

var bet3 = jQuery("#bet3-"+id).val();

var bet4 = jQuery("#bet4-"+id).val();

var bet5 = jQuery("#bet5-"+id).val();

        

jQuery.ajax({

    url: "' . $this->createUrl("bet/ajaxSave") . '",

    data: { "bet1": bet1, "bet2": bet2, "bet3": bet3, "bet4": bet4, "bet5": bet5, "id": id },

    success: function(data) {

                   if(data=="false"){ 

                        alert("Mentési Hiba!");  

                   }else{

                        jQuery(".result-cell-"+id).addClass("saved");

                   }

              }

    });

}', CClientScript::POS_END); // <<< Insert at the end of the body section



Yeap, after my reply, I found this solution :) Thanks.