2

我在 yii 中的 CJuiDialog 有问题。我想将“删除”按钮设置为“不可见”,但这不起作用,按钮仍然显示。这是我的代码:

$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id' => 'dlg_EventCal',
'options' => array(
    'title' => Yii::t('CalModule.fullCal', 'Event detail'),
    'modal' => true,
    'autoOpen' => false,
    'hide' => 'slide',
    'show' => 'slide',
    'width'=> 400,
    'buttons' => array(
        array(
            'text' => Yii::t('CalModule.fullCal', 'OK'),
            'click' => "js:function() { eventDialogOK(); }"
        ),
        array(
            'text' => Yii::t('CalModule.fullCal', 'Cancel'),
            'click' => 'js:function() { $(this).dialog("close"); }',

        ),  
         array( 
    'text' => Yii::t('CalModule.fullCal', 'Delete'),
    'click' => 'js:function() { eventDialogDelete(); }',
   'visible'=>Yii::app()->user->checkAccess('deleteAllEvents'),

            ),

))));
4

1 回答 1

1

CJuiDialog只将数据传递给jQuery.dialog插件

看看这个函数(来自https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.dialog.js#L322):

_createButtons: function( buttons ) {
    var that = this,
        hasButtons = false;

    // if we already have a button pane, remove it
    this.uiDialogButtonPane.remove();
    this.uiButtonSet.empty();

    if ( typeof buttons === "object" && buttons !== null ) {
        $.each( buttons, function() {
            return !(hasButtons = true);
        });
    }
    if ( hasButtons ) {
        $.each( buttons, function( name, props ) {
            var button, click;
            props = $.isFunction( props ) ?
                { click: props, text: name } :
                props;
            // Default to a non-submitting button
            props = $.extend( { type: "button" }, props );
            // Change the context for the click callback to be the main element
            click = props.click;
            props.click = function() {
                click.apply( that.element[0], arguments );
            };
            button = $( "<button></button>", props )
                .appendTo( that.uiButtonSet );
            if ( $.fn.button ) {
                button.button();
            }
        });
        this.uiDialog.addClass( "ui-dialog-buttons" );
        this.uiDialogButtonPane.appendTo( this.uiDialog );
    } else {
        this.uiDialog.removeClass( "ui-dialog-buttons" );
    }
}

您的所有属性都将传递给props对象,然后将用作button 元素的属性:

button = $( "<button></button>", props )

所以你需要这样的代码:

array( 
    'text' => Yii::t('CalModule.fullCal', 'Delete'),
    'click' => 'js:function() { eventDialogDelete(); }',
    'style' => Yii::app()->user->checkAccess('deleteAllEvents') ? '' : 'display: none;',
)

但在这种情况下,您需要额外检查服务器端的权限!

于 2012-11-07T11:04:26.867 回答