I have an events-table with a couple of date/time fields:
Depending on the value of eventtype_id, which is a dropdown, I wanted to hide certain fields, for example...
...when selecting eventtype "Antragstellung" (Date of application) I only need one date...
...when selecting eventtype "Mehrtätiges Ereignis ohne Uhrzeiten" I need two dates but no times ...
...when selecting eventtype "Mehrtätiges Ereignis mit Uhrzeiten" I need two dates with times ...
As you can see the visibility of certain fields changes.
This is mainly done by listening to the change event of the dropdown, getting the id of the selected dropdown-item, pulling some JSON data from the server via ajax and finally hiding fields according to the JSON data.
I'm not only hiding fields, I'm also changing labels:
2nd screenshot "date_from"-label = "Datum"
3rd screenshot "date_from"-label = "Datum (von)"
This is the same field "date_from" in the database.
This is part of events-dv.js:
Code: Select all
// on change of eventtype_id: update fields
$j('#eventtype_id-container').change(function () {
var id = $j(this).val();
updateFields(id);
});
// ... there is more here
// load eventtype by id using ajax with josn response
$j.getJSON('hooks/eventtypes.api.php?id=' + id, function (data) {
console.log(data);
// ...
// hide/show fields
new AppGiniField('date_from')
.visible(data.show_date_from)
.label(data.show_date_from && data.show_date_till, 'Datum (von)', 'Datum')
;
new AppGiniField('time_from')
.visible(data.show_time_from)
.label(data.show_time_from && data.show_time_till, 'Uhrzeit (von)', 'Uhrzeit')
;
new AppGiniField('date_till').visible(data.show_date_till);
new AppGiniField('time_till').visible(data.show_time_till);
new AppGiniField('custom').visible(data.show_custom);
});