Can't create, edit or delete

Can't create, edit or delete

FyesinhoFyesinho Posts: 3Questions: 0Answers: 0
edited January 2014 in Editor
Hi, my name is Rodrigo, from Chile. My english is poor.

I need show a table whit 2 where. Since there is no "or_where" I try whit this solution:
PHP:
[code]if ( !isset($_POST['action']) ) {
// Get data
$data = $db
->sql( 'SELECT * FROM usuario, perfil where perfil.id = usuario.perfil_id' )
->fetchAll();

echo json_encode( array( 'aaData' => $data

) );
}
else{

$editor = Editor::inst( $db, 'usuario' )
->field(
Field::inst( 'nombre_usuario' )->validator( 'Validate::required' ),
Field::inst( 'password_usuario' )->validator( 'Validate::required' ),
Field::inst( 'correo_usuario' )->validator('Validate::required')
)
->join(
Join::inst( 'perfil', 'object' )
->join(
array( 'id', 'usuario_id' ),
array( 'id', 'perfil_id' ),
'usuario_perfil'
)
->field(
Field::inst( 'id' )->validator( 'Validate::required' ),
Field::inst( 'nombre_perfil' )
)
);

// The "process" method will handle data get, create, edit and delete
// requests from the client
$out = $editor
->process($_POST)
->data();

if ( !isset($_POST['action']) ) {
// Get department details
$out['perfil'] = $db
->select( 'perfil', 'id as value, nombre_perfil as label' )
->fetchAll();
}

echo json_encode( $out );
} [/code]

HTML:

[code] var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "php/test.php",
"domTable": "#example",
"fields": [ {
"label": "Nombre Usuario:",
"name": "nombre_usuario"
}, {
"label": "Password Usuario:",
"name": "password_usuario",
"type": "password"
},{
"label": "Correo Usuario:",
"name": "correo_usuario"
}, {
"label": "Perfil Usuario:",
// The 'id' value from the property is used to set the value
// of the select list.
"name": "perfil.id",
"type": "select"
}
],
"i18n": {
"create": {
"button": "Insertar",
"title": "Insertar Perfil",
"submit": "Insertar"
},
"edit": {
"button": "Modificar",
"title": "Modificar Perfil",
"submit": "Modificar"
},
"remove": {
"button": "Eliminar",
"title": "Eliminar Perfil",
"submit": "Eliminar",
"confirm": {
"_": "¿Está seguro de borrar %d usuarios?",
"1": "¿Está seguro de borrar este usuario?"
}
},
"error": {
"system": "Ha ocurrido un error inesperado. Contactar con el Administrador de Sistema"
}
}
} );

$('#example').dataTable( {
"sDom": "Tfrtip",
"sAjaxSource": "php/test.php",
"aoColumns": [
{ "mData": "nombre_usuario" },
{ "mData": "password_usuario" },
{ "mData": "correo_usuario"},
{
// Use the 'name' property from the 'dept' object in the
// JSON. It might not be set, so we also provide a default.
"mData": "nombre_perfil",
"sDefaultContent": ""
}
],
"oTableTools": {
"sRowSelect": "multi",
"aButtons": [
{ "sExtends": "editor_create", "editor": editor },
{ "sExtends": "editor_edit", "editor": editor },
{ "sExtends": "editor_remove", "editor": editor }
]
},
"oLanguage": {
"sEmptyTable": "No hay datos",//tabla vacia
"sInfo": "Mostrando (_START_ - _END_) de _TOTAL_ registros",
"sLengthMenu": 'Mostrar '+
'10'+
'20'+
'30'+
'40'+
'50'+
'Todo'+
' registros',
"sLoadingRecords": " ",
"sSearch": "Buscar:",
"sZeroRecords": "No hay datos con esa busqueda",
"oPaginate": {
"sFirst": "Primero",
"sLast": "Ultimo",
"sNext": "Siguiente",
"sPrevious": "Anterior"
}
},
"fnInitComplete": function ( settings, json ) {
// Set the allowed values for the select and radio fields based on
// what is available in the database
editor.field('perfil.id').update( json.perfil );
}
} );
} );
[/code]

I can see the table, but i can't create, edit or delete some row. I have some problem or no exist solution?

Thanks in advanse

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    When you try to create, edit, delete, what is the Ajax response from the server? Editor will make an Ajax request, and the server will return some data - what is that data?

    Are you able to link me to the page you are working on so I can take a look?

    Allan
  • FyesinhoFyesinho Posts: 3Questions: 0Answers: 0
    Hi Allan.

    When I Create an user, this appear:

    https://dl.dropboxusercontent.com/u/109884878/1.png

    As you can see, don't work the select of "perfil usuario", but in background appear "perfil usuario" for the query "Select * from usuario, perfil where usuario.perfil_id = perfil.id".
    When i click in create button, return to the table but no add a new row.

    When I Modify an user, this appear:

    https://dl.dropboxusercontent.com/u/109884878/2.png
    ---
    It is worth mentioning that when I hold only this code works perfect, but I want to use the query to have more control and options in the tables
    [code] $editor = Editor::inst( $db, 'usuario' )
    ->field(
    Field::inst( 'nombre_usuario' )->validator( 'Validate::required' ),
    Field::inst( 'password_usuario' )->validator( 'Validate::required' ),
    Field::inst( 'correo_usuario' )->validator('Validate::required')
    )
    ->join(
    Join::inst( 'perfil', 'object' )
    ->join(
    array( 'id', 'usuario_id' ),
    array( 'id', 'perfil_id' ),
    'usuario_perfil'
    )
    ->field(
    Field::inst( 'id' )->validator( 'Validate::required' ),
    Field::inst( 'nombre_perfil' )
    )
    );

    // The "process" method will handle data get, create, edit and delete
    // requests from the client
    $out = $editor
    ->process($_POST)
    ->data();

    if ( !isset($_POST['action']) ) {
    // Get department details
    $out['perfil'] = $db
    ->select( 'perfil', 'id as value, nombre_perfil as label' )
    ->fetchAll();
    }

    echo json_encode( $out );
    }[/code]

    Allan,this is the information you needed? or do not understand you?
    Thanks in advance
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Thanks for the information!

    The select isn't being populated because the `$out['peril']` part of the PHP isn't running - there is not `peril` property in the returned JSON for the get request.

    I suspect the other error is the same - the structure of the data from your 'get' isn't working the way that the table is expecting data (i.e. joined data in a nested object).

    So it basically works when you use the Editor libraries? I don't see in the above code where you are adding an 'OR' condition though, which you said was the reason for doing it this way?

    Allan
  • FyesinhoFyesinho Posts: 3Questions: 0Answers: 0
    Allan,

    This is just a test thit one join and 4 fields, isn't the real table. The real table have more fields, joins and I need filter by 2 "where" (user and state).
    I try whit this, obviously don't work:

    [code]if ( !isset($_POST['action']) ) {
    // Get data
    $data = $db
    ->sql( 'SELECT * FROM usuario, perfil where perfil.id = usuario.perfil_id' )
    ->fetchAll();

    $out['perfil'] = $db
    ->select( 'perfil', 'id as value, nombre_perfil as label' );
    ->fetchAll();
    echo json_encode( array( 'aaData' => $data

    ) );
    }[/code]

    How should I incorporate the $out?
    Sorry all, I'm totally new to this and EDITOR DataTables and have helped me a lot to move.

    Thanks in advance
This discussion has been closed.