EditableConfig

Application component to configure editable settings and defaults. Should be used in configuration file.
Has following options:

  • form - style of editable form: bootstrap, jqueryui, plain
  • mode - mode of container: popup, inline
  • defaults - default settings for all editable elements

Example

protected/config/main.php

//assume you unzipped extension under protected/extensions/x-editable
Yii::setPathOfAlias('editable', dirname(__FILE__).'/../extensions/x-editable');

return array(
    ...
    'import'=>array(
        ...
       'editable.*' //easy include of editable classes
    ),     
    
    // application components
    'components'=>array(
        ...
        //X-editable config
        'editable' => array(
            'class'     => 'editable.EditableConfig',
            'form'      => 'bootstrap', 
            'mode'      => 'popup',      
            'defaults'  => array(        
               'emptytext' => 'Click to edit',
               //'ajaxOptions' => array('dataType' => 'json') //usefull for json exchange with server
            )
        ),        
    )
);

EditableSaver

Server-side part for editable widgets. It helps to update record in db.
During request it performs validation of attribute, saves new value and sends response to widget.

Example

public function actionUpdateUser()
{
    $es = new EditableSaver('User');  //'User' is name of model to be updated
    $es->update();
}

If you want to update additional attributes you can use onBeforeUpdate event:

public function actionUpdateUser()
{
    $es = new EditableSaver('User');
    $es->onBeforeUpdate = function($event) {
        $event->sender->setAttribute('updated_at', date('Y-m-d H:i:s'));
    };
    $es->update();
}

EditableSaver does not echo anything to browser in case of success (as by default widgets look only into HTTP status).

  • HTTP = 200 means update is ok
  • HTTP != 200 means error, CHttpException is thrown with error message
Please note that EditableSaver performs only update of existing records!
To create new record you should use another action, see example in EditableDetailView.

If you want to exchange with server-side in more advanced mode (e.g. via JSON) you should catch exceptions thrown by EditableSaver and transform it into JSON response, e.g.

public function actionUpdateUser()
{
    $es = new EditableSaver('User');
    try {
        $es->update();
    } catch(CException $e) {
        echo CJSON::encode(array('success' => false, 'msg' => $e->getMessage()));
        return;
    }
    echo CJSON::encode(array('success' => true));
}

Widget configuration for JSON is:

'editable' => array(
   ...
   'url' => $this->createUrl('user/update'),
   'success' => 'js: function(response, newValue) {
       if(!response.success) return response.msg;
    }',
   'options' => array(
     'ajaxOptions' => array('dataType' => 'json')
   )    
),

Events

NameDescription
onBeforeUpdateFired before validation and saving of attribute. You can set additional data here. To cancel update you can call error() method:
$es = new EditableSaver('User');
$es->onBeforeUpdate = function($event) {
    if(Yii::app()->user->isGuest) {
        $event->sender->error('You are not allowed to update data');
    }
};
Message will be shown inside widget.
onAfterUpdateFired after successful update of attribute.
For any questions feel free to open issue on github.
Your feedback / contribution will be appreciated!