Bind a grid to select list
There are a number of examples of binding a grid column to a text field, but binding a grid column to a select list is a little trickier. The problem is that a number of components contain the bind attribute, however, the select list does not. To select an item in a select list you need to find the item you want and tell the component to select that row.
To bind a select list to a grid, you need to loop over all of the items in the select list comparing them to the value you want selected. We will do this in the onChange="" attribute of cfgrid and a for loop in actionscript.
<cfscript>
qNames = queryNew("id,firstname,lastname,city,state,zip");
queryAddRow(qNames);
querySetCell(qNames, "id", "1");
querySetCell(qNames, "firstname", "Mike");
querySetCell(qNames, "lastname", "Nimer");
querySetCell(qNames, "city", "Boston");
querySetCell(qNames, "state", "MA");
querySetCell(qNames, "zip", "O2127");
queryAddRow(qNames);
querySetCell(qNames, "id", "1");
querySetCell(qNames, "firstname", "Bob");
querySetCell(qNames, "lastname", "Jones");
querySetCell(qNames, "city", "Salt Lake City");
querySetCell(qNames, "state", "UT");
querySetCell(qNames, "zip", "84111");
queryAddRow(qNames);
querySetCell(qNames, "id", "3");
querySetCell(qNames, "firstname", "Chris");
querySetCell(qNames, "lastname", "Smith");
querySetCell(qNames, "city", "Las Vegas");
querySetCell(qNames, "state", "NV");
querySetCell(qNames, "zip", "55555");
queryAddRow(qNames);
querySetCell(qNames, "id", "4");
querySetCell(qNames, "firstname", "Susan");
querySetCell(qNames, "lastname", "McFoo");
querySetCell(qNames, "city", "Boston");
querySetCell(qNames, "state", "MA");
querySetCell(qNames, "zip", "02466");
</cfscript>
<cfform format="Flash">
<cfgrid name="UsersGrid" format="Flash"
query="qNames" maxRows="5" rowheaders="No"
onchange="for (var i:Number = 0; i<state.length; i++) {if (state.getItemAt([i]).data == UsersGrid.selectedItem.state) state.selectedIndex = i}">
<cfgridcolumn name="id">
<cfgridcolumn name="firstname" header="First Name">
<cfgridcolumn name="lastname" header="Last Name">
<cfgridcolumn name="city" header="city">
<cfgridcolumn name="state" header="state">
</cfgrid>
<cfinput type="text" name="firstname" label="First Name"
bind="{UsersGrid.dataProvider[UsersGrid.selectedIndex]['firstname']}"
onChange="UsersGrid.dataProvider.editField(UsersGrid.selectedIndex, 'firstname', firstname.text);">
<cfinput type="text" name="lastname" label="Last Name"
bind="{UsersGrid.dataProvider[UsersGrid.selectedIndex]['lastname']}"
onChange="UsersGrid.dataProvider.editField(UsersGrid.selectedIndex, 'lastname', lastname.text);">
<cfselect name="state" width="200" size="1" label="State" onchange="UsersGrid.dataProvider.editField(UsersGrid.selectedIndex, 'state', state.selectedItem.data);">
<option value="MA">Boston</option>
<option value="CA">California</option>
<option value="NV">Las Vegas</option>
<option value="UT">Salt Lake City</option>
</cfselect>
<cfformgroup type="horizontal">
<cfinput type="Submit" name="submitBtn" value="Save">
</cfformgroup>
</cfform>


TABLENAME="products"
GRID="artistGrid"
KEYONLY="Yes">
SELECT product_category, Product_category_id
FROM ProductCategories
ORDER BY Product_category
SELECT products.product_id, productcategories.product_category_id, productbrands.product_brand_id,
producttypes.product_type_id, vendors.vendor_id,
products.productname,
productcategories.product_category,
productbrands.product_brand,
producttypes.product_type,
products.price, products.sku, vendors.CompanyName
FROM products, productcategories, productbrands, producttypes, vendors
WHERE products.product_category_id = productcategories.product_category_id
AND products.product_brand_id = productbrands.product_brand_id
AND products.product_type_id = producttypes.product_type_id
AND products.vendor_id = vendors.vendor_id
DISPLAY="No">
DISPLAY="No">
DISPLAY="No">
DISPLAY="No">
DISPLAY="No">
HEADER="Product"
WIDTH="100">
HEADER="Price"
WIDTH="100">
HEADER="SKU"
WIDTH="100">
HEADER="Category"
WIDTH="80">
HEADER="Brand"
WIDTH="100">
HEADER="Type"
WIDTH="80">
HEADER="Vendor"
WIDTH="80">
onChange="artistGrid.dataProvider.editField(artistGrid.selectedIndex, 'productname', productname.text);">
onChange="artistGrid.dataProvider.editField(artistGrid.selectedIndex, 'price', price.text);">
onChange="artistGrid.dataProvider.editField(artistGrid.selectedIndex, 'sku', sku.text);">
How can i do to bind the grid to two or more selects?
I´ve got to bind it to a category and a subcategory selects, is there a way?
Here is the code I am working with...
However, it is really annoying that IE makes short clicking sounds when you type into the input boxes. It sounds kind of like a typewriter. Is there a way to prevent this?
I tried to set the field with as but I keep getting undefined.
Here is quick part of my code.
<CFQUERY NAME="getinv" DATASOURCE="ds_inv">
SELECT Tag_num, Type
FROM inventory
ORDER BY Tag_num
</CFQUERY>
<cfsavecontent variable="setinv">
tagnum.text = assettag.selectedItem.Tag_num;
type.text = assettag.selectedItem.Type;
</cfsavecontent>
<cfSelect query="getinv" name="assettag" label="assettag" onChange="#setinv#"
value="Tag_num" display="Tag_num" queryPosition="below" width="100">
<option value=""> </option>
</cfSelect>
<cfformgroup type="horizontal">
<cfinput type="text" name="tagnum" label="TagNo" size="8"/>
<cfinput type="text" name="type" label="Type" size="8"/>
</cfformgroup>
Any help would be very appreciated.
assettag.selectedItem.TAG_NUM;
hope that helps.
It seems to work with
tagnum.text = assettag.selectedItem.Tag_num;
But it comes in as undefined like there is no data in the other fields or it can't match.
Thanks anyway.
Thank you for your reply and the link to this post. What I am trying to do is bind a select list to a grid, and having problems looping over all of the items in the select list comparing them to the value I want selected. For example if the db entry is "Call" then that is selected in the cfselect.
I have a a cfgrid which is outputting the cfquery fine in the cfgrid for the cfinputs but not the cfselect, and the cfselect doesn´t change when I select a new record to display:
<cfquery name="getcompany" datasource="cfcodeexplorer">
select * from COMPANY
</cfquery>
The values are:
send fax, send email, call and email, call, send letter presentation, no, meeting
Following your link that you send me I get the following code for the cfselect but I don´t understand how to loop over the values with cfscript and don´t know what to do if the value is "null".
<cfselect name="NEXTSTEP" width="200" size="1" label="NEXT STEP" onchange="companyGrid.dataProvider.editField(company.selectedIndex, 'NEXTSTEP', NEXTSTEP.selectedItem.data);">
<option value="SEND FAX">SEND FAX</OPTION>
<option value="SEND EMAIL">SEND EMAIL</OPTION>
<option value="CALL AND EMAIL">CALL AND EMAIL</OPTION>
<option value="CALL">CALL</OPTION>
<option value="SEND LETTER PRESENTATION">SEND LETTER</OPTION>
<option value="NO">NO</OPTION>
<option value="MEETING">MEETING</OPTION>
</cfselect>
Thank you very much for any pointers, Best Regards, Edward
Thanks a lot for your answer worked great: for those reading the blog: watch out for upper and lowercase problems in the formfields, db fields etc as you will get errors!
The cfgrid tag works fine for one field the "state" field output with cfselect with the following onchange actionscript:
<cfform format="Flash" skin="haloblue">
<cfgrid name="UsersGrid" format="Flash"
query="qNames" rowheaders="No"
onchange="for (var i:Number = 0; i<state.length; i++) {if (state.getItemAt().data == UsersGrid.selectedItem.state) state.selectedIndex = i}">
What I want to get is
onchange="for (var i:Number = 0; i<state.length; i++) {if (state.getItemAt().data == UsersGrid.selectedItem.state) state.selectedIndex = i}"
AND
onchange="for (var i:Number = 0; i<meetingtime.length; i++) {if (meetingtime.getItemAt().data == UsersGrid.selectedItem.meetingtime) meetingtime.selectedIndex = i}">
so I can connect to my meetingtime field and db field. I just don´t know how , thank you for any help.
<cfgridcolumn name="FIRSTNAME" header="Contact Name">
<cfgridcolumn name="ENTID" display="FALSE">
<cfgridcolumn name="state" header="Next Step">
</cfgrid>
<cfformgroup type="tabnavigator">
<cfformgroup type="page" label="Details: #dateFormat(now(), 'mmm dd, yyyy')#">
<cfformgroup type="horizontal">
<cfinput type="text" name="FIRSTNAME" label="First Name"
bind="{UsersGrid.dataProvider[UsersGrid.selectedIndex]['FIRSTNAME']}"
onChange="UsersGrid.dataProvider.editField(UsersGrid.selectedIndex, 'FIRSTNAME', FIRSTNAME.text);">
<cfselect name="state" width="200" size="1" label="Next Step" onchange="UsersGrid.dataProvider.editField(UsersGrid.selectedIndex, 'state', state.selectedItem.data);">
<option value="nulo">None</OPTION>
<option value="SEND STUDY">SEND STUDY</OPTION>
<option value="SEND PROPOSAL">SEND PROPOSAL</OPTION>
<option value="CALL AND EMAIL">CALL AND EMAIL</OPTION>
<option value="CALL">CALL</OPTION>
<option value="SEND PRESENTATION">SEND PRESENTATION</OPTION>
<option value="MEETING">MEETING</OPTION>
</cfselect>
<cfinput type="hidden" name="ENTID" label="ENTID"
bind="{UsersGrid.dataProvider[UsersGrid.selectedIndex]['ENTID']}"
onChange="UsersGrid.dataProvider.editField(UsersGrid.selectedIndex, 'ENTID', ENTID.text);">
</cfformgroup>
</cfform>
this works perfectly, thank you so much!
now, i have also checkboxes and radio buttons ...
how could i do that because it doens't have to loop for checkboxes, no?
cheers,
kat