1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-06-05 22:09:38 +02:00

Porting della funzionalità di ajax-info (#369)

This commit is contained in:
Thomas Zilio
2020-07-31 10:58:59 +02:00
parent b47fbc923d
commit e1f758c52f
4 changed files with 57 additions and 24 deletions

View File

@@ -8,12 +8,18 @@ if (!isset($resource)) {
$page = filter('page') ?: 0;
$length = filter('length') ?: 100;
// Opzioni di selezione sugli elementi
$options = filter('options') ?: [];
$options_compatibility = $_SESSION['superselect'] ?: [];
$options = array_merge($options_compatibility, $options);
// Preselezione su $elements dichiarato da file precedente
if (!isset($elements)) {
$elements = [];
}
$elements = (!is_array($elements)) ? explode(',', $elements) : $elements;
$results = AJAX::select($op, $elements, $search, $page, $length);
$results = AJAX::select($op, $elements, $search, $page, $length, $options);
echo json_encode($results);
}

View File

@@ -70,11 +70,24 @@ function start_superselect() {
dataType: 'json',
delay: 250,
data: function (params) {
return {
const data = {
search: params.term,
page: params.page || 0,
length: params.length || 100,
}
options: {}, // Dati aggiuntivi
};
// Lettura dei dati aggiuntivi
$(this).each(function () {
$.each(this.attributes, function () {
if (this.specified && this.name.startsWith('data-select-')) {
const name = this.name.replace('data-select-', '');
data.options[name] = this.value;
}
});
});
return data;
},
processResults: function (data, params) {
params.page = params.page || 0;
@@ -163,7 +176,7 @@ jQuery.fn.selectAdd = function (values) {
values.forEach(function (item) {
if (item.data) {
Object.keys(item.data).forEach(function(element) {
Object.keys(item.data).forEach(function (element) {
item['data-' + element] = item.data[element];
});
}
@@ -196,3 +209,12 @@ jQuery.fn.selectData = function () {
}
}
};
/**
* Imposta le informazioni aggiuntive di un <select> creato con select2.
*/
jQuery.fn.selectInfo = function (name, value) {
this.attr('data-select-' + name, value);
return this;
};

View File

@@ -25,10 +25,11 @@ class AJAX
* @param mixed $search
* @param int $page
* @param int $length
* @param array $options
*
* @return array
*/
public static function select($resource, $elements = [], $search = null, $page = 0, $length = 100)
public static function select($resource, $elements = [], $search = null, $page = 0, $length = 100, $options = [])
{
if (!isset($elements)) {
$elements = [];
@@ -215,13 +216,12 @@ class AJAX
* @param array $elements
* @param array $limit
* @param mixed $search
* @param array $options
*
* @return array|null
*/
protected static function getSelectResults($file, $resource, $elements = [], $limit = [], $search = null)
protected static function getSelectResults($file, $resource, $elements = [], $limit = [], $search = null, $options = [])
{
$superselect = self::getSelectInfo();
$where = [];
$filter = [];
$search_fields = [];
@@ -234,6 +234,9 @@ class AJAX
// Database
$dbo = $database = database();
// Opzioni di selezione
$superselect = $options;
require $file;
if (!isset($results) && !empty($query)) {
@@ -243,14 +246,6 @@ class AJAX
return isset($results) ? $results : null;
}
/**
* Restituisce le informazioni aggiuntive per i select.
*/
protected static function getSelectInfo()
{
return !empty($_SESSION['superselect']) ? $_SESSION['superselect'] : [];
}
/**
* Ottiene i risultati della ricerca all'interno di un file specifico (modulo).
*

View File

@@ -28,6 +28,21 @@ class SelectHandler implements HandlerInterface
$values['value'] = setting($values['valore_predefinito']);
}
// Informazioni aggiuntive per il select
$infos = [];
if (!empty($values['ajax-info'])) {
$list = explode(',', $values['ajax-info']);
foreach ($list as $element) {
list($name, $value) = explode('=', $element);
$values['data-select-'.$name] = $value;
$infos[$name] = $value;
}
unset($values['ajax-info']);
}
$values['value'] = (array) $values['value'];
// Inizializzazione del codice HTML
@@ -38,7 +53,7 @@ class SelectHandler implements HandlerInterface
// Gestione delle richieste AJAX (se il campo "ajax-source" è impostato)
if (!empty($values['ajax-source'])) {
if (!empty($values['value']) || is_numeric($values['value'])) {
$result .= $this->select2($values['ajax-source'], $values['value']);
$result .= $this->select2($values['ajax-source'], $values['value'], $infos);
}
} else {
if (!in_array('multiple', $extras)) {
@@ -109,17 +124,12 @@ class SelectHandler implements HandlerInterface
*
* @return string
*/
protected function select2($op, $elements)
protected function select2($op, $elements, $info)
{
// Richiamo del file dedicato alle richieste AJAX per ottenere il valore iniziale del select
ob_start();
$dbo = database();
include DOCROOT.'/ajax_select.php';
$text = ob_get_clean();
$response = \AJAX::select($op, $elements, null, 0, 100, $info);
$html = '';
$response = (array) json_decode($text, true);
$results = $response['results'];
foreach ($results as $element) {
$element = (array) $element;