Aggiunta gestione righe dinamiche per i modelli prima nota

This commit is contained in:
MatteoPistorello 2022-02-21 16:39:25 +01:00
parent 0a3de4b428
commit 09b016abd8
2 changed files with 198 additions and 103 deletions

View File

@ -19,6 +19,40 @@
include_once __DIR__.'/../../core.php'; include_once __DIR__.'/../../core.php';
function renderRiga($id, $riga)
{
// Conto
echo '
<tr>
<td>
{[ "type": "select", "name": "idconto['.$id.']", "id": "conto'.$id.'", "value": "'.($riga['idconto'] ?: '').'", "ajax-source": "conti-modelliprimanota" ]}
</td>';
// Dare
echo '
<td>
{[ "type": "number", "name": "dare['.$id.']", "id": "dare'.$id.'", "value": "'.($riga['totale'] > 0 ? abs($riga['totale']) : 0).'" ]}
</td>';
// Avere
echo '
<td>
{[ "type": "number", "name": "avere['.$id.']", "id": "avere'.$id.'", "value": "'.($riga['totale'] < 0 ? abs($riga['totale']) : 0).'" ]}
</td>
</tr>';
}
// Nuova riga
echo '
<table class="hide">
<tbody id="template">';
renderRiga('-id-', null);
echo '
</tbody>
</table>';
?><form action="" method="post" id="add-form"> ?><form action="" method="post" id="add-form">
<input type="hidden" name="op" value="add"> <input type="hidden" name="op" value="add">
<input type="hidden" name="backto" value="record-edit"> <input type="hidden" name="backto" value="record-edit">
@ -41,6 +75,10 @@ include_once __DIR__.'/../../core.php';
Form di aggiunta riga movimento Form di aggiunta riga movimento
*/ */
echo ' echo '
<button class="btn btn-info btn-xs pull-right" type="button" id="addriga" onclick="addRiga(this)">
<i class="fa fa-plus"></i> '.tr('Aggiungi riga').'
</button>
<br><br>
<table class="table table-striped table-condensed table-hover table-bordered" <table class="table table-striped table-condensed table-hover table-bordered"
<tr> <tr>
<th width="60%">'.tr('Conto').'</th> <th width="60%">'.tr('Conto').'</th>
@ -48,24 +86,10 @@ include_once __DIR__.'/../../core.php';
<th width="20%">'.tr('Avere').'</th> <th width="20%">'.tr('Avere').'</th>
</tr>'; </tr>';
for ($i = 0; $i < 10; ++$i) { $counter = 0;
$required = ($i <= 1) ? 1 : 0;
// Conto foreach ($rs as $r) {
echo ' renderRiga($counter++, $r);
<tr>
<td>
{[ "type": "select", "name": "idconto['.$i.']", "ajax-source": "conti", "required": "'.$required.'" ]}
</td>
<td class="text-right">
{[ "type": "number", "name": "dare['.$i.']", "id": "dare'.$id.'", "value": "'.($rs[$i]['totale'] > 0 ? $rs[$i]['totale'] : '').'"]}
</td>
<td class="text-right">
{[ "type": "number", "name": "avere['.$i.']", "id": "avere'.$id.'", "value": "'.($rs[$i]['totale'] < 0 ? abs($rs[$i]['totale']) : '').'"]}
</td>
</tr>';
} }
echo ' echo '
@ -110,21 +134,9 @@ include_once __DIR__.'/../../core.php';
</form> </form>
<script> <script>
$(document).ready( function() { $(document).ready(function() {
$('#modals select').on('change', function() { // Fix per l\'inizializzazione degli input
if($(this).parent().parent().find('input[disabled]').length != 1){ $("input[id*=dare], input[id*=avere]").each(function() {
if($(this).val()) {
$(this).parent().parent().find('input').prop("disabled", false);
}
else{
$(this).parent().parent().find('input').prop("disabled", true);
$(this).parent().parent().find('input').val("");
}
}
});
});
$("#modals input[id*=dare], #modals input[id*=avere]").each(function() {
if (input(this).get() === 0) { if (input(this).get() === 0) {
$(this).prop("disabled", true); $(this).prop("disabled", true);
} else { } else {
@ -132,19 +144,62 @@ include_once __DIR__.'/../../core.php';
} }
}); });
$(document).on("keyup change", "#modals input[id*=dare]", function() { // Trigger dell\'evento keyup() per la prima volta, per eseguire i dovuti controlli nel caso siano predisposte delle righe in prima nota
$("input[id*=dare][value!=\'\'], input[id*=avere][value!=\'\']").keyup();
$("select[id*=idconto]").click(function() {
$("input[id*=dare][value!=\'\'], input[id*=avere][value!=\'\']").keyup();
});
// Creazione delle eventuali righe aggiuntive
let table = $("#modals table").first();
let button = table.parent().find("button").first();
let row_needed = $("#modals select").length;
let row_count = table.find("tr").length - 2;
while (row_count < row_needed) {
button.click();
row_count++;
}
});
$(document).on("change", "select", function() {
let row = $(this).parent().parent(); let row = $(this).parent().parent();
if (!$(this).prop("disabled")) { if (row.find("input[disabled]").length > 1) {
row.find("#modals input[id*=avere]").prop("disabled", input(this).get() !== 0); row.find("input").prop("disabled", !$(this).val());
} }
}); });
$(document).on("keyup change", "#modals input[id*=avere]", function() { $(document).on("keyup change", "input[id*=dare]", function() {
let row = $(this).parent().parent(); let row = $(this).parent().parent();
if (!$(this).prop("disabled")) { if (!$(this).prop("disabled")) {
row.find("#modals input[id*=dare]").prop("disabled", input(this).get() !== 0); row.find("input[id*=avere]").prop("disabled", $(this).val().toEnglish());
} }
}); });
$(document).on("keyup change", "input[id*=avere]", function() {
let row = $(this).parent().parent();
if (!$(this).prop("disabled")) {
row.find("input[id*=dare]").prop("disabled", $(this).val().toEnglish());
}
});
var n = <?php echo $counter; ?>;
function addRiga(btn) {
var raggruppamento = $(btn).parent();
cleanup_inputs();
var tabella = raggruppamento.find("tbody");
var content = $("#template").html();
var text = replaceAll(content, "-id-", "" + n);
tabella.append(text);
restart_inputs();
n++;
}
$(document).ready(init);
</script> </script>

View File

@ -19,6 +19,40 @@
include_once __DIR__.'/../../core.php'; include_once __DIR__.'/../../core.php';
function renderRiga($id, $riga)
{
// Conto
echo '
<tr>
<td>
{[ "type": "select", "name": "idconto['.$id.']", "id": "conto'.$id.'", "value": "'.($riga['idconto'] ?: '').'", "ajax-source": "conti-modelliprimanota" ]}
</td>';
// Dare
echo '
<td>
{[ "type": "number", "name": "dare['.$id.']", "id": "dare'.$id.'", "value": "'.($riga['totale'] > 0 ? abs($riga['totale']) : 0).'" ]}
</td>';
// Avere
echo '
<td>
{[ "type": "number", "name": "avere['.$id.']", "id": "avere'.$id.'", "value": "'.($riga['totale'] < 0 ? abs($riga['totale']) : 0).'" ]}
</td>
</tr>';
}
// Nuova riga
echo '
<table class="hide">
<tbody id="template">';
renderRiga('-id-', null);
echo '
</tbody>
</table>';
?><form action="" method="post" id="edit-form"> ?><form action="" method="post" id="edit-form">
<input type="hidden" name="op" value="editriga"> <input type="hidden" name="op" value="editriga">
<input type="hidden" name="backto" value="record-edit"> <input type="hidden" name="backto" value="record-edit">
@ -58,6 +92,10 @@ for ($x = 0; $x < sizeof($conti2); ++$x) {
Form di modifica riga movimento Form di modifica riga movimento
*/ */
echo ' echo '
<button class="btn btn-info btn-xs pull-right" type="button" onclick="addRiga(this)">
<i class="fa fa-plus"></i> '.tr('Aggiungi riga').'
</button>
<br><br>
<table class="table table-striped table-condensed table-hover table-bordered" <table class="table table-striped table-condensed table-hover table-bordered"
<tr> <tr>
<th width="60%">'.tr('Conto').'</th> <th width="60%">'.tr('Conto').'</th>
@ -67,24 +105,10 @@ echo '
// Lettura movimenti del mastrino selezionato // Lettura movimenti del mastrino selezionato
$rs = $dbo->fetchArray('SELECT * FROM co_movimenti_modelli WHERE idmastrino='.prepare($record['idmastrino'])); $rs = $dbo->fetchArray('SELECT * FROM co_movimenti_modelli WHERE idmastrino='.prepare($record['idmastrino']));
for ($i = 0; $i < 10; ++$i) { $counter = 0;
$required = ($i <= 1) ? 1 : 0;
// Conto foreach ($rs as $r) {
echo ' renderRiga($counter++, $r);
<tr>
<td>
{[ "type": "select", "name": "idconto['.$i.']", "value": "'.$rs[$i]['idconto'].'", "ajax-source": "conti-modelliprimanota", "required": "'.$required.'" ]}
</td>
<td class="text-right">
{[ "type": "number", "name": "dare['.$i.']", "id": "dare'.$id.'", "value": "'.($rs[$i]['totale'] > 0 ? $rs[$i]['totale'] : '').'"]}
</td>
<td class="text-right">
{[ "type": "number", "name": "avere['.$i.']", "id": "avere'.$id.'", "value": "'.($rs[$i]['totale'] < 0 ? abs($rs[$i]['totale']) : '').'"]}
</td>
</tr>';
} }
echo ' echo '
@ -92,30 +116,30 @@ echo '
?> ?>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready( function() { $(document).ready(function() {
$('select').on('change', function() { // Fix per l\'inizializzazione degli input
if($(this).parent().parent().find('input[disabled]').length != 1){
if($(this).val()) {
$(this).parent().parent().find('input').prop("disabled", false);
}
else{
$(this).parent().parent().find('input').prop("disabled", true);
$(this).parent().parent().find('input').val("");
}
}
});
});
$("input[id*=dare], input[id*=avere]").each(function() { $("input[id*=dare], input[id*=avere]").each(function() {
let row = $(this).parent().parent();
if (!row.find("select").val()) {
if (input(this).get() === 0) { if (input(this).get() === 0) {
$(this).prop("disabled", true); $(this).prop("disabled", true);
} else { } else {
$(this).prop("disabled", false); $(this).prop("disabled", false);
} }
});
// Trigger dell\'evento keyup() per la prima volta, per eseguire i dovuti controlli nel caso siano predisposte delle righe in prima nota
$("input[id*=dare][value!=\'\'], input[id*=avere][value!=\'\']").keyup();
$("select[id*=idconto]").click(function() {
$("input[id*=dare][value!=\'\'], input[id*=avere][value!=\'\']").keyup();
});
});
$(document).on("change", "select", function() {
let row = $(this).parent().parent();
if (row.find("input[disabled]").length > 1) {
row.find("input").prop("disabled", !$(this).val());
} }
}); });
@ -123,7 +147,7 @@ echo '
let row = $(this).parent().parent(); let row = $(this).parent().parent();
if (!$(this).prop("disabled")) { if (!$(this).prop("disabled")) {
row.find("input[id*=avere]").prop("disabled", input(this).get() !== 0); row.find("input[id*=avere]").prop("disabled", $(this).val().toEnglish());
} }
}); });
@ -131,10 +155,26 @@ echo '
let row = $(this).parent().parent(); let row = $(this).parent().parent();
if (!$(this).prop("disabled")) { if (!$(this).prop("disabled")) {
row.find("input[id*=dare]").prop("disabled", input(this).get() !== 0); row.find("input[id*=dare]").prop("disabled", $(this).val().toEnglish());
} }
}); });
</script>
var n = <?php echo $counter; ?>;
function addRiga(btn) {
var raggruppamento = $(btn).parent();
cleanup_inputs();
var tabella = raggruppamento.find("tbody");
var content = $("#template").html();
var text = replaceAll(content, "-id-", "" + n);
tabella.append(text);
restart_inputs();
n++;
}
$(document).ready(init);
</script>
</form> </form>