mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Añadido datatables editor
This commit is contained in:
@ -10,6 +10,9 @@ use App\Entities\Clientes\ClienteContactoEntity;
|
||||
use App\Models\Clientes\ClienteModel;
|
||||
|
||||
use App\Models\Clientes\ClienteContactoModel;
|
||||
use DataTables\Editor;
|
||||
use DataTables\Editor\Field;
|
||||
use DataTables\Editor\Validate;
|
||||
|
||||
class Clientecontactos extends \App\Controllers\GoBaseResourceController
|
||||
{
|
||||
@ -206,7 +209,7 @@ class Clientecontactos extends \App\Controllers\GoBaseResourceController
|
||||
$length = $reqData['length'] ?? 5;
|
||||
$search = $reqData['search']['value'];
|
||||
$requestedOrder = $reqData['order']['0']['column'] ?? 1;
|
||||
$order = ClienteContactoModel::SORTABLE[$requestedOrder > 0 ? $requestedOrder : 1];
|
||||
$order = ClienteContactoModel::SORTABLE[$requestedOrder >= 0 ? $requestedOrder : 1];
|
||||
$dir = $reqData['order']['0']['dir'] ?? 'asc';
|
||||
|
||||
$id_C = $reqData['id_cliente'] ?? -1;
|
||||
@ -223,13 +226,71 @@ class Clientecontactos extends \App\Controllers\GoBaseResourceController
|
||||
return $this->respond(Collection::datatable(
|
||||
$resourceData,
|
||||
$this->model->getResource()->countAllResults(),
|
||||
$this->model->getResource($search, $id_C)->countAllResults()
|
||||
$this->model->getResource("", $id_C)->countAllResults()
|
||||
));
|
||||
} else {
|
||||
return $this->failUnauthorized('Invalid request', 403);
|
||||
}
|
||||
}
|
||||
|
||||
public function datatable_editor() {
|
||||
if ($this->request->isAJAX()) {
|
||||
|
||||
include(APPPATH . "ThirdParty/DatatablesEditor/DataTables.php");
|
||||
|
||||
// Build our Editor instance and process the data coming from _POST
|
||||
$response = Editor::inst( $db, 'cliente_contactos' )
|
||||
->fields(
|
||||
Field::inst( 'nombre' )
|
||||
->validator( 'Validate::notEmpty',array(
|
||||
'message' => 'Falta nombre' )
|
||||
),
|
||||
Field::inst( 'apellidos' )
|
||||
->validator( 'Validate::notEmpty',array(
|
||||
'message' => 'Falta apellidos' )
|
||||
),
|
||||
Field::inst( 'cargo' )
|
||||
->validator( 'Validate::notEmpty',array(
|
||||
'message' => 'Falta cargp' )
|
||||
),
|
||||
Field::inst( 'telefono' )
|
||||
->validator( 'Validate::notEmpty',array(
|
||||
'message' => 'Falta telefono' )
|
||||
),
|
||||
Field::inst( 'email' )
|
||||
->validator( 'Validate::notEmpty',array(
|
||||
'message' => 'Falta email' )
|
||||
),
|
||||
|
||||
Field::inst( 'cliente_id' ),
|
||||
|
||||
)
|
||||
->validator( function($editor, $action, $data){
|
||||
if ($action === Editor::ACTION_CREATE || $action === Editor::ACTION_EDIT){
|
||||
/*foreach ($data['data'] as $pkey => $values ){
|
||||
// No se pueden duplicar valores al crear o al editar
|
||||
if (!empty($response)){
|
||||
return $response;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
})
|
||||
->debug(true)
|
||||
->process( $_POST )
|
||||
->data();
|
||||
|
||||
$newTokenHash = csrf_hash();
|
||||
$csrfTokenName = csrf_token();
|
||||
|
||||
$response[$csrfTokenName] = $newTokenHash;
|
||||
|
||||
echo json_encode($response);
|
||||
|
||||
} else {
|
||||
return $this->failUnauthorized('Invalid request', 403);
|
||||
}
|
||||
}
|
||||
|
||||
public function allItemsSelect()
|
||||
{
|
||||
if ($this->request->isAJAX()) {
|
||||
|
||||
@ -14,12 +14,11 @@ class ClienteContactoModel extends \App\Models\GoBaseModel
|
||||
protected $useAutoIncrement = true;
|
||||
|
||||
const SORTABLE = [
|
||||
0 => "t1.cargo",
|
||||
1 => "t1.nombre",
|
||||
2 => "t1.apellidos",
|
||||
0 => "t1.nombre",
|
||||
1 => "t1.apellidos",
|
||||
2 => "t1.cargo",
|
||||
3 => "t1.telefono",
|
||||
4 => "t1.email",
|
||||
5 => "t2.nombre",
|
||||
];
|
||||
|
||||
protected $allowedFields = ["cliente_id", "cargo", "nombre", "apellidos", "telefono", "email"];
|
||||
@ -109,36 +108,22 @@ class ClienteContactoModel extends \App\Models\GoBaseModel
|
||||
$builder = $this->db
|
||||
->table($this->table . " t1")
|
||||
->select(
|
||||
"t1.id AS id, t1.cargo AS cargo, t1.nombre AS nombre, t1.apellidos AS apellidos, t1.telefono AS telefono, t1.email AS email, t2.nombre AS cliente_id"
|
||||
"t1.id AS id, t1.cargo AS cargo, t1.nombre AS nombre, t1.apellidos AS apellidos, t1.telefono AS telefono, t1.email AS email, t1.cliente_id AS cliente_id"
|
||||
);
|
||||
|
||||
// IMN
|
||||
if ($cliente_id != -1) {
|
||||
$builder->where('cliente_id', $cliente_id);
|
||||
}
|
||||
$builder->where('t1.cliente_id', $cliente_id);
|
||||
$builder->where("t1.is_deleted", 0);
|
||||
|
||||
$builder->join("clientes t2", "t1.cliente_id = t2.id", "left");
|
||||
|
||||
return empty($search)
|
||||
? $builder
|
||||
: $builder
|
||||
->groupStart()
|
||||
->like("t1.id", $search)
|
||||
->orLike("t1.cargo", $search)
|
||||
->like("t1.cargo", $search)
|
||||
->orLike("t1.nombre", $search)
|
||||
->orLike("t1.apellidos", $search)
|
||||
->orLike("t1.telefono", $search)
|
||||
->orLike("t1.email", $search)
|
||||
->orLike("t2.id", $search)
|
||||
->orLike("t1.id", $search)
|
||||
->orLike("t1.cliente_id", $search)
|
||||
->orLike("t1.cargo", $search)
|
||||
->orLike("t1.nombre", $search)
|
||||
->orLike("t1.apellidos", $search)
|
||||
->orLike("t1.telefono", $search)
|
||||
->orLike("t1.email", $search)
|
||||
->orLike("t2.nombre", $search)
|
||||
->groupEnd();
|
||||
}
|
||||
}
|
||||
|
||||
@ -495,7 +495,7 @@
|
||||
<th><?= lang('ClienteContactos.cargo') ?></th>
|
||||
<th><?= lang('ClienteContactos.telefono') ?></th>
|
||||
<th><?= lang('ClienteContactos.email') ?></th>
|
||||
<th></th>
|
||||
<th class="text-nowrap"><?= lang('Basic.global.Action') ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -663,12 +663,110 @@
|
||||
|
||||
const actionBtns = function(data) {
|
||||
return `
|
||||
<span class="edit"><i class="ti ti-pencil ti-sm btn-edit mx-2" data-id="${data.id}"></i></span>
|
||||
<span class="edit"><a href="javascript:void(0);"><i class="ti ti-pencil ti-sm btn-edit mx-2" data-id="${data.id}"></i></a></span>
|
||||
<a href="javascript:void(0);"><i class="ti ti-trash ti-sm btn-delete mx-2" data-id="${data.id}" data-bs-toggle="modal" data-bs-target="#confirm2delete"></i></a>
|
||||
<span class="cancel"></span>
|
||||
<span class="remove"><i class="ti ti-trash ti-sm btn-delete mx-2" data-id="${data.id}"></i></span>
|
||||
`;
|
||||
};
|
||||
|
||||
editor = new $.fn.dataTable.Editor( {
|
||||
ajax: {
|
||||
url: "<?= route_to('editorOfClienteContactos') ?>",
|
||||
headers: {
|
||||
<?= csrf_token() ?? "token" ?> : <?= csrf_token() ?>v,
|
||||
},
|
||||
},
|
||||
table : "#tableOfClienteContactos",
|
||||
idSrc: 'id',
|
||||
fields: [ {
|
||||
name: "nombre"
|
||||
}, {
|
||||
name: "apellidos"
|
||||
}, {
|
||||
name: "cargo"
|
||||
}, {
|
||||
name: "telefono"
|
||||
}, {
|
||||
name: "email"
|
||||
}, {
|
||||
"name": "cliente_id",
|
||||
"type": "hidden"
|
||||
},{
|
||||
"name": "deleted_at",
|
||||
"type": "hidden"
|
||||
},{
|
||||
"name": "is_deleted",
|
||||
"type": "hidden"
|
||||
},
|
||||
]
|
||||
} );
|
||||
|
||||
editor.on( 'preSubmit', function ( e, d, type ) {
|
||||
if ( type === 'create'){
|
||||
d.data[0]['cliente_id'] = id;
|
||||
}
|
||||
else if(type === 'edit' ) {
|
||||
for (v in d.data){
|
||||
d.data[v]['cliente_id'] = id;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
editor.on( 'postSubmit', function ( e, json, data, action ) {
|
||||
|
||||
yeniden(json.<?= csrf_token() ?>);
|
||||
});
|
||||
|
||||
editor.on( 'submitSuccess', function ( e, json, data, action ) {
|
||||
|
||||
theTable.clearPipeline();
|
||||
theTable.draw();
|
||||
});
|
||||
|
||||
// Activate an inline edit on click of a table cell
|
||||
$('#tableOfClienteContactos').on( 'click', 'tbody span.edit', function (e) {
|
||||
editor.inline(
|
||||
theTable.cells(this.parentNode.parentNode, '*').nodes(),
|
||||
{
|
||||
cancelHtml: '<a href="javascript:void(0);"><i class="ti ti-x"></i></a>',
|
||||
cancelTrigger: 'span.cancel',
|
||||
submitHtml: '<a href="javascript:void(0);"><i class="ti ti-device-floppy"></i></a>',
|
||||
submitTrigger: 'span.edit',
|
||||
submit: 'allIfChanged'
|
||||
}
|
||||
);
|
||||
} );
|
||||
|
||||
|
||||
// Delete row
|
||||
$('#tableOfClienteContactos').on( 'click', 'tbody span.remove', function (e) {
|
||||
|
||||
Swal.fire({
|
||||
title: '<?= lang('Basic.global.sweet.sureToDeleteTitle', [mb_strtolower(lang('Basic.global.sweet.line'))]) ?>',
|
||||
text: '<?= lang('Basic.global.sweet.sureToDeleteText') ?>',
|
||||
icon: 'warning',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonText: '<?= lang('Basic.global.sweet.deleteConfirmationButton') ?>',
|
||||
cancelButtonText: '<?= lang('Basic.global.Cancel') ?>',
|
||||
cancelButtonColor: '#d33'
|
||||
})
|
||||
.then((result) => {
|
||||
const dataId = $(this).data('id');
|
||||
const row = $(this).closest('tr');
|
||||
if (result.value) {
|
||||
editor
|
||||
.create( false )
|
||||
.edit( this.parentNode, false)
|
||||
.set( 'deleted_at', new Date().toISOString().slice(0, 19).replace('T', ' ') )
|
||||
.set( 'is_deleted', 1 )
|
||||
.submit();
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
var theTable = $('#tableOfClienteContactos').DataTable( {
|
||||
serverSide: true,
|
||||
processing: true,
|
||||
@ -717,7 +815,7 @@
|
||||
language: {
|
||||
url: "//cdn.datatables.net/plug-ins/1.13.4/i18n/<?= config('Basics')->i18n ?>.json"
|
||||
},
|
||||
/*buttons: [ {
|
||||
buttons: [ {
|
||||
className: 'btn btn-primary float-end me-sm-3 me-1',
|
||||
extend: "createInline",
|
||||
editor: editor,
|
||||
@ -725,7 +823,7 @@
|
||||
submitTrigger: -1,
|
||||
submitHtml: '<i class="ti ti-device-floppy"/>'
|
||||
}
|
||||
} ]*/
|
||||
} ]
|
||||
} );
|
||||
|
||||
|
||||
@ -733,6 +831,7 @@
|
||||
|
||||
<?=$this->section('css') ?>
|
||||
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.2.3/css/buttons.bootstrap5.min.css">
|
||||
<link rel="stylesheet" href="<?= site_url('themes/vuexy/css/datatables-editor/editor.dataTables.min.css') ?>">
|
||||
<?=$this->endSection() ?>
|
||||
|
||||
<?= $this->section('additionalExternalJs') ?>
|
||||
|
||||
Reference in New Issue
Block a user