You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CGridView列按钮语法错误:意外的'"'求助

Fixing CGridView Column Button Syntax Error in Yii 1.x

Let's break down the issue you're facing: that unexpected quote error is happening because of messy quote nesting in your value attribute, plus incorrect handling of the $data["id"] variable. Your original code tries to splice the variable into the URL string but ends up creating invalid PHP syntax.

What Went Wrong

In your original value string, you wrapped $data["id"] in single quotes, which means PHP treats it as a literal string instead of a variable. On top of that, the way you nested quotes for the onclick handler creates a syntax conflict—PHP can't parse the mixed quotes correctly. Also, directly appending parameters to the URL path isn't the Yii way to build URLs, which adds unnecessary complexity.

Solution 1: Fix the String Splicing (Legacy Approach)

If you need to stick with the string-based value definition, correct the quote escaping and variable handling like this:

array(
    'header' => "Action",
    'id' => 'id',
    'name' => 'name',
    'type' => 'raw',
    'value' => 'CHtml::button("Settings", array(
        "onclick" => "document.location.href=\'" . Yii::app()->createUrl(\'admin/settings/AddEditPaymentSetting/edit\', array(\'action\' => \'edit\', \'eid\' => $data[\'id\'])) . "\'",
        "class" => "btn btn-default btn-top-rv btn-block"
    ))',
    'htmlOptions' => array(
        'style' => 'text-align: center;'
    ),
)

Here, we:

  • Properly escape single quotes inside the outer single-quoted string
  • Pass $data['id'] as a parameter to createUrl() instead of splicing it into the URL path
  • Use Yii's recommended URL building pattern with parameter arrays

If you're using Yii 1.1.14 or later, anonymous functions for value are supported and way easier to read—no more quote escaping headaches:

array(
    'header' => "Action",
    'id' => 'id',
    'name' => 'name',
    'type' => 'raw',
    'value' => function($data) {
        return CHtml::button("Settings", array(
            "onclick" => "document.location.href='" . Yii::app()->createUrl('admin/settings/AddEditPaymentSetting/edit', array('action' => 'edit', 'eid' => $data['id'])) . "'",
            "class" => "btn btn-default btn-top-rv btn-block"
        ));
    },
    'htmlOptions' => array(
        'style' => 'text-align: center;'
    ),
)

This approach lets you write clean PHP code directly, so you don't have to juggle nested quotes. The $data variable is passed directly to the function, making variable access straightforward.

Key Notes

  • Always use Yii::app()->createUrl() with parameter arrays instead of hardcoding URL paths—it handles URL management rules automatically and avoids errors.
  • Anonymous functions make column configuration much more maintainable, especially for complex actions.

内容的提问来源于stack exchange,提问作者Hoodini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:27:56