Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

jquery - Calling a function on grid action coumn click

I have a grid in yii that has an action column with a custom button I would like to call function when the grid button is clicked but i get an error

This is my code:

[
        'class' => 'kartikgridActionColumn',
        'template' => '{approve}{new}{reject}',
        'buttons' => [
            'approve' => function ($url, $model) {
                return Html::button('<i class="fa fa-check"></i>', ['value' => Url::to('approvetruck?id='.$model->id),'style' =>'background:none;border:none;','id' => 'approve','onclick'=>'alert('.$model->id.')']);
        },
            'reject' => function ($url, $model) {
                return Html::button('<i class="fa fa-close"></i>',  ['value' => Url::to('checktruck?id='.$model->id."&category=1"),'style' =>'background:none;border:none;', 'onclick'=>'reject('.$model->id.')');
            },

        ],
        'dropdown' => false,
        'vAlign' => 'middle',
        'urlCreator' => function ($action, $model, $key, $index) {
            return '#';
        },


        'deleteOptions' => ['label' => '<i class="glyphicon glyphicon-remove"></i>']
    ]

After the grid i have(on the same page)

<?php
$script = <<< JS
 function reject(id) {
   alert(id);

 }

 JS;
$this->registerJs($script);
 ?>

The first one approve doing the alert works but the reject which calls a function reject fails.. it returns an error of Uncaught ReferenceError: reject is not defined

How do i do this

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems you have to use below code for register js

<?php 

$this->registerJs(' 
    function reject(id) {
    alert(id);
    }', yiiwebVIEW::POS_HEAD); 
?> 

that will add the js in head part and it may get the function which you defined


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...