Implementando la creacion de un registro con esta extension
vitalets.github.io/x-editable/docs.html#newrecord
x-editable.demopage.ru/index.php?r=site/widgets#EditableDetailView
No me funciona, ya logre la edicion, eliminar, buscar, modificar; pero me falta crear, solicito su valiosa ayuda.
Me ha fascinado Yii, llevo poco, pero este error ya llevo dos semanas y no encuentro porque no graba.
Notas:
Tabla usuarios
key usuariosId int autoincremento (x-editable no me carga este campo supongo que lo asignara automaticamente).
[color="#0000FF"]Texto del UsuariosController[/color]
//FUNCION PARA CREAR UN REGISTRO DE XEDITABLE
public function actionCreateUser()
{
if(Yii::app()->request->isPostRequest) {
$model = new Usuarios;
$model->attributes = $_POST;
if($model->save()) {
echo CJSON::encode(array('usuariosId' => $model->primaryKey));
} else {
$errors = array_map(function($v){ return join(', ', $v); }, $model->getErrors());
echo CJSON::encode(array('errors' => $errors));
}
} else {
throw new CHttpException(400, 'Invalid request');
}
}
[color="#0000FF"][b]
Texto de la vista create.php[/b][/color]
<script>
$('#save-btn').click(function() {
$('.myeditable').editable('submit', {
url: $this->createUrl('Usuarios/InvocarUsandoAjax'),
ajaxOptions: {
dataType: 'json' //assuming json response
},
success: function(data, config) {
if(data && data.id) { //record created, response like {"id": 2}
//set pk
$(this).editable('option', 'pk', data.id);
//remove unsaved class
$(this).removeClass('editable-unsaved');
//show messages
var msg = 'New user created! Now editables submit individually.';
$('#msg').addClass('alert-success').removeClass('alert-error').html(msg).show();
$('#save-btn').hide();
$(this).off('save.newuser');
} else if(data && data.errors){
//server-side validation error, response like {"errors": {"username": "username already exist"} }
config.error.call(this, data.errors);
}
},
error: function(errors) {
var msg = '';
if(errors && errors.responseText) { //ajax error, errors = xhr object
msg = errors.responseText;
} else { //validation error (client-side or server-side)
$.each(errors, function(k, v) { msg += k+": "+v+"<br>"; });
}
$('#msg').removeClass('alert-success').addClass('alert-error').html(msg).show();
}
});
});
$('#reset-btn').click(function() {
$('.myeditable').editable('setValue', null) //clear values
.editable('option', 'pk', null) //clear pk
.removeClass('editable-unsaved'); //remove bold css
$('#save-btn').show();
$('#msg').hide();
});
</script>
<?php
$this->breadcrumbs = array(
$model->label() => array('index'),
Yii::t('app', 'Create'),
);
$this->menu = array(
array('label'=>Yii::t('app', 'List') . ' ' . $model->label(), 'url' => array('index')),
array('label'=>Yii::t('app', 'Manage') . ' ' . $model->label(), 'url' => array('admin')),
);
?>
<h1><?php echo Yii::t(‘app’, ‘Create’) . ’ ’ . GxHtml::encode($model->label()); ?></h1>
<?php //METODO ANTIGUO
$this->renderPartial(’_form’, array(
'model' => $model,
'buttons' => 'create'));
?>
<form class="form-inline editableform">
<div id="msg" class="alert hide"></div>
<div class="control-group">
<?php
//echo Yii::app()->basePath;
$model= new Usuarios;
$this->widget('ext.editable.EditableDetailView', array(
'data' => $model,
'id'=>'user-details',
//you can define any default params for child EditableFields
'url' => $this->createUrl('Usuarios/CreateUser'), //common submit url for all fields
'params' => array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken), //params for all fields
'emptytext' => 'Campo Vacio',
'attributes' => array(
array(
'name' => 'usuariosId',
/* ‘htmlOptions’ => array (
'class' => 'neweditable' ,
'rel' => 'Model_result_' . $model -> usuariosId ,
'data-pk' => $model -> usuariosId,) */
),
array(
'name' => 'cedula',
'editable' => array(
'type' => 'text',
'inputclass' => 'input-large',
'emptytext' => 'special emptytext',
'validate' => 'function(value) {
if(!value) return "Cedula es requerido";
}',
//'onblur'=>'cancel',
)
),
array(
'name' => 'nombres',
'editable' => array(
'type' => 'text',
'inputclass' => 'input-large',
'emptytext' => 'special emptytext',
'validate' => 'js: function(value) {
if ($.trim(value) == "")
return "Nombre es requerido";
}'
)
),
array(
'name' => 'apellidos',
'editable' => array(
'type' => 'text',
'inputclass' => 'input-large',
'emptytext' => 'special emptytext',
'validate' => 'function(value) {
if(!value) return "Apellido es requerido";
}'
)
),
array(
'name' => 'fnacimiento',
'title'=>'Ingresar Fecha de Nacimiento',
'editable' => array(
'type' => 'date',
'viewformat' => 'dd/mm/yyyy',
'options'=>array('language'=>'en'), //PARA COLOCAR TODO EN ESPAÑOL
)
),
array(
'name' => 'finicio',
'title'=>'Ingresar Fecha de Inicio',
'editable' => array(
'type' => 'date',
'viewformat' => 'dd/mm/yyyy',
)
),
array(
'name' => 'email',
'editable' => array(
'type' => 'text',
'inputclass' => 'input-large',
'title'=>'Ingresar email',
'validate' => 'function(value) {
if(!value) return "email es requerido";
}'
)
),
array(
'name' => 'usuario',
'editable' => array(
'type' => 'text',
'inputclass' => 'input-large',
'title'=>'Ingresar Usuario',
'validate' => 'function(value) {
if(!value) return "Usuario es requerido";
}'
)
),
array(
'name' => 'clave',
'editable' => array(
'type' => 'text',
'inputclass' => 'input-large',
'title'=>'Ingresar Clave',
'url'=>'Usuarios/createUser1',
'validate' => 'function(value) {
if(!value) return "Clave es requerido";
}',
)
),
array( //select loaded from ajax.
'name' => 'FK_perfilesId',
'editable' => array(
'type' => 'select',
'title'=>'Perfil del Usuario',
'source' => CHtml::listData(Perfiles::model()->findAll(),'perfilesId','nombre')
)
),
array( //select loaded from ajax.
'name' => 'cambio',
'editable' => array(
'type' => 'select',
'title'=>'Debe Cambiar la Clave',
'source' => array('0'=>'Inactivo','1'=>'Activo'),
)
),
array( //select loaded from ajax.
'name' => 'activo',
'editable' => array(
'type' => 'select',
'title'=>'Estado del Usuario',
'source' => array('0'=>'Inactivo','1'=>'Activo'),
)
),
)
));//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
?>
<?php
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
?>
</div><div class="editable-buttons">
<button id="save-btn" class="btn btn-primary">Save</button> <!–//hide–>
<button id="reset-btn" class="btn">Cancelar</button>
</div>
<div class="editable-error-block">
</div>
</form>
<?php if($model->isNewRecord) {
Yii::app()->clientScript->registerScript('new-user', '
$("#save-btn").click(function() {
$(this).parent().parent().find(".editable").editable("submit", {
url: "'.$this->createUrl('Usuarios/CreateUser').'",
data: '.CJSON::encode(array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken)).',
ajaxOptions: { dataType: "json" },
success: function(data, config) {
if(data && data.id) {
$(this).editable("option", "pk", data.id);
$(this).removeClass("editable-unsaved");
$("#msg").removeClass("alert-error").addClass("alert-success")
.html("User created! Now you can update it.").show();
$("#save-btn").hide();
} else {
config.error.call(this, data && data.errors ? data.errors : "Unknown error");
}
},
error: function(errors) {
var msg = "";
if(errors && errors.responseText) {
msg = errors.responseText;
} else {
$.each(errors, function(k, v) { msg += v+"<br>"; });
}
$("#msg").removeClass("alert-success").addClass("alert-error")
.html(msg).show();
}
});
});
');
}
?>