Bicycle/App/media/js/budgets.js
Egor Isaev c9b81cdcd1 dev
2026-08-14 13:09:54 +03:00

52 lines
1.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

$(function () {
var $form = $('#budgets-form');
if (!$form.length) {
return;
}
var $error = $('#budgets-form-error');
var csrf = $form.find('[name=csrf_token]').val();
var year = $form.find('[name=year]').val();
// Кнопки «Сохранить» больше нет — каждая ячейка сохраняется сама по себе при потере
// фокуса (change), формульная связанная статья пересчитывается на сервере и подставляется
// в свою (readonly) ячейку сразу, без перезагрузки страницы.
$(document).on('submit', '#budgets-form', function (e) {
e.preventDefault();
});
$(document).on('change', '.plan-input:not([readonly])', function () {
var $input = $(this);
var match = ($input.attr('name') || '').match(/^plan\[(\d+)\]\[(\d+)\]$/);
if (!match) {
return;
}
$error.hide().text('');
var data = {csrf_token: csrf, year: year};
data['plan[' + match[1] + '][' + match[2] + ']'] = $input.val();
$.post($form.attr('action'), data)
.done(function (response) {
if (!response.recalculated) {
return;
}
$.each(response.recalculated, function (categorieId, months) {
$.each(months, function (month, summ) {
$('input[name="plan[' + categorieId + '][' + month + ']"]').val(summ);
});
});
})
.fail(function (xhr) {
var errors = xhr.responseJSON && xhr.responseJSON.errors;
var message = errors ? Object.values(errors).join(' ') : 'Не удалось сохранить значение';
$error.text(message).show();
});
});
});