Page 1 of 1

Option List Customization

Posted: 2020-11-27 07:43
by sathukorala
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?
1.png
1.png (18.97 KiB) Viewed 6022 times

Re: Option List Customization

Posted: 2020-11-27 09:33
by jsetzer
Alternative (1): JQuery plugin

Integrate a date range picker like this one:
https://www.jqueryscript.net/demo/Multi ... er-Plugin/

Scroll down to the example named "Batch mode ( week )" which automatically selects a whole week when selecting a date.

This is not out of the box. For integrating such a plugin you will need some programming knowlegdge in JQuery and Javascript.

There are more date range picker plugins available on the market, for example this one:
https://www.cssscript.com/date-range-picker-lightpick/

Re: Option List Customization

Posted: 2020-11-27 09:41
by jsetzer
Alternative (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
  • id
  • start {date, required, unique}
  • end {date, required, unique}
  • year {int, *}
  • month {int, *}
  • week {int, *}
* Year, month and week are optional by may (!) be very helpful later on for example for filtering using your lookup/Advanced SQL code.

Insert can be done using PHP+SQL being called by a CRON job or after login or in an _init hook.

Re: Option List Customization

Posted: 2020-11-27 14:55
by sathukorala
jsetzer wrote:
2020-11-27 09:41
Alternative (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
  • id
  • start {date, required, unique}
  • end {date, required, unique}
  • year {int, *}
  • month {int, *}
  • week {int, *}
* Year, month and week are optional by may (!) be very helpful later on for example for filtering using your lookup/Advanced SQL code.

Insert can be done using PHP+SQL being called by a CRON job or after login or in an _init hook.
The list will be a huge one, how can we have only few weeks around current date?

Re: Option List Customization

Posted: 2020-11-27 15:10
by jsetzer
... very helpful later on for example for filtering using your lookup/Advanced SQL code ...
Just click [Advanced] button in lookup configuration and change the SQL code which controls fetching lookup data.

Re: Option List Customization

Posted: 2020-11-28 04:58
by sathukorala
jsetzer wrote:
2020-11-27 15:10
... very helpful later on for example for filtering using your lookup/Advanced SQL code ...
Just click [Advanced] button in lookup configuration and change the SQL code which controls fetching lookup data.

Can't we use a code block like following to the DV.php and get the values at the form level?

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>
	
	 }?>

Re: Option List Customization

Posted: 2020-11-28 14:05
by sathukorala
Can't we use a code block like above in the DV.php and get the values at the form level?

Re: Option List Customization

Posted: 2020-11-28 20:31
by pbottcher
Hi,

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.

Re: Option List Customization

Posted: 2020-11-29 01:29
by sathukorala
pböttcher wrote:
2020-11-28 20:31
Hi,

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.
hooks/TABLENAME_init function is an excelent idea....

Re: Option List Customization

Posted: 2020-11-29 04:01
by sathukorala
This is the code which should be put in hooks/table_name.php for init function
It will auto-populate the CSV file and that will be taken to option list

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;
	}
1.png
1.png (12.7 KiB) Viewed 5855 times

Re: Option List Customization

Posted: 2020-11-29 04:23
by sathukorala
To make the dates descending change the common.js.php below section
"return b.text > a.text;" instead of "return a.text > b.text;"

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;
	});
}
1.png
1.png (12.22 KiB) Viewed 5854 times

Re: Option List Customization

Posted: 2020-11-30 05:32
by sathukorala
I have compiled a complete answer in this post

viewtopic.php?f=7&t=4044&p=15536#p15536

Re: Option List Customization

Posted: 2020-12-07 17:50
by pfrumkin
Does this happen every time someone lands on the form?

I assume the list is always Monday to Sunday, so this list would change weekly as opposed to daily.

Thanks.
~Paul

Re: Option List Customization

Posted: 2020-12-08 04:12
by sathukorala
pfrumkin wrote:
2020-12-07 17:50
Does this happen every time someone lands on the form?

I assume the list is always Monday to Sunday, so this list would change weekly as opposed to daily.

Thanks.
~Paul
Yes the list will be auto generated each time you load the detail view. Date ranges will be generated based on the current date