How to dynamic edit a table using a JSP? - java

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.

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 page a table generated by an ajax call on runtime

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

JSP delete row and save

I am working in Spring MVC 2, Jsp, Dojo, Javascript.
Actually I am populating Jsp page table-grid with list of objects coming in form command object. Let say 3 records displayed in grid. I am deleting third record with JavaScript getElementById.. delete-row/removeChild functions. That record is deleted from presentation i.e. grid. Now when I save this. It takes 3 records to server side instead of 2. It should take 2 records because the third record was deleted. I am using Dojo to dragNdrop grid rows.
If you're using a grid component that maintains a datastore - e.g. the DojoX DataGrid, you might be removing the markup for the row, but not telling the datastore to purge the row data. When the save occurs, the datastore sends all three rows.
If you are using the DataGrid, you should delete the row from the DataStore, which will be reflected automatically in the UI.
When I have this kind of issue, I always check the cache related headers in my response.
Could it be that the http request supposed to fetch saved data from the server in order to refresh the view doesn't hit the server, but instead hit the browser cache?
Could not resolve issue but another logic fulfills my need. Spring form tags were used to bind this for with objectclass. Converting deleted item row's id to negative and hiding this row at client side does the trick. When form submits this negative id converted to positive value and deleted from DB.

Categories

Resources