CrudField JavaScript Library for Bootstrap Date Picker #436
-
Tried to use the CrudField JavaScript Library calls to get and set dates for a Bootstrap Date Picker (Backpack pro). Is it supported ? Tried this script as a workaround : function setDateForInputField(variableName, monthsToAdd) {
var element = document.querySelector(`[bp-field-name="${variableName}"]`);
var formattedtoValue = null;
if (monthsToAdd !== null) {
var currentDate = new Date();
var toValue = new Date(currentDate.setMonth(currentDate.getMonth() + monthsToAdd));
var formattedtoValue = toValue.toISOString().slice(0, 10);
}
var inputField = element.querySelector('input[bp-field-main-input]');
var dateField = element.querySelector('input[data-bs-datepicker]');
$(dateField).datepicker('setDate', toValue); // Order of the next 2 methods seems important !
$(inputField).attr("value", formattedtoValue);
} Does the job. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @rchampagneca! Indeed some fields are not linear in terms of setting a value. Specially jQuery fields. Your solution works 👌 By the way, just for the reference, CRUD Fields can make it easy to manage the inputs. function setDateForInputField(fieldName, monthsToAdd) {
let field = crud.field(fieldName);
let $datepicker = field.wrapper.find('[data-bs-datepicker]').datepicker();
// date manipulation
let date = new Date();
date.setMonth(date.getMonth() + monthsToAdd)
$datepicker.datepicker('setDate', date);
field.value = date.toISOString().slice(0, 10);
} It's not tested, but should work the same way ... |
Beta Was this translation helpful? Give feedback.
-
@tabacitu this may have highlighted a possible issue we have with CRUD Fields. Not all fields allow to just set value ( I'm not sure how many field have this issue, but maybe we should think about it ... |
Beta Was this translation helpful? Give feedback.
Hi @rchampagneca!
Indeed some fields are not linear in terms of setting a value. Specially jQuery fields.
In most of the fields
field.value = 'new value'
will easily set a new value, this date picker requires extra steps.Your solution works 👌
By the way, just for the reference, CRUD Fields can make it easy to manage the inputs.
It's not …