Cfgrid cell contain hyperlink? - coldfusion

Using CF9 and have a pretty basic html cfgrid that returns results from a SQL query.
There are just two columns in the grid: "ID" and "IDType".
I'm looking to see if there's a way to implement some logic so that when I certain IDType shows up, the value in the ID field becomes the key value in a hyperlink.
Example: IF IDType = "web", and the ID is "1234", the value inside the ID field would show up as http:/www.website.com/1234.html (or...better: just show up as "1234" but be hyperlink-enabled to go to the aforementioned site.)
If the IDType is not (for example) "web", then the value just shows up as a regular cell value (text).
<cfgrid
name="idGrid"
title="Related IDs"
query="get_IDs"
format="html"
>
<cfgridcolumn name="ID" header="ID" />
<cfgridcolumn name="IDType" header="ID Source" />
</cfgrid>

One way to do this is you can generate and append a column to your query using the queryColumnAdd() function, put your link in the cell, and then push the modified query it to the cfgrid.
The link will render and you can click on it!
If I may pseudo code.. first write your normal query
<cfquery name = "myQuery" datasource = "myCFDatasource">
SELECT ID, field1, field2, field3
FROM aTable
</cfquery>
Next, we add your link column onto it..
<cfset queryAddColumn(myQuery, "Link", ArrayNew(1)) />
<cfloop query="myQuery">
<cfset querySetCell(myQuery, "Link", "http://www.mysite.com/some/index.cfm?ID=#myQuery.ID#"), myQuery.currentRow) />
</cfloop>
Then, you can take your modified query myQuery and submit it into your cfgrid like you did above.
Hope that helps get you on the right path.., worked well for me the last time I used it!

You can do it with javascript onRender function.
var gridRender = function()
{
var grid = ColdFusion.Grid.getGridObject('gridname');
var cm = grid.getColumnModel();
cm.setRenderer(0,renderFun); //first arguments stands for column number
}
var renderFun = function(value, cellMeta, record, row, col, data)
{
if(value != null)
{
switch(col)
{
case 0:
return "<a href='yoururl?id=" & value & "'>" & value & "</a>";
default:
return value;
}
}
};
And on coldfusion page call gridRender function on ajaxonload
<cfset ajaxOnLoad("gridRender")>
ajaxOnLoad will automatically call gridRender js function on page loaded and setup grid to render through gridRender function. renderFun will call everytime your column cell going to render.
Note: I haven't tested code just consider as test code and modify as per your need.

Related

problem creating a select using enum values

i have mysql table and has a data like this
i have a table with two columns value and label and both the columns have the enum type
Data of value is:
-43200|-39600|-36000|-32400|-28800|-25200|-21600|-18000|-14400|-10800|-7200|-3600|0|3600|7200|10800|14400|18000|21600|25200|28800|32400|36000|39600|43200|46800
Data of Label is:
GMT-12:00|GMT-11:00|GMT-10:00|GMT-09:00|GMT-08:00|GMT-07:00|GMT-06:00|GMT-05:00|GMT-04:00|GMT-03:00|GMT-02:00|GMT-01:00|GMT|GMT+01:00|GMT+02:00|GMT+03:00|GMT+04:00|GMT+05:00|GMT+06:00|GMT+07:00|GMT+08:00|GMT+09:00|GMT+10:00|GMT+11:00|GMT+12:00|GMT+13:00
but as i have two columns one is for the value and other is for the label, how can i create a function which will create a select having a option value and text and create a dropdown,
if its a single column, i could have done a valuelist of valuearray and loop over it, but now its two columns, i am bit confused how to
any clue
You already know how to do this, you're just not thinking about it in context.
Two columns, two lists (or arrays).
You know you can loop over "a" list to create HTML.
So loop over BOTH lists to create HTML.
Reference each list item by position and you're good to go.
<cfset sValuesList = "-43200|-39600|-36000|etc.">
<cfset sLabelsList = "GMT-12:00|GMT-11:00|GMT-10:00|etc.">
<cfoutput>
<select>
<cfif listLen(sValuesList, "|") EQ listLen(sLabelsList, "|")>
<cfloop from="1" to="#listLen(sValuesList, "|")#" index="x">
<option value="#listGetAt(sValuesList, x, "|")#">
#listGetAt(sLabelsList, x, "|")#
</option>
</cfloop>
</cfif>
</select>
</cfoutput>

How do you link a menu to a table from a query?

From the menu I have
<cfset temp =valuelist(GetDeptNum.csedept_name)>
<cfset ChangedElement = ListGetAt(temp, 2)>
<cfset TempToo = ListSetAt(temp, 2, ",")>
<cfdump var=#TempToo#>
<li class="fullChild"><a id="corporate" href="phonelistsearch_test.cfm?corporate" onclick="changeClass('corporate')" >Corporate</a><a class="opener"></a>
<ul>
<cfloop From = "1" To = "#ListLen(temptoo)#" INDEX = "Counter">
<cfoutput><li> #ListGetAt(temptoo, Counter)#
</cfoutput>
</cfloop>
</ul>
</li>
I have a table which I use a filter to search the table, working, see http://jsfiddle.net/45grrk3m/ .
How would I link it to a table so when I click on the plus sign I can choose different choices
and it will only filter those rows in that csedept_name I click?
Both tables GetUsers and GetDeptNum share the csedept_id collumn.
So from the menu dropdown I get the name right now which works GetDeptNum.csedept_name,
but not sure how I can make the connection between the menu and table.
You need to think about how you're generating the table.
Each option in your table should be a hyperlink back to the page (or ajax....) which passes the option selected. Your SQL query would then refine the search, and the table is recreated using the refined resulty

Query of Queries failing in Coldfusion 10

I'm getting and error when I tried to do a query of query.
Table named allData was not found in memory. The name is misspelled or the table is not defined.
I have an excel document and I'm outputting to a coldfusion var called allData, then I'm doing a query on that var. but I'm getting an error:
What am I doing wrong? The first dump shows the table appropriately.
function name="validateExcel" access="public" output="yes" returnType="void"
hint="search for dogs">
<cfspreadsheet
action="read"
src="#SESSION.theExcelFile#"
headerrow= "1"
excludeHeaderRow = "true"
query = "allData"
rows = "1-25"/>
<cfdump var = "#allData#"/>
<cfset rotCheck = new Query(
sql = "SELECT * FROM allData where dogType like '%rot'",
dbtype = "query"
) />
<cfset dogResult = rotCheck.execute().getResult() />
<cfdump
var = "#dogResult#" />
</cffunction>
(From comments ...)
I have to run, but short answer - the query variable from the spreadsheet is not in scope within the Query.cfc. (The documentation on Query.cfc is somewhat lacking IMO. ) Either pass in the query object as a parameter ie new Query(...., allData=allData) or use a <cfquery> instead.
Given that the dump works, the allData variable exists. A cfquery tag with the appropriate attributes will solve your problem for you.

Getting the Value of the Query to Populate Form Fields

I had a Query Where i am Getting 14 records and i need to Populate this records in 4 form fields, they are coming as
select id, name, value from mytable
check the attached Screenshot.
I cannot do cfoutput query as it will loop the form fields 14 times,
i tried something like
queryname.name[currentow], but it not worked, checked this URL of cflib http://cflib.org/udf/queryGetRow
but that did not seems to get work. need to populate the value of the name column in the form field
need to know what is the right way
From this comment only,
"honestl my friend, i think i got confused what to ask; I will try to explain again: Auume i have two columns in my table {name & Value}. It has four rows. Now i have to populate the form fields of the rows 1 to 4, which contains the the details from the value column of the table"
I would do this to build my form:
<cfquery name="data" datasource="whatever">
select name, value
from mytable
</cfquery>
<form action="somewhere.cfm" method="post">
<cfoutput query="data">
<input name="name_#name# value="#value#">
</cfoutput>
</form>
Processing the form is outside the scope of the question so I won't do it. However, I will mention some potential problems. If name is a text field, and it's values contain spaces, puncuation marks, etc, your page will crash due to an invalid variable name. If your table has an id field that is either uuid or numeric, that's a better field to use.
Ok, I did it, Here is if someone encounters this, i may not be able to correctly tell what is required. So here is my Solution:
<cfset rsSettings= initial.getSettings()>
<cfloop array="#rsSettings#" index="k">
<cfset settings[k['name']] = k['value']>
</cfloop>
My Query is returning me an array, Using ben's post
http://www.bennadel.com/blog/124-Ask-Ben-Converting-a-Query-to-an-Array.htm
so to fill the form i used like this and it worked
<input type="text" name="pri" class="text" value="<cfoutput>#settings['pri']#</cfoutput>" />
if you guys have better solution, please share

How can I use dynamically generated variables from cfloop individually?

I'm not sure my title explains this very well. Frankly, I'm not sure how to put it into words, but here goes:
I am capturing variables from dynamically created form fields on another page, that have been submitted to a page containing the code below. So far, everything works as I want it to. I'm getting the dynamically named form fields. Now I need to add the subtotal dollar amounts together to create a grand total. Unfortunately, I can't figure out how to get the individual totals out of the loop so I can do the math for the final number, due to their dynamic nature.
This part of the code below generates the subtotals, and I need to be able to add all of them together to get a grand total, but I can't figure out how:
#dollarformat(val(getticket[item].ticketprice * form[item]))#
Maybe I need to take a different approach...suggestions/help appreciated.
Here's the full code:
<CFLOOP LIST="#form.fieldnames#" INDEX="item">
<cfoutput>
<cfquery datasource="outertixdb" name="getticket[item]">
select * from tickets
where ticketid = '#item#'
</cfquery>
#getticket[item].ticketname#: #dollarformat(getticket[item].ticketprice)# x #form[item]# = #dollarformat(val(getticket[item].ticketprice * form[item]))#<br/>
</cfoutput>
You would need to set the name attribute of your cfquery using the following format:
<cfquery datasource="outertixdb" name="#getticket[item]#">
To handle the total, you would first need a variable before the cfloop
<cfset total = 0 />
Then, inside the loop, you simply add the price of the ticket to the total
<cfset total = total + getticket[item].ticketprice />
Also, you should be using cfqueryparam in your query. You can read more about it here
Lastly, if you do not need all the data in the tickets table, do not use 'select *..', only pull pack the data that you need.
Not sure I completely understand, but it sounds like you are simply trying to look up a bunch of ticket records, by "id". Then display the individual costs, plus a grand total. If that is all your doing, just give the fields the same name: ticketID. For example:
<input name="ticketID" value="1" ...>
<input name="ticketID" value="22" ...>
<input name="ticketID" value="45" ...>
<input name="ticketID" value="16" ...>
Then the values will be submitted as a list ie 1,22,45,16, which you can feed into your query using an IN clause. That lets you grab all of the data in a single query. (Generally you want to avoid running queries within a loop because performing a separate database query for each id generates a lot of unnecessary overhead and degrades performance).
* Change the cfsqltype as needed
SELECT TicketID, TicketPrice
FROM YourTable
WHERE TicketID IN ( <cfquerparam value="#FORM.ticketID#"
list="true"
cfsqltype="cf_sql_integer">
)
UPDATE:
form[item] is the value of the quantity select from the previous page.
That is a confusing naming convention. I would recommend using a slightly more intuitive name like "quantity". Something more descriptive of the contents. You can still use the ticket id to generate unique names, ie quantity_#ticketID#. For example, using the same ticket id's as above:
<input name="quantity_1" ...>
<input name="quantity_22" ...>
<input name="quantity_45" ...>
<input name="quantity_16" ...>
Once you have the results, there are several ways to generate a grand total. The simplest being to initialize a variable before your loop, then increment it as you iterate. Use the query ticket ID to grab the quantity value from the FORM scope:
<cfset grandTotal = 0>
<cfoutput query="yourQuery">
<!--- extract quantity value --->
<cfset currQuantity = FORM["quantity_"& yourQuery.ticketID ]>
... display price ...
<cfset grandTotal += val(yourQuery.ticketPrice * currQuantity )>
</cfoutput>
GrandTotal <cfoutput>#grandTotal#</cfoutput>
I would recommend throwing out the whole dynamically named query thing.
<cfset total = 0>
<CFLOOP LIST="#form.fieldnames#" INDEX="item">
<cfquery datasource="outertixdb" name="getticket">
select * from tickets
where ticketid = <cfqueryparam cfsqltype="cf_sql_varchar" value="#item#">
</cfquery>
<cfset total += getticket.ticketprice />
<cfoutput query="getTicket">
#ticketname#: #dollarformat(ticketprice)# × #form[item]#
= #dollarformat(val(ticketprice * form[item]))#<br/>
</cfoutput>
</cfloop>
Output your total as needed