0

I made a table that sums rows and columns of a table. But now I want to calculate the sum in Servlet.I'm sending the array of columns and rows with JSON to Servlet. Everything works ok. But the problem now is, How can I calculate the sum of rows and columns in servlet and send back?

This is my code:

  $(document).on('change',function(){

     var columnValues={}, rowValues={};

     $("#sum_table tr").each(function(rowIndex){
          $("td input", $(this)).each(function(colIndex){
            var value=$(this).val();
            // indexes need +1 to get the row number, because 
            // the indexes are 0-based.
            if (undefined===columnValues[colIndex+1]){
              columnValues[colIndex+1]=[];
            }
            if (undefined===rowValues[rowIndex+1]){
              rowValues[rowIndex+1]=[];
            }
            rowValues[rowIndex+1].push(value);
            columnValues[colIndex+1].push(value);

          });

      });
            // send data to server
       $.ajax({
           url: 'ServletPost',
           type: 'post',
           data: {rows:rowValues, columns:columnValues},
           dataType: 'json',
           success: function(data){
               // insert your server-calculated data to dom   
               var rows = data.rows,
                columns = data.columns;

                // insert your server-calculated data to dom   
                $("td.total").each(function(rowIndex){
                    $(this).text(rows[rowIndex+1]);
                });

                $("tr.totalCol td").each(function(columnIndex){
                    $(this).text(columns[columnIndex+1]);
                });
           }
       }); 
});

Thank You in Advance!

1 Answer 1

1

I will not tell exact solution but a way.

0>

$.ajax({
        url:"ServletPost",
        type:"POST",
        dataType:'json',
        data: {rows:rowValues, columns:columnValues},
        success:function(data){
            // codes....
        }

    });

1> In doPost() function in servlet

 String[] rows= request.getParameterValues("rows[]");
 String[] columns= request.getParameterValues("columns[]");

2> Find sum.

3> Return the Json Result

response.setContentType("application/json");
PrintWriter out = response.getWriter();
String jsonStr = "{\"rows\": \""+rowsResult+"\",\"columns\":\""+columnsResult+"\"}";
out.print(jsonStr);
out.flush();
Sign up to request clarification or add additional context in comments.

6 Comments

I got a problem with the Integer[] rows= (Integer[]) request.getParemeter("rows"); Integer[] columns= (Integer[]) request.getParemeter("columns"); "Cannot cast from String to Integer[]"
Tank You SeeThec, in rows and in columns I have to put getParameterValues or getParameter? and on jsonStr = "{\"rows\": \"+rowsResult+"\",\"columns\": \"+columnsResult+"\"}"; I write something wrong? Thank You!
@devtreat its a getParameterValues .
@devtreat Whats your 2nd question about jsonstr ? Could you please elaborate it.
try this "{\"rows\": \""+rowsResult+"\",\"columns\":\""+columnsResult+"\"}"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.