How to set pre-selected values in Select2 APEX plugin? - jquery-select2

I have a page 100 with a Classic Report table region p100_report. It renders with a query similar to this:
select id, pages_list from (
select id, listagg(col_page, ':') within group (order by col_page) as pages_list
from t1
group by id
order by id
) t1_lag;
So if t1 is a table like this:
|id|col_page |
|--|---------|
| 1| 102|
| 1| 103|
| 1| 500|
| 1| 600|
| 2| 101|
| 2| 102|
| 2| 103|
then t1_lag is a table like this:
|id| pages_list|
|--|---------------|
| 1|102:103:500:600|
| 2| 101:102:103|
Each cell in p100_report table, pages_list column contains a link to the modal page 200. It has Select2 Multi-select item called p200_pages_s2.
List of values for p200_pages_s2:
|pageID|pageName|
|------|--------|
| 100|Main |
| 101|First |
| 102|Second |
| 103|Third |
| 200|Modal |
| 500|Admin |
| 600|Log |
It uses pageID for submits and pageName for Select2 lis display correctly.
I try to send specific page_list values to page 200 with Link > Target > Set Items, assigning \#PAGES_LIST#\ value (for example, \101:102:103\) to the P200_PAGES_S2.
In session state it has required colon separated string value, but GUI part is empty. I tried different Dynamic Actions with Submit, Refresh, even some JS code from here, but nothing seems to work.
The problem: I need to set pre-selected values in ul.select2-selection__rendered, add an interactive working li for each value from the colon separated list. Any advice?

The problem was in an incorrect Dynamic Action that was trying to set value with an SQL SELECT. I've deleted it, and now it works as designed.
I've tried to do it with SQL SELECT because before I was trying to set value via link, but with a comma separated set of values (e.g. 100,101,102), and that didn't work. Select2 understands only colon separated sets (100:101:102).

Related

Reference a cell to populate an array with its value with Google Sheets QUERY function

I am trying to put together a QUERY that I need to reference a static cell within the SELECT clause to input the cell's value as column value.
I've browsed around Stack Overflow and could not find that specific issue being referred to.
I need a workaround for:
= QUERY(Data!A1:R,
"SELECT A, Sheet2!B1, R
WHERE A is not null
LABEL Sheet2!A1 'Batch ID',1)
And it should be resulting in:
+--------------------------+
| QUERY output |
+--------------------------+
| Name | Batch ID | Status |
+------+----------+--------+
| Alan | 7632r | Sent |
+------+----------+--------+
| Joe | 7632r | Sent |
+------+----------+--------+
And the static reference cell from Sheet2!B1:
+------------------+
| Sheet2 reference |
+------------------+
| Batch ID | 7632r |
+----------+-------+
Apologies for not sharing a dummy sheet, but I have corporate security restrictions, that are not allowing me to share any sheet's link to anyone outside the company.
I am also interested in what downsides are possible if this can be made to work?
pausing query argument with '" appending cell with & then again appending the continue of query argument & and reopening argument with "'
=QUERY(Data!A1:R,
"select A, '"&Sheet2!B1&"', R
where A is not null
label '"&Sheet2!B1&"' 'Batch ID', 1)

How to return distinct rows in rails?

I have a model Program containing fields program_title, department_id and date. I have inserted two rows having same program title and date but different department_id.
Insert into programs(program_title,date,department_id) Values ("prog1","4/2/2017","1");
Insert into programs(program_title,date,department_id) Values ("prog1","4/2/2017","2");
Now I want to return rows which will be distinct by program_title whatever be the department_id. I have tried,
#event_contents=Program.select(:id,:date,:program_title).distinct(:program_title)
But still it returns both the rows. Any help is appreciated.
SQL can only collapse rows where all values are the same when using DISTINCT. Because you are selecting id, which is different for every record, the rows are not distinct. E.g.:
---------------------------------
| id | program_title | date |
---------------------------------
| 1 | prog1 | 4/2/2017 |
| 2 | prog1 | 4/2/2017 |
---------------------------------
You'll need to exclude the id from your #select for it to work:
Program.select(:date, :program_title).distinct
Try this
Program.all.distinct(:program_title).pluck( :id,:program_title,:date)
It will return data as array of elements though
Hope it helps

select distinct records based on one field while keeping other fields intact

I've got a table like this:
table: searches
+------------------------------+
| id | address | date |
+------------------------------+
| 1 | 123 foo st | 03/01/13 |
| 2 | 123 foo st | 03/02/13 |
| 3 | 456 foo st | 03/02/13 |
| 4 | 567 foo st | 03/01/13 |
| 5 | 456 foo st | 03/01/13 |
| 6 | 567 foo st | 03/01/13 |
+------------------------------+
And want a result set like this:
+------------------------------+
| id | address | date |
+------------------------------+
| 2 | 123 foo st | 03/02/13 |
| 3 | 456 foo st | 03/02/13 |
| 4 | 567 foo st | 03/01/13 |
+------------------------------+
But ActiveRecord seems unable to achieve this result. Here's what I'm trying:
Model has a 'most_recent' scope: scope :most_recent, order('date_searched DESC')
Model.most_recent.uniq returns the full set (SELECT DISTINCT "searches".* FROM "searches" ORDER BY date DESC) -- obviously the query is not going to do what I want, but neither is selecting only one column. I need all columns, but only rows where the address is unique in the result set.
I could do something like Model.select('distinct(address), date, id'), but that feels...wrong.
You could do a
select max(id), address, max(date) as latest
from searches
group by address
order by latest desc
According to sqlfiddle that does exactly what I think you want.
It's not quite the same as your requirement output, which doesn't seem to care about which ID is returned. Still, the query needs to specify something, which is here done by the "max" aggregate function.
I don't think you'll have any luck with ActiveRecord's autogenerated query methods for this case. So just add your own query method using that SQL to your model class. It's completely standard SQL that'll also run on basically any other RDBMS.
Edit: One big weakness of the query is that it doesn't necessarily return actual records. If the highest ID for a given address doesn't corellate with the highest date for that address, the resulting "record" will be different from the one actually stored in the DB. Depending on the use case that might matter or not. For Mysql simply changing max(id) to id would fix that problem, but IIRC Oracle has a problem with that.
To show unique addresses:
Searches.group(:address)
Then you can select columns if you want:
Searches.group(:address).select('id,date')

How to display TIMEDIFF(now, then) in a DB Grid?

Sorry, I am very new to DbGrids.
Should I use the query's field editor and somehow add a new field that captures the TIMEDIFF and then just add that as a column in my DbGrid?
Or can/should I skip the field editor and somehow declare the TIMEDIFFF as a column?
For this table, I want a DbGrid with 4 columns : start time, end time, duration, description (run_id is the primary key & will not be displayed).
I am stumped as to how to get data into a 'duration' column ...
mysql> describe test_runs;
+------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+----------------+
| run_id | int(11) | NO | PRI | NULL | auto_increment |
| start_time_stamp | timestamp | YES | | NULL | |
| end_time_stamp | timestamp | YES | | NULL | |
| description | varchar(64) | YES | | NULL | |
+------------------+-------------+------+-----+---------+----------------+
4 rows in set (0.37 sec)
[Update]
The query for the datasource is
SELECT start_time_stamp,
end_time_stamp,
TIMEDIFF(end_time_stamp, start_time_stamp) as duration,
description
FROM test_runs ORDER BY start_time_stamp DESC
and when I execute it manually in MySql, I get
mysql> select TIMEDIFF(end_time_stamp, start_time_stamp) as duration FROM
+----------+
| duration |
+----------+
| NULL |
| 00:04:43 |
| 00:00:13 |
| 00:00:06 |
| 00:00:04 |
+----------+
5 rows in set (0.00 sec)
but the corresponding column in the DB grid remains blank. Can anyone help? Thanks.
[Update] I am using AnyDac, if that helps. The query produces all fields, including the time difference, in MySql and also in the Delphi IDE when I use the AnYDac query editor and execute it.
The only problem is that I don't see it in the DB grid at run time. I double click the DB grid at design time and the columns are correct. The FielName property is set to duration, which is retuned by the query shown above. It doesn't exist in the database, but is calculated by the query; could that somehow be the problem??
[Aaaaaaaargh!!!] Someone tried to "improve" my code and set the query's text programatically at run-time (as SELECT * FROM test_runs), thus overwriting my design time query!! Since the databse table does not have a duration field, none was shown in the DB grid.
Words were had, voices were rasied and now I must apolgize for wasting your time. Sorry.
I would create a calculated field in your Query and add that field to your DbGrid.
so, as you say, with the Field Editor open for the query, right-click and select New Field (or press Ctrl-N). Give your new field a name (eg Duration), keep Component name default or rename if you desire. Set the Type as appropriate (DateTime most likely in this case) and set the Field Type to Calculated.
Then, in the OnCalcFields event of your Query, set the value of that field to the value you want. eg:
procedure TForm1.Query1CalcFields(DataSet: TDataSet);
begin
Dataset.FieldByName('description').AsDateTime :=
DataSet.FieldByName('end_time_stamp').AsDateTime -
DataSet.FieldByName('start_time_stamp').AsDateTime;
end;
Or, you could also include the Duration as an extra field in your select query. Unfortunately I don't have ready access to MySQL here, but it could be something like:
select run_id, start_time_stamp, end_time_stamp, description,
(end_time_stamp - start_time_stamp) as duration from test_runs;
Should I use the query's field editor and somehow add a new field that
captures the TIMEDIFF and then just add that as a column in my DbGrid?
Not at first, delete all of the entries from that field editor
Or can/should I skip the field editor and somehow declare the
TIMEDIFFF as a column?
Not at first, delete all of the entries from that column editor
After that you should see all columns from the open dataset inside the grid, because you eliminate all the limitations from column editor and field editor.
As a guess what happens to your grid/data you did not set the FieldName property for column duration and therefor the grid didn't know what to present.
to use a DBgrid you need a datasource,dataset each should be assigned to respective components,and make sure you have made the connection. if you didnt get any thing in the grid you need to check your dataset,if only the date difference column data is missing but header display the field name in the query then probably you may be getting null data.....if even you didn't get the header name too you need to check your query.......

How can i display a nested list in a multiple select or with checkboxes in Rails?

How can i display a nested list in a multiple select or with checkboxes in Rails?
My table looks like this:
| id | parent_id | name | lft | rgt |
Thanks...
You can try this link
http://code.google.com/p/checkboxtree/
For demo http://checkboxtree.googlecode.com/svn/tags/checkboxtree-0.4.3/index.html

Resources