Update a specific row in an html table - ColdFusion - coldfusion

I am outputting data from a query to an html table for representation. On the Right corner of the table I have an "Update" button and a "Delete" button.
What I am trying to do is:
When I press on the update button a modal opens. Inside that modal I have a form which I want to have predefined the values from the current row and be able to edit the specific row
When I press the delete button on a row I want that row to be deleted and reload the page
This is my html table, the last two columns on the right are the buttons
**Survey Name** **Category** **Weight** **Update** **Delete**
Consultation Ambiance 20 Update Delete
Consultation Consultation 40 Update Delete
Consultation Follow Up 40 Update Delete
This is my first query which generates the table
<cfquery name="categories" datasource="#dsn#">
select s.name, s.id as surveyid, rc.categoryname, rc.id as categoryid, sc.cweight
from survey_categories sc
join surveys s on s.id = sc.surveyidfk
join rating_categories rc on rc.id = sc.categoryidfk
where sc.surveyidfk='#form.survey#'
</cfquery>
This is the form I am accessing when I press "Update"
This form has an extra cfloop around the select tag to get the rest of the categories that I have in the database in case the user needs to change the category.
So, for example if I pressed the update button on the second row on my table this form should have Consultation in the drop down menu and the number 40 on the bottom textbox
A small note that may help, the first query that outputs the table also output a unique id with the pair (id, surveyName, Category, Weight). So the update query in the end would be something like
update categories set category='Example', weight='30'
where id='345'
I don't know how much this can help.
<cfoutput>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h3 id="myModalLabel">Update</h3>
</div>
<div class="modal-body">
<form name="update" action="updateSCpair.cfm" method="post">
<input type="text" value="#categories.name#" class="input-xlarge" disabled> <br />
<select name="categories">
<cfloop query="ratingCat">
<option value="#ratingCat.id#" >#ratingCat.categoryName#</option>
</cfloop>
</select>
<br />
<input class="span3" type="number" placeholder="Enter Category Weight" required >
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</form>
</div>
</cfoutput>
UPDATE
TO make it more clear because I think I wrote too much. I need to call a modal on form submittion. I will need to replace my current buttons with a form and then pass all the data through hidden variables. The problem is that this is not working for me. I found another example here but it doesn't seem to work. EXAMPLE

I think the simplest way is to have two forms at the end of each row. You already have the buttons. The rest can be hidden fields.
Your update form would have a target attribute to launch your popup. Since you already have the values from your query, you just submit them to the popup as hidden fields.
Your delete form would submit to the current coldfusion page. At the start of the page, you would have something like this:
<cfif StructKeyExists(form, "DeleteMeOrSomethingLikeThat")>
code to delete record
</cfif>
This will get you started. If you want to improve it later on, you can.
Finally, do one thing at a time.

Related

Bootstrap-Select not updating on mobile

I am using Bootstrap-select for multi-select items with Django. It works fine on desktop, but when the native mobile drop-down is enabled, the selected values of the dropdown do not populate.
HTML
<!-- start product brand info -->
<div class="row">
<div class="col-md-6 col-xs-*">
<div>
<label for="id_brand-name" class="form-label">Products</label>
</div>
<select multiple class="form-control selectpicker mb-3" id="id_brand-name" name="brand-name" mobile="true" multiple required>
{% for product in products %}
<option value="{{product.id}}" id="optiion">{{ product.brand.name | title }} - {{product.name | title}}</option>
{%endfor%}
</select>
<div class="invalid-feedback">
Select at least one product.
</div>
</div>
</div>
<!-- end product info -->
<script>
//Enble native drop down on mobile
window.onload = function () {
$('#id_brand-name').selectpicker({
container: 'body'
});
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
$('#id_brand-name').selectpicker('mobile')
}};
</script>
No matter how many items are selected, the selector always shows Nothing selected. When the data is posted, it is apart of the form though.
I like to use bootstrap-select, but this problem has been bothering me for a long time
Similar questions can be found on github, but no perfect answer.
The reason is that it will always refresh his title, no matter how remove() is done externally, and you can't change it's style on iphone Safari and Firefox.
So my solution is: if you can't remove it, then join it.
You have to change the original file: bootstrap-select.js
Search for: bs-title-option, next line you will find:
this.selectpicker.view.titleOption.value = '';
And add two lines down:
this.selectpicker.view.titleOption.value = '';
// new add
this.selectpicker.view.titleOption.disabled='true';
this.selectpicker.view.titleOption.textContent=this.options.title;
//
Done
When you use $('#id_brand-name').selectpicker('mobile')
The title text show on first empty option, and it can't be choose.
try it!

Creating a structure as a button in ColdFusion

First off, I'm not really sure how to title this question so I apologize if it's vague. I am trying to create a shopping list using ColdFusion and I've ran into a bit of a snag. I want a delete button to appear next to the item that's been created. I have almost everything working, but I don't understand structures enough in ColdFusion to understand what I am doing wrong. Is it similar to a component in React.js? I ran into an issue saying that the variable "button" is not defined. I'm assuming this is because structkeyExists can't identify a single button. Why would this work for form and not for a button?
Here is my code:
<cfif structKeyExists(form, "submitButt")>
<cfquery datasource="ESC-ADD-TECH">
INSERT INTO Main(itemDesc) VALUES('#itemDesc#')
</cfquery>
</cfif>
<cfif structKeyExists(button, "delete_butt")>
<cfquery datasource="ESC-ADD-TECH">
INSERT INTO Main(itemDesc) VALUES('#itemDesc#')
</cfquery>
</cfif>
<cfquery datasource="ESC-ADD-TECH" name="items">
DELETE FROM Main
WHERE itemDesc = '#itemDesc#'
</cfquery>
<body>
<div id="myDIV" class="header">
<h2>My Shopping List</h2>
<form method="POST">
<input type="text" name="itemDesc" placeholder="Title...">
<input name="submitButt" type="submit" class="addBtn">
</form>
</div>
<cfoutput query="items">
<li>#items.itemDesc# <button class="delete" name="delete_butt">x</button></li>
</cfoutput>
</body>
Is there a way to do what I am trying to do here using a structure? Am I better off creating the button in javascript and try to create a structure as a boolean statement and just have javascript rewrite that value? Kinda just shooting in the dark here, but I would appreciate any and all help.
Thank you everyone!
So there isn't going to be a "button" structure from your form being submitted.
The first thing to remember is that a ColdFusion structure is just a collection of key/value pairs (similar to a JavaScript object), and unless the value is set, will be undefined.
In your case, the "form" struct exists because you are submitting your page back to itself with your input[type="submit"]. Which for a ColdFusion page, will create a form struct with keys for each named input within the submitted form, the values of which are pulled from those elements' value attributes.
If you are trying to use the form struct to handle deleting items, you may be better served using radio buttons/checkboxes to select which item(s) to delete, and set the action to take using the value attribute of your submit buttons.
Using your code as an example:
<cfparam name="form.action" type="string" default="none">
<cfswitch expression="#form.action#">
<cfcase value="insert">
<!---Your insert query goes here--->
</cfcase>
<cfcase value="delete">
<!---Your delete query goes here--->
</cfcase>
<cfdefaultcase></cfdefaultcase>
</cfswitch>
<!---Your select query--->
<body>
<form method="post" action="#">
<div id="myDIV" class="header">
<h2>My Shopping List</h2>
<input type="text" name="itemDesc" placeholder="Title...">
<button type="submit" name="action" value="insert">Submit</button>
</div>
<ul>
<cfoutput query="items">
<li>#items.itemDesc#
<input type="radio" name="delDesc" value="#items.itemDesc#"/>
</li>
</cfoutput>
</ul>
<button type="submit" name="action" value="delete">Delete</button>
</form>
</body>
In this case you will use form.itemDesc when inserting values, and form.delDesc when deleting items.

Display data in HTML table or DataTable depending on radio button selection

I would like to give a user the option to display data in an HTML table or a DataTAble. I have a select dropdown that I am populating with a query output. After the user selects an option from the list they would select one of two radio buttons: (1) view HTML table, or (2) view DataTable. The user selects one of these radio buttons and then clicks on the submit button to display the results on a new page. The new page receives the form.value and inserts this into the query to produce the HTML table or DataTable.
I'm new to ColdFusion. It seems easier assign an action page to each radio button selection. I have made some attempt but nothing working yet. I'm guessing this may be a trivial task, but any help would be appreciated.
So on the form you would have your select dropdown and input
<select name="Species" class="dropdown">
<cfoutput query="species">
<option value="#species.Species#">#species.GenusSpecies#</option>
</cfoutput>
</select>
<input type="radio" name="Species" value="DataTable"> DataTable
<input type="radio" name="Species" value="HTML" checked="checked"> HTML Table
<input type="submit" value="Submit" class="buttons"></input>
How do I assign something to a radio button and then execute with submit? cfparam or cflocation??
Thanks.
in 1st cfm from your example you do
<body>
<select name="Species" class="dropdown">
<cfoutput query="species">
<option value="#species.Species#">#species.GenusSpecies#</option>
</cfoutput>
</select>
<form action="Species.cfm" name="mainform">
<input type="radio" name="Species" value="DataTable"/> DataTable
<input type="radio" name="Species" value="HTML" checked="checked"/> HTML Table
<input type="submit" value="Submit" class="buttons"/>
</form>
</body>
on a separate cfm. let's say species.cfm you'd do
<!---<cfdump var="#form#"> --->
<cfif form.species eq 'DataTable'>
display in datatables
<cfelse>
display in html
</cfif>

Get ID (not value) of selected ColdFusion radio button

I've created a quiz in ColdFusion with questions in one table and answers in another; it's multiple choice and each answer is presented as a member of a radio group. I'm looping through my recordset, rsAnswers, to output the radio group. I need to get the database ID of the selected radio button for an Insert operation and can't figure it out.
Here's how I'm outputting the question and answers:
<h3>Question #<cfoutput>#Session.theQuestion#</cfoutput></h3>
<h3><cfoutput>#rsQuestion.rrqQuestion#</cfoutput></h3>
<ol type="A" id="answerList">
<cfoutput query="rsAnswers">
<li>
<label>
<input type="radio" name="theAnswers" id=#rsAnswers.ID#" value="#rsAnswers.rraValue#" />
#rsAnswers.rraAnswer#</label>
</li>
</cfoutput>
</ol>
If I could retrieve the ID attribute of the selected radio button, I'd be fine, but I don't see a way to do that in CF. What am I missing?
TIA - Joe
When you post the form over, CF only gives you the value of the selected radio button from the form scope. If you need the ID, you should set it as the button value:
<input type="radio" name="theAnswers"
id=#rsAnswers.ID#" value="#rsAnswers.ID#">#rsAnswers.rraAnswer#</label>

Django form submit on dropdown selection rather than submit button

I have a table with lots of job data, and I have a dropdown menu and a submit button, which acts as a filter, so that the table only displays jobs based on the filter:
<form>
<select id="user_id_dropdown" name="user_id">
<option disabled selected>Filter by Username</option>
<option value="all">All Usernames</option>
<option disabled>────────────</option>
{% for user in users %}
<option value={{ user.id }}>{{ user.username }}</option>
{% endfor %}
</select>
<input id="filter" class="btn btn-primary" type="submit" value="Filter" />
</form>
<table>
...
How I've done it with the button is such that the user_id of the username is passed as a query string and my view handles it. Upon selecting a username (say it's user_id is 4) and clicking the submit button, the url is:
http://...jobs?user_id=4
Then I have a table below where all the jobs displayed are now only those created by user_id 4.
The thing is, now I just want to do away with the submit button and just submit the form on dropdown selection.
I've tried to give the form a name and to submit when there is a change on selection:
<form name='filter' method=POST>
<select id="user_id_dropdown" name="user_id" onChange="filter.submit();">
...
But this doesn't seem to work. It does seem like the page reloads (similar to the submit button), but the table data doesn't change. What am I missing out here?
I tried this:
onChange="form.submit();"
and it worked. Seems the name is not necessary.
Try putting this on your onchange attr:
document.filter.submit();
If this fails, give your form an ID attribute and do:
document.getElementById('youFormId').submit();
You could also send it as a GET paramenter like:
onchange="window.locatio.href+='?v='+this.value;"
By the way this question has little relation with Django, you should tag it html/javascript next time you ask about this kind of stuff.