Search function in jsp dropdown spring MVC - java

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.

Related

How to pass variable from Java code to JSP and generate pop up of Yes and No and back to java on selection

I have a JSP and a Java Class. Upon moving from previous page, the Java code is called via Action Class and I have to check a value in Database(which is done). Now I want to pass that variable in JSP, if the variable is empty then I need to give user option to proceed or not.
If proceed - go to next JSP , else stop in same JSP.
My Problem is how to pass a variable to Java to JSP and upon suitable selection of option call back Java to complete the process.
On Click YES button you want to again call Action method.
You can create simple java-script function and provide Ajax call to your appropriate action name.
This javascript function call,onClick event of your jsp button.
Example
function actionCall() {
$.ajax({
type: "POST",
data: params,
url: 'Action url'+'Action Name',
error: function(o) {
//error code
},
success: function(data) {
//Success code
}
});
}

Autofill a form based on DB content

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.

How to select the particular row data in jsp using radio button

I know its a basic question and please bear with this.
I'm new to struts frame work and I'm creating a sample application using struts 1.2. I've inserted few values into the database using form beans. And i'm displaying all the database records in the jsp page using function. and i also placed the radio button for each row..
Now my question is, i want to select a particular data from the records displayed in the jsp page by clicking the radio button and i need to store the selected records into the another table.
Give me some suggestion regarding the condition specified above...
Thanks in advance..
Once you set those unique values in radio button, you can get the values for your purpose. Like using jquery,
var selRadio = $('input:radio[name=cname]:checked').val();
You can find out fiddle explains how to get checked value.
Use some ajax for send that value to server (servlet) and add the values to new table without refresh entire page.
$.ajax({
url : "ur_servlet_url" + selRadio,
type : "POST",
async : false,
success : function(data) {
alert("success");
}
});
Let me know if this helps.

Display a loading image as long as a jquery function is executing

I m working on a Spring MVC java web application . The app contains a JSP page , on it I have a table in which I display data from the DB. The data is diplayed in text inputs which belong to the class .editable
I implemented a script that does the following :
-select the text inputs from the table ,
-create a span with the data from the text inputs
-hides the text input
function editableInput() {
$('.editable').each(function() {
var input = $(this);
input.before("<span class='spanEditable'>" + input.val() + "</span>");
input.hide();
});
}
I later use a script to display the text input for a row , when the row is selected from a radio input.
All of this for making a good looking table that permits CRUD operations , on a row , when the row is selected , when no row is selected , it displays all the table content like plain text , not using a text input.
The problem i am facing : When the page is loading it displays all the table rows like it is initially made , with all the data in text inputs , after the script is being completed all the data is displayed in the spans. This looks really bad , until all the stuff is loaded.
How can i use a loading gif , until the function editableInput() is completed ?
If i would use a AJAX call i would know how to add the loading gif , but this is how i`m calling the function :
$(document).ready(function() {
editableInput(); )};
You may experiment with jQuery BlockUI Plugin (v2)
Well you can try this one, though rather crude but will work. On loading the page wrap your table into another div using:
$("table#table1").wrap("<div id='dvWrapp' style='background-color:#000;'/>");
then after completing the editableInput call just unwrap it:
$("table#table1").unwrap();
You can set style for the wrapper div to show loading image or some message also.
Please try this example: http://jsfiddle.net/amantur/FkjkR/

Sending the selected rows from datatable to action method?

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.

Categories

Resources