WTForms / SQLAlchemy - Can you concatenate display values in SelectField or QuerySelectField - flask-sqlalchemy

Is is possible to have a WTForm in which a drop down selection box (SelectField or QuerySelectField) displays a concatentated display value?
For example, your database may contain:
ID=1, FirstName=John, LastName=Smith
ID=2, FirstName=Kim, LastName=Johnson
so the generated HTML code would be something like:
<select name="userid">
<option value="1">John Smith</option>
<option value="2">Kim Johnson</option>
</select>
The display value is the concatenated value of FirstName + LastName. The unique identifier in the database will be the ID.

You need a dynamically set SelectField:
form.userid.choices = [(item.id, item.firstname+' '+item.lastname) for item in
session.query(ModelName).all()]

Related

Generate xml fragments from a static set of rows

I have a static list of values I want to create a list of XML elements from. The values are: one, two, and three. I'm trying to avoid creating a new table just for this one special use case. The goal is to get rows with the following output from this list:
<option value="one"/>
<option value="two"/>
<option value="three"/>
I've tried the following SQL query in PostgreSQL which gets close:
SELECT xmlelement(name option, xmlattributes(s as value))
FROM (values('one'), ('two')) AS s;
However, I end up with the following output:
<option value="(one)"/>
<option value="(two)"/>
<option value="(three)"/>
I've also tried the following query with the same results:
SELECT xmlelement(name option, xmlattributes(s as value))
FROM (SELECT unnest('{one,two}'::varchar[])) AS s;
I believe I just need to find a way to refer to that column. I tried using ?column? but it didn't work. I'm also not sure if there is a cleaner way to write what I'm attempting.
May be it's me but there should be better ways of rendering HTML select options than database... like i.e. php, ruby, node.js etc.
In any case you can simply do
SELECT format('<option value="%s"/>', value) AS option
FROM (VALUES ('one'), ('two'), ('three')) options(value);
or
SELECT xmlelement(name "option", xmlattributes(value AS "value")) AS option
FROM (VALUES ('one'), ('two'), ('three')) options(value);
Output:
option
-------------------------
<option value="one"/>
<option value="two"/>
<option value="three"/>
Here is a dbfiddle demo

yadcf filter with select tag inside column

I have a table (using datatables framwork) which contains in a column tag. I mean that content of this columns looks like this:
<td>
<select class="form-control attendance_select" data-id_player="130">
<option value="-1">No</option>
<option value="0" selected="">No answer</option>
<option value="1">Yes</option>
</select>
</td>
When I use yadcf column filter and try to search only columns wih "Yes" as selected options in this select, it doesn't work because every rows contains "Yes" in html code.
Can you please help me, how to set yadcf for solving this issue if it's possible?
Thank you
You probably need to define filter type "custom_func" and specify a custom filter function for your column.
From inline docs: (reformatted explanation )
custom_func
Required: true, when filter_type is custom_func
Type: function
Default value: undefined
Description: should be pointing to a function with the following signature
function myCustomFilterFunction(filterVal, columnVal, rowValues, stateVal) {
}
where filterVal: is the value from the select box,
columnVal is the value from the relevant row column,
rowValues is an array that holds the values of
the entire row and
stateVal which holds the current state of the
table row DOM
, stateVal is perfect to handle situations in which you placing radiobuttons / checkbox inside table column (should be suitable for your case of select.
This function should return true if the row matches your condition and the row should be displayed) and false otherwise.

dynamic value sql

My dropdown return value 2014 and 2015
<select><option value="2014">2014</option>
<option value="2015">2015</option>
</select>
and my query is:
$sql = "SELECT name, value
FROM sales
WHERE year IN ('2015') ";
How I can do my query dynamic, depend on dropdown?
Thank you!
Get the selected value, place it into a variable, then assemble the query string, concatenating the command and this variable.
I recommend some data binding framework to perform this kind of task for you.
What's your development environment? PHP?
Try this with PHP:
$stmt = $conn->prepare("SELE‌​CT name, value FROM sales WHERE year = :year");
$stmt->bind_param(":‌​year", $year); $year = $_POST['year'];
$stmt->execute()

Adding Select All to dropdown

This is my query:
<cfquery name="qry" datasource="#variables.staffDs#">
SELECT
firstname + ' ' + surname name, userid, mobileno, extension
FROM
currentstaff
WHERE
(mobileno IS NOT NULL OR
Left(LTrim(extension), 2) = '07')
</cfquery>
This is my ColdFusion dropdown box:
<select name="to" id="to">
<option value=""> -- Select -- </option>
<option style="font-weight: bold" value="">Send to All</option>
<cfoutput query="people">
<option value="#qry.userid#">#qry.name#</option>
</cfoutput>
</select>
The names of all people are displayed in the dropdown box list.
What I'd like to do is add an option at the top of the dropdown box to 'Sent to All' to select all users and I'm not sure how to do this.
I'd like to incorporate any extra SQL I might need into my current query, if this is necessary.
you just need to change like this if possible , just add
Select 'Select All'
union
to your query , final code like
Select 'Select All','','',''
union
SELECT
firstname + ' ' + surname name, userid, mobileno, extension
FROM
currentstaff
WHERE
(mobileno IS NOT NULL OR
Left(LTrim(extension), 2) = '07')
Select 'Select All', '','',''
union
SELECT
firstname + ' ' + surname name, userid, mobileno, extension
FROM
currentstaff
WHERE
(mobileno IS NOT NULL OR
Left(LTrim(extension), 2) = '07')
Is there a reason to do this in SQL, rather than in HTML, like your example shows? I've worked on systems where a 'Select All' or 'Please choose' is unioned into the query and the downside is that you cannot re-use that query for other purposes, because the query is now generating something that is only useful in an HTML dropdown.
To my mind, getting a list of people is one concern and building a UI with those people and a 'Select All' option is a different concern.
I wouldn't add the option to query, I'd add an extra option row into the HTML and when SELECT ALL is chosen I would update my query to not filter.
<select name="to" id="to">
<option value=""> -- Select -- </option>
<option value="ALL">SELECT ALL</option>
<option style="font-weight: bold" value="">Send to All</option>
<cfoutput query="people">
<option value="#qry.userid#">#qry.name#</option>
</cfoutput>
</select>

Using CFQUERY and CFSELECT to pull multiple values based on selection

I have a CFQUERY pulling three columns. The following CFSELECT allows the user to make a selection from various results based on the 'display' parameter, and sets the value to the 'value' parameter.
I would like to pass the third unused value of that record to a variable to be used in a later query. I cannot set the 'value' field to the column I need since that value is needed in a query following this one (queries populate based on previous drop down selections).
Is there a way to do this? To somehow have the CFSELECT grab 2 separate values?
SubRegionName is Displayed.
State is the value to pass.
SubRegionCD for this selection made is needed later.
Code example below:
<cfquery name="qrySubTurf"
DATASOURCESTUFF>
SELECT SubRegionName, SubRegionCd, State
From dbo.tblRegions
where Region='#form.getRegion#' <!---Previous CFSELCT value--->
order by SubRegionName
</cfquery>
<cfselect name="getSubTurf"
style="width:220px"
size=1
multiple="no"
query="qrySubTurf"
value="state" <!---Value passed to the next CFQUERY--->
display="SubRegionName" <!---Value displayed to user--->
queryPosition="below"
onChange="AddForm.submit();">
<option value=""></option>
</cfselect>
Now I need to grab the SubRegionCD associated with the users selection of State and SubRegionName and assign it to a variable that can be used in the final query. I cannot use State alone to determine the SubRegionCD, but I CAN use SubRegionName to make a 1-1 match. Help?
Simplest (in terms of littlest-possible code change) would be to do:
<cfquery name="qrySubTurf"
DATASOURCESTUFF>
SELECT SubRegionName, SubRegionCd + ',' + State AS Key
From dbo.tblRegions
where Region=<cfqueryparam value="#form.getRegion#" cfsqltype="CF_SQL_VARCHAR">
order by SubRegionName
</cfquery>
<cfselect name="getSubTurf"
style="width:220px"
size=1
multiple="no"
query="qrySubTurf"
value="Key"
display="SubRegionName"
queryPosition="below"
onChange="AddForm.submit();">
<option value=""></option>
</cfselect>
And then use ListFirst(FORM.getSubTurf) and ListLast(FORM.getSubTurf). Also, don't forget to use <cfqueryparam>.
To maintain the query result across pages you have to split the value returned to the form's action page ... this is was the answer suggested ... <cfqueryparam value ="#ListFirst(form.***)#>