📋 HelperList — tablas y listados legacy en el Admin
Actualizado: 2024-12-01
HelperList genera tablas de datos con paginacion, ordenacion, busqueda y acciones en el Back Office. Se usa tipicamente en AdminController o en la pagina de configuracion de un modulo para mostrar registros de una tabla propia.
#Estructura de HelperList
Ejemplo completo de HelperList en getContent()
php
<?php
private function renderList(): string
{
$fields_list = [
'id_mymodule_item' => [
'title' => $this->l('ID'),
'align' => 'center',
'width' => 50,
],
'name' => [
'title' => $this->l('Nombre'),
'width' => 'auto',
],
'active' => [
'title' => $this->l('Activo'),
'active' => 'status',
'type' => 'bool',
'align' => 'center',
'width' => 50,
],
'date_add' => [
'title' => $this->l('Fecha'),
'type' => 'datetime',
'width' => 150,
],
];
$helper = new HelperList();
$helper->shopLinkType = '';
$helper->simple_header = false;
$helper->identifier = 'id_mymodule_item'; // Campo PK
$helper->table = 'mymodule_item'; // Nombre de la tabla (sin prefijo)
$helper->actions = ['edit', 'delete']; // Acciones por fila
$helper->show_toolbar = true;
$helper->toolbar_scroll = true;
$helper->listTotal = $this->getTotalItems();
$helper->_defaultOrderBy = 'id_mymodule_item';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->title = $this->l('Mis elementos');
$this->_list = $this->getItems();
return $helper->generateList($this->_list, $fields_list);
}
private function getItems(): array
{
return Db::getInstance()->executeS(
'SELECT * FROM `' . _DB_PREFIX_ . 'mymodule_item` ORDER BY id_mymodule_item DESC'
) ?: [];
}
private function getTotalItems(): int
{
return (int) Db::getInstance()->getValue(
'SELECT COUNT(*) FROM `' . _DB_PREFIX_ . 'mymodule_item`'
);
}
#Definicion de columnas
| Propiedad | Tipo | Descripcion |
|---|---|---|
| title | string | Titulo de la columna |
| type | string | Tipo: text, bool, datetime, price, image |
| align | string | Alineacion: left, center, right |
| width | int|string | Ancho en px o 'auto' |
| active | string | Nombre de la accion de toggle (e.g. 'status') |
| callback | string | Nombre de metodo para renderizado custom |
| orderby | bool | Permitir ordenar por esta columna (default true) |
| search | bool | Mostrar campo de busqueda (default true) |
#Acciones por fila (row actions)
Procesar acciones de fila en postProcess() de getContent()
php
<?php
public function getContent(): string
{
$output = '';
// Accion de editar
if (Tools::isSubmit('editmymodule_item')) {
$id = (int) Tools::getValue('id_mymodule_item');
// Redirigir a formulario de edicion
}
// Accion de eliminar
if (Tools::isSubmit('deletemymodule_item')) {
$id = (int) Tools::getValue('id_mymodule_item');
Db::getInstance()->delete('mymodule_item', 'id_mymodule_item = ' . $id);
$output .= $this->displayConfirmation($this->l('Elemento eliminado.'));
}
// Accion de toggle activo/inactivo
if (Tools::isSubmit('statusmymodule_item')) {
$id = (int) Tools::getValue('id_mymodule_item');
$active = (int) Db::getInstance()->getValue(
'SELECT active FROM `' . _DB_PREFIX_ . 'mymodule_item` WHERE id_mymodule_item = ' . $id
);
Db::getInstance()->update('mymodule_item', ['active' => !$active], 'id_mymodule_item = ' . $id);
}
return $output . $this->renderList() . $this->renderForm();
}
#Bulk actions (acciones masivas)
Añadir bulk actions al HelperList
php
<?php
// Dentro de renderList()
$helper->bulk_actions = [
'delete' => [
'text' => $this->l('Eliminar seleccionados'),
'icon' => 'icon-trash',
'confirm' => $this->l('¿Estas seguro de eliminar los elementos seleccionados?'),
],
'enableSelection' => [
'text' => $this->l('Activar seleccionados'),
'icon' => 'icon-power-off text-success',
],
'disableSelection' => [
'text' => $this->l('Desactivar seleccionados'),
'icon' => 'icon-power-off text-danger',
],
];
// Procesar bulk action en getContent()
if (Tools::isSubmit('submitBulkdeleteMymodule_item')) {
$ids = Tools::getValue('mymodule_itemBox', []);
foreach ($ids as $id) {
Db::getInstance()->delete('mymodule_item', 'id_mymodule_item = ' . (int) $id);
}
}
Descargar en Markdown
Pensado para pegar en ChatGPT, Claude u otra IA. Incluye solo el contenido de esta pagina.