How to page a table generated by an ajax call on runtime - java

I am generating a table depending on the user's selected choice by calling a servlet using AJAX call onchange of dropdown. The table is generating fine, but the problem is I want to page the table as well as it has a lot of rows. I have tried a lot of jquery plugins, but haven't been able to page the table as most plugins want table id which is only generated on ajax call.Please Help...!

Simply call the paging plugin in the success callback for the AJAX request, since at that point the table will exist and you should know the id of the table. If you don't know the id of the table at that point the easiest solution would be to modify the server-side code to pass that back as part of the response (as a property of a JSON object).
If that's not an option then you'll have to traverse the DOM in order to get a reference to the table, then get the id from that. I'd need to know the HTML structure of the page to tell you exactly how that would look.

You can use the this plugin
http://tablesorter.com/docs/
You can trigger the sorting externally by refering this url
http://tablesorter.com/docs/example-trigger-sort.html

Related

Sending data from table to servlet through AJAX via POST request

I have a JSP with a table and once the 'submit' button is pressed I would like to send all the information from the table to a servlet through AJAX via POST request.
I have set up the request and response via the servlet and it is tested and working for a few variables.
However, I would like to know, whats the best way to send a lot of information, i.e. information from the table? The table could have 30-40 rows with each row having three columns. I need to preserve the information for each row since each row is processed independently of the others.
While adding the rows you can create the tree kind of structure in the frontend. That could be some thing like this :
Each row will be an object and it has three attribute eg :
var rows_data = {firstName:"amit", lastName:"kumar", age:28};
Keep adding this rows into the array using javascript.
var rowArray= []; rowArray.push(rows_data);
While posting you can post this javascript array and from the servlet iterate the list and process the results.
Hope it helps to solve your problem.
I would like to know, whats the best way to send a lot of information through AJAX
You can use JSON or XML to send the table data that contains records one for each row. You can use various JSON or XML parsing library to form java object back from JSON or XML string and vice-verse.
You can use GSON and JSON library for this purpose.

Pass data from editable HTML table to servlet

So i have a JSP page, that contents editable table with checkbox in each row, the question is, how can i pass the data from selected rows to servlet?
Can i pass it as a parameter, or get html code in servlet to parse it and get selected data?
Thank you.
Your question needs some more specifics. But here is a perfect example of how to implement editable tables:
https://code.google.com/p/jquery-datatables-editable/
It is very easy to implement and basic knowledge of jQuery will help you play around as much as you like. It even shows examples of how to use data at Java servlets.
It depends on when you want pass the values to the servlet. If you want to make all the selections and then submit, a html form submit will be the best option. once you submit the form, the data will be available in request.getParameterValues('names'). If you want to send values immediately after user selects, then you can use ajax through an onchange event

Recommendations on Updating MySql database from Checkbox values in a HTML

I'm having a table with the contents retrieved from my MYSQL DB. Now, below the TH row I have a row for searching through the table column wise. I have shown a sample search through the table.
The user of this application might check some checkboxes after doing some search (and according to some rules that satisfies research requirements). Now, I would like to have a submit [update records] button below this table which on clicking updates the corresponding records - against which the checkbox has been selected to update the value of CheckBox column - "Accepted"in this case.
For you information, I'm using struts and so to do this I will have write a struts action class to handle this. I also considered AJAX to update this asynchronously in the back-end. I would like suggestions from people on the best practice to do this.
Given the stack you have described, you have a web application with Struts on the server. When a user chooses to update the records you can either post the data using a html form, or use Ajax calls. Irrespective of how the data is sent, you will have Struts code to process the information and update the database. The only difference is in how the data got there. It's up to you to decide which makes more sense.

How to dynamic edit a table using a JSP?

I'm trying to set delete buttons on each row of my table in order to let users to delete some rows of the table. However i'm having a terrible time to remove this rows dynamic and remove the data from database... ( no idea how to use the button onclick from html with jsp )
I decided to use some javascript inside the Jsp which did the tricky to remove the row but now i can't delete the entry from the database...
I would like to know if there is anyway to delete data from the database using the javascript and what should be the easiest way to do it ?
If you want to use JavaScript then you'll need to send an AJAX request to your server and then remove the row from the view upon success of the previous action.
For an easy and simple approach to AJAX you can try PrototypeJS. However, if you'd like to have really fancy dynamic features you may be more interested in jQuery.
Without knowing more from your project, I'd suggest to use a Servlet in the server side to receive the AJAX request.
I would like to use Ajax with div and InnerHTML. Onclick to the Remove button I will one javascript function which will make a call to server with unique id to delete the record from database , when we get response back I will hide innerHTML representing that row based on div Id.

Retaining previous form data

I have a form that I could put on to one page, but for aesthetic reasons, I want to split into two. The original form, on submission, would go to a Servlet which would get the form data and insert it into a database. However, I don't know how to make this work when it is split into two different forms on two different pages. My forms are currently in html but I could change them to JSP if that is the solution. I do not want to use hidden fields; if there is no way to do it without hidden fields, then I will just put it on one page as my form has quite a few fields and hidden fields would mean basically doubling the amount of code. I also only want to use html or jsp for the forms; I don't want to use JavaBeans, and I want to avoid scripting in the jsp's. I have already done this by simply dividing the database logic into two and using the ID of the last inserted object as a hidden field, and the second form then uses that ID to update that item, but this is not an elegant solution and could cause a problem if one user submitted the first form and a different user submitted the second (the wrong item would be updated). Is this possible?
Well, this is one of the things the Session can be used for: store the data from the first page in the HttpSession and then in the second page retrieve the session data and save it to the database.
You can use javascript pagination (It'll save page loading time. Many online test applications adopt this approach)
a) Either on submission on each part of form data will send to server using ajax call.(partial submission)
b) Or the whole data will be maintained at client side only until whole form is completed and submitted successfully.
(depends on your application need)
You can logically relate each part of form with some unique id & session id combination. In addition, if you are not willing/required to store whole form data in session, you can have primary key in session. It'll make database update process easier for 2nd or next part of form data.

Categories

Resources