Option List Customization
Posted: 2020-11-27 07:43
I want to get option list of the current week and 7 weeks prior to this week to be auto-populated and the user to select, any help?
Like this?
Like this?
A place where AppGini users can exchange ideas and help each other.
https://forums.appgini.com:443/phpbb/
https://forums.appgini.com:443/phpbb/viewtopic.php?f=8&t=4035
The list will be a huge one, how can we have only few weeks around current date?jsetzer wrote: ↑2020-11-27 09:41Alternative (2): Additional "weeks" table
Create an additional table for weeks and populate it by PHP+SQL code. For example automatically insert all weeks till the end of the current yeay (or for the next 10 years or for the next three months ...).
Use that "weeks" table as lookup.
This solution could become be very flexible and can be integrated using built-in AppGini functions without any additional libraries or tools.
Example:
weeks* Year, month and week are optional by may (!) be very helpful later on for example for filtering using your lookup/Advanced SQL code.
- id
- start {date, required, unique}
- end {date, required, unique}
- year {int, *}
- month {int, *}
- week {int, *}
Insert can be done using PHP+SQL being called by a CRON job or after login or in an _init hook.
Just click [Advanced] button in lookup configuration and change the SQL code which controls fetching lookup data.... very helpful later on for example for filtering using your lookup/Advanced SQL code ...
Code: Select all
<?php
$PeriodWeekly = 8;
$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
for($i=0; $i<=$PeriodWeekly;$i++){
$n = $i*7;
$start_date = date("Y-m-d",strtotime(date("Y-m-d",$monday)." -$n days"));
$end_date = date("Y-m-d",strtotime(date("Y-m-d",$sunday)." -$n days"));
<option value="<?php echo "$start_date to $end_date"; ?>"><?php echo "$start_date to $end_date"; ?></option>
}?>
hooks/TABLENAME_init function is an excelent idea....pböttcher wrote: ↑2020-11-28 20:31Hi,
just a thought, you could use your code actually in the hooks/TABLENAME_init function to create the TABLENAME.FIELDNAME.csv file in the hooks folder, (see https://bigprof.com/appgini/help/advanc ... agic-files )
Like that you will always have the expected selection available.
One drawback, if you have an exiting record with a date selected that is not available in the newly created list (as it is already older than 7 weeks), the data will still be displayed in the tableview, but not in the detailview, as the reference to the possible selections will not be available any more.
Code: Select all
function COVID_cases_init(&$options, $memberInfo, &$args) {
$PeriodWeekly = 8;
$file = '.\hooks\tablename.fieldname.csv';
file_put_contents('.\hooks\tablename.fieldname.csv', "");
$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
for($i=0; $i<=$PeriodWeekly;$i++){
$n = $i*7;
$start_date = date("Y-m-d",strtotime(date("Y-m-d",$monday)." -$n days"));
$end_date = date("Y-m-d",strtotime(date("Y-m-d",$sunday)." -$n days"));
$current = $start_date . " to " . $end_date . ";;";
$current = file_get_contents($file);
$current .= $start_date . " to " . $end_date . ";;";
file_put_contents($file, $current);
}
return TRUE;
}
Code: Select all
/* function to sort select2 search results by relevence */
AppGini.sortSelect2ByRelevence = function(res, cont, qry) {
return res.sort(function(a, b) {
if(qry.term) {
var aStart = a.text.match(new RegExp("^" + qry.term, "i")) !== null;
var bStart = b.text.match(new RegExp("^" + qry.term, "i")) !== null;
if(aStart && !bStart) return false;
if(!aStart && bStart) return true;
}
return b.text > a.text;
});
}
Yes the list will be auto generated each time you load the detail view. Date ranges will be generated based on the current date