i am using data table to show the customer records on jsp page where i show customer name,last name,department,joining date in data table.
i have one more column which is select box. now user can user select any number of customer records from ui and send to action on server side for update. In my
jquery function i get the selected rows in data table. But i am not able to figure out how to send the these rows to action method?
what i am expecting is i send these rows from ajax to action method which has one method parameter as list(representing the selected rows) and i can extract field values from each row in list representing selected rows from UI.
But i am unsure what will be type of object that list will contain?Can someone point me in right direction with some brief example ?
Why dont you give a checkbox in each row whose value will be the id of the corresponding record
<input type="checkbox" name="rec" value="{id of the record}"/>
Then when you submit get the selected ids
var selectedRec="";
$("input[name='rec']:checked").each(function(){
selectedRec += $(this).val()+",";
});
you can now pass selectedRec to the action as a string variable. In your action you can split this string as array
String[] recArray = selectedRec.split(",");
Then in the loop you can get each id as follows
int customerId = Integer.parseInt(recArray[i]);
This is another way to do, where you don't need to iterate your selected record in JQuery and create a string in Script.
Step 1. Create checkbox
<s:checkbox name="Bean.record" id="record" fieldValue="%{id of the record}" />
Step 2. Create String[] in your Bean class
private String[] record;
That's all.... so whatever record you will select on your JSP, value will be added into String Array and you can grab it on your action.
Hope this will help.
Related
hello guys please I am trying to select multiple data from the database with JSP and MySQL based on user selection in a drop-down in the picture below when the user selects the value from the dropdown that value will be added to the select condition and if it's not selected it will be eliminated this is my query but it's not working this is my query
sql="select * from copform where marital='"+marital+"' or mtype='"+memT+"' or empStatus='"+empS+"' or edul='"+edul+"' or occu='"+occu+"' or wbap='"+bab+"' or hbap='"+bab+"' and assembly='"+assembly+"' and gender='Female'";
and the dropdown form is in the picture below
user selection dropdown form
so in case the user selects 3 values then how do I fetch data for only those 3 values
If you're passing marital as a string(VARCAHR) value, you don't have to again give it inside a single quotation mark. That might be the issue.
sql="select * from copform where marital="+marital+"
use above instead,
sql="select * from copform where marital='"+marital+"'
I want to autofill a form value based on value I have selected before. When I select a product id and all of my product info should be filled. How to implement this? I want to auto complete product as well. I am using JSP but the problem is that I can get product id and I can get value in JS variable. So how can I assign JS to JSP variable and fetch content from DB.
Let me explain clearly what i am doing and what i want to achieve. I have jsp page, which holds two drop downs along with search button and a table with four columns. Two of the table columns data is same as drop downs data.
Initially when the jsp loads, these dropdowns set to ALL and by defaults all the results are showed in jsp table with single query fire to DB.
What i want is when a particular item is selected in both the dropdowns. I would like to display the results related to this search in table.
Do i need to fire a query for every search? because i have already got the total data at the beginning.
Any help would be appreciated?
Once you get all the data from DB
Store the values in a Pojo
Once your drop down is filled and pressed search button
Go for a ajax call and pass your drop down select to your controller
Filter your new ArrayList\Object based on your drop down select and update
your object which holds the value needed to populate your table
Populate your table with the updated object upon returning to your jsp.
Ajax Code
$.ajax({
url:'filterTableData.do',
cache:false,
type: 'POST',
dataType: 'text',
data: ({'dropdownValue1' : dropdownValue1 ,'dropdownValue2' : dropdownValue2 }),
error: function (res, textStatus, errorThrown) {
alert(' Error :' + errorThrown);
},
success: function(res){
window.location.replace("jsptobedisplayed.do");
}
});
Java code -
#RequestMapping("/filterTableData")
#ResponseBody
public String deleteProd(#RequestParam String dropdownValue1,#RequestParam String dropdownValue2){
----> your logic <--------}
Alternate was is once you press search , then go back to java and do the filter based on the drop down value and return the updated table object and populate the table.
I want to get all the values of a select in a Servlet even if the values are not selected. What should I do?
I know that I can fetch all the selected values using request.getParameterValues(" "), but I want all the values as they are filled at the run time.
I have two Select and at runtime the user can insert options from one select to another one. Now on submit of the form I want to access all the values present in the second select.
The first Select is getting values from a database, not from any other servlet, and I want to access only a few of them which are inserted in second select by user
String nonconti = request.getParameter("non-conti");
if(nonconti.equals("yes")){
String[] elLeaves = request.getParameterValues("addELDate");
cal = Calendar.getInstance();
for(int i=0; i<elLeaves.length;i++){
try {
if(!(elLeaves[i] == null || elLeaves[i].trim().equals("") || elDateList.contains(elLeaves[i]))){
cal.setTime( formater1.parse(elLeaves[i]));
if(!((cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))){
elDateList.add(formater1.format(formater1.parse(elLeaves[i])));
}
}
}
Its not possible to get the value which is not selected in the select element.
But yes there are some way around.
You can store the value in session when you send it to display on the
page and retrieve it upon submitting the form
Just keep all the values of select in any hidden input as comma
separted and get it on servlet and then split it and make array or in
whatever form you want.
form submit will send only selected Values to your servlet.
UPDATE: due to question update
also if user select an item from the first , you add this item to the second
your function thats adding selected Item to second, could add these selected items as hidden fields to your form like:
<input type="hidden" name="insertedItems[]" value="value of your new added options" />
in your servlet you get these options like:
String[] options = request.getParameterValues("insertedItems");
I have two dropdowns in html. Both dropdowns are getting data mysql first dropdown is "hostgroup" having value like "windows,linux" and second dropdown is "host" having value like"office,home,sidearea,localhost"
Basically I want to do it so when I select "linux" is hostgroup combobox it will filter "host" dropdown box and show only localhost in "host" dropdown and when I select "windows" in hostgroup dropdown it will filter it and remove localhost from host dropdown
My code for filling dropdown in html is,
html.append("<select id='hosts' name='hosts' style='width: 180px' onchange=\"document.forms['form1'].submit();\">>");
//if(rs != null)
//{
while(rshostgroup.next())
{
html.append("<option value='"+rshostgroup.getString(2)+"'>"+rshostgroup.getString(1)+"</option>");
//html.append("<option value='web'>web</option>");
}
//}
html.append("<select>");
html.append("</td>");
html.append("</tr>");
html.append("<tr>");
html.append("<td>");
html.append("Host");
html.append("</td>");
html.append("<td>");
html.append("<select id='hosts' name='hosts' style='width: 180px' onchange=\"document.forms['form1'].submit();\">>");
//if(rs != null)
//{
while(rshost.next())
{
html.append("<option value='"+rshost.getString(2)+"'>"+rshost.getString(1)+"</option>");
//html.append("<option value='web'>web</option>");
}
//}
html.append("<select>");
Please dont get confuse that what html.append is and what is option value comming from
Actually I am using string builder appending my string builder html in .java(class) file and calling class html appended method in jsp. It is working fine and the option value of drop down is filling from ResultSet rs and rshost having data from mysql.
Firstly using java to build HTML element is a terribly bad idea.
Use JSTL to loop through the dropdown options instead of using Java to spit out HTML code.
Secondly the problem you mention has nothing to do with Java and everything to do with JavaScript.
Well, not entirely true as you might have to make an AJAX request to server get values to populate your secondary dropdown box.
What you need to do is attach a onChange listener for the primary dropdown so that when it changes your javascript code either makes a AJAX call to server to get values for your secondary dropdown box, or use hardcoded values held in some javascript variables.
You will have to loop through the secondary dropdown to remove all previous values and add new dropdown options to it or alternately you can remove the secondary dropdown box entirely and recreate it with the new values based on the selection of the primary dropdown box.
There's already more than enough articles in the internet so use your fiend Google to find the ones that suits your needs closely.