0

I am making an application that receive data from servlet to jsp page. I have used List<> mechanism to store and retrieve data. So I have used one time a html design code and embed it into for loop that display data untill List<> data end. I want to sort data retrieved data on jsp page using java script but how to get value of that retrieved data in Java Script I don't know.

My JSP Code :

<div class="listinggitems">
      <%
            List<Integer> prdIDList = (List<Integer>)request.getAttribute("prodID");                    
            List<String> prdNAMEList = (List<String>)request.getAttribute("prodNAME");
            List<String> prdIMAGEList = (List<String>)request.getAttribute("prodIMAGE");
            List<Float> prdPRICEList = (List<Float>)request.getAttribute("prodPRICE");
            List<String> prdFEATUREList = (List<String>)request.getAttribute("prodFEATURE");

            for(int i = 0;i < prdIDList.size();i++)
            {

                Integer prdID = prdIDList.get(i);
                String prdNAME = prdNAMEList.get(i);
                String prdIMAGE = prdIMAGEList.get(i);
                Float prdPRICE = prdPRICEList.get(i);
                String prdFEATURE = prdFEATUREList.get(i);

      %>     
         <div class="mainitemlist">
          <div class="mainitemlistimage"><div align="center"><a href="product?pid=<%= prdID %>"> <img src="product_images/<%= prdIMAGE %>" height="125px" width="100px"></a></div></div>

            <div class="mainitemlistname">
              <div align="center"><a href="product?pid=<%= prdID %>"  style="color: #9caeb9;text-decoration: none;"><%= prdNAME %></a></div>
            </div>
            <div class="mainitemlistprice">
              <div align="center"><%= prdPRICE %></div>
            </div>
          <div class="mainitemlistfeatures"><div align="center"><%= prdFEATURE %></div></div>          
        </div>
       <%
            }
       %>

      </div>

I have taken 2 buttons: 1 for to sort data as per price, 2 for to sort data as per name.

When user click on button it calls Java Script Function to Sort data.

But how to get all data into Java Script for to sort I don't know.

Anyone will guide me, how to do that ?

Java Curious ღ
  • 3,622
  • 8
  • 39
  • 63
  • you can make ajax calls on button clicks and in response get the sorted list – Hussain Akhtar Wahid 'Ghouri' Oct 19 '13 at 12:14
  • @ BackSlash - Yes sir I don't have added Java Script Here. I have Just defined it but how to get all data into it I don't know so can't proceed forward. Will You tell me that how to get price value data into Java Script ? – Java Curious ღ Oct 19 '13 at 12:15
  • @Sniffer He knows that. In fact he is talking of javascript on a Java generated page – BackSlash Oct 19 '13 at 12:16
  • @Sniffer - I also know that JS != Java. I need the mechanism to fetch value of Java Code into Java Script. – Java Curious ღ Oct 19 '13 at 12:17
  • A possible way to achieve this (other than using AJAX - which is a good solution) is to convert the list to a JSON object (http://stackoverflow.com/questions/14228912/how-to-convert-list-to-json-in-java) send it to the client and then sort it (http://stackoverflow.com/questions/4222690/sorting-a-json-object-in-javascript) when the user clicks on a button. If I have time I will generate some code for you later. – Alex Theedom Oct 19 '13 at 17:47
  • @AlexTheedom - ok sir I ll try above link but if possible then tell me How to sort data in above code. – Java Curious ღ Oct 21 '13 at 06:49

1 Answers1

1

I strongly believe that the most appropriate solution to this issue is the use of AJAX as suggested by Hussain Akhtar Wahid failing that my suggestion to convert the product data to a JSON object and pass that to a JavaScript function would allow you to use mainly JavaScript. However if you must use nothing more than the request object and JavaScript then I have a solution for you.

In short, you must get the product data from the request object into a JavaScript object. This is possible but it not a pretty. Then once the product data is in the JavaScript object you can sort it using a custom sort function in JavaScript.

Here is your modified JSP code: ShowProduct.jsp

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
  <%@ page import="java.util.*"%>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  <html><head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Product Catalogue</title>
  <link rel="stylesheet" type="text/css" href="sortList/layout.css" />
  <script type="text/javascript" src="sortList/sort.js"></script>
  <script type="text/javascript">   
  //Puts the products into the product array of the catalogue object
    <%
        List<Integer> prdIDList = (List<Integer>) request.getAttribute("prodID");
        List<String> prdNAMEList = (List<String>) request.getAttribute("prodNAME");
        List<String> prdIMAGEList = (List<String>) request.getAttribute("prodIMAGE");
        List<Float> prdPRICEList = (List<Float>) request.getAttribute("prodPRICE");
        List<String> prdFEATUREList = (List<String>) request.getAttribute("prodFEATURE");

        for (int i = 0; i < prdIDList.size(); i++) {

            Integer prdID = prdIDList.get(i);
            String prdNAME = prdNAMEList.get(i);
            String prdIMAGE = prdIMAGEList.get(i);
            Float prdPRICE = prdPRICEList.get(i);
            String prdFEATURE = prdFEATUREList.get(i);
    %>      
    catalogue.product[<%=i%>] = {pId:<%=prdID%>, pImage:"<%=prdIMAGE%>", pName:"<%=prdNAME%>", pPrice:<%=prdPRICE%>, pFeature:"<%=prdFEATURE%>"};
    <%
        }
    %>
  </script></head>
  <body onload="catalogue.sortByName()">

  <button onclick="catalogue.sortById()">Sort By Id</button>
  <button onclick="catalogue.sortByName()">Sort By Name</button>
  <button onclick="catalogue.sortByPrice()">Sort By Price</button>

  <div id="container"><div id="mainitemlist"></div></div>

  </body></html>

A lot of changes to go over so I will be brief. Two major changes.

  1. Instead of displaying the product data immediately I cycle through the lists and put the data into a JavaScript array of objects. The array is call product and is a property of the catalogue literal object. See the JavaScript file below.

    catalogue.product[<%=i%>] = {pId:<%=prdID%>, pImage:"<%=prdIMAGE%>", pName:"<%=prdNAME%>", pPrice:<%=prdPRICE%>, pFeature:"<%=prdFEATURE%>"};

  2. I have created a div into which to insert the product data once it is sorted.

    <div id="container"><div id="mainitemlist"></div></div>

I have also created three buttons that sort the product data

 <button onclick="catalogue.sortById()">Sort By Id</button>
 <button onclick="catalogue.sortByName()">Sort By Name</button>
 <button onclick="catalogue.sortByPrice()">Sort By Price</button>

I have created a JavaScript file called sort.js which sorts and displays the product data.

// The catalogue literal object
var catalogue = {

sortDirection: -1, // The direction of the sort
product: [], // The product list generated by the JSP


// Sorts the products by their ID
sortById: function sortById() { 
    catalogue.product.sort(function(a, b) {
        return catalogue.sortDirection * (a.pId - b.pId);
    });
    catalogue.sortDirection *= -1;      
    catalogue.display();
},


// Sorts the products by their NAME
sortByName: function sortByName() {
    catalogue.product.sort(function(a, b) {
        var result = 0;
        var nameA = a.pName.toLowerCase(), nameB = b.pName.toLowerCase();
        if (nameA < nameB) {
            result = -1;
        } else if (nameA > nameB) {
            result = 1;
        } else {
            result = 0;
        }
        return catalogue.sortDirection*result;          
    });

    catalogue.sortDirection *= -1;  
    catalogue.display();
},


// Sorts the products by their PRICE
sortByPrice: function sortByPrice() {
    catalogue.product.sort(function(a, b) {
        return catalogue.sortDirection * (a.pPrice - b.pPrice);
    });
    catalogue.sortDirection *=  -1; 
    catalogue.display();
},


// Displays the sorted products
display: function display(){        

    var node = document.getElementById('container');
    while (node.hasChildNodes()) {
        node.removeChild(node.firstChild);
    }

    var html = "";

    for(var i = 0; i < catalogue.product.length; i++){              
        var tableRow = new catalogue.tableRow(catalogue.product[i]);  
        html += tableRow.generateHTML();    
    }

    document.getElementById('container').innerHTML = html;
},


// Contructor object for the table row
tableRow: function tableRow(product){

    this.id = product.pId;
    this.image = product.pImage;
    this.name = product.pName;
    this.price = product.pPrice;
    this.feature = product.pFeature;

    var image = "<div id='mainitemlist'><div id='mainitemlistimage'><a href='product?pid=prdID'><img src='product_images/prdIMAGE'></a></div>";
    var name = "<div id='mainitemlistname'><a href='product?pid=prdID'>prdNAME</a></div>";
    var price = "<div id='mainitemlistprice'>prdPRICE</div>";
    var features = "<div id='mainitemlistfeatures'>prdFEATURE</div></div>";


    this.generateHTML = function generateHTML(){
        html = "";      
        html += image.replace("prdIMAGE", this.image).replace("prdID", this.id);
        html += name.replace("prdNAME", this.name).replace("prdID", this.id);
        html += price.replace("prdPRICE", this.price);
        html += features.replace("prdFEATURE", this.feature);
        return html;        
    };

}

};

This script creates a catalogue literal object that contains all the properties and methods necessary to sort and display the product data.

There are three sort functions: sortById, sortByName and sortByPrice. Each implements a custom sort. I wont explain how custom sort works in as there are many article on the internet that explain it very well.

In order for the sort to be bidirectional (sorts alternate low to high on clicking the sort button) I use the sortDirection variable that alternates its value between 1 and -1. This controls the direction of the sort.

The display method produces the output to the screen by passing each product object in the product array to the constructor of the tableRow constructor object. Then by calling the generateHTML() method on this object the HTML for each row is generated.

This an example of the template that is used to generate the HTML:

var name = "<div id='mainitemlistname'>
            <a href='product?pid=prdID'>prdNAME</a></div>";

This method replaces the place holders prdID and prdName with the real values, obtained from the product and returns a string of HTML to the display method. Then this HTML in inserted into the ShowProduct DOM by setting the innerHTML property.

This JavaScript can be improved substantially nevertheless you have some code that gives you a rough solution to your issue. But I must reiterate that this is not the best way to tackle this issue, especially as you are using scriptlets which I am sure you know are taboo.

EDIT: Its missing the CSS, see below. Save it in a file called layout.css and import is in the HEAD elements of the HTML like so: <link rel="stylesheet" type="text/css" href="sortList/layout.css" />

div#mainitemlist{
    float: left;        
    width: 100%;    
}

div#mainitemlistimage {
    float: left;    
    width: 200px;   
}

div#mainitemlistimage img{
    height: 125px;
    width: 100px;
}


div#mainitemlistname{
    float: left;        
    width: 200px;   
}

div#mainitemlistname a{
    color: #9caeb9; 
    text-decoration: none;
}


div#mainitemlistprice{
    float: left;        
    width: 200px;   
}


div#mainitemlistfeatures{
    float: left;        
    width: 200px;   
}
Alex Theedom
  • 1,604
  • 15
  • 16
  • Simply awesome sir. Everything works perfectly. Problem is Just that it's not displayed in proper format otherwise it's works best. Any idea Sir why it's not display in proper format ? – Java Curious ღ Oct 22 '13 at 08:56
  • See my edit to the answer. The CSS is very basic so you will need to make whatever chances are necessary. Good luck. – Alex Theedom Oct 22 '13 at 09:11
  • Sir I have addded `CSS` code directly in Java Script `tableRow` function and it works so is it feasible to use that method to add `css` directly with control using `style` property. – Java Curious ღ Oct 22 '13 at 09:16
  • That will work. Although, it is considered better not to use inline css but to use classes or ids and define the CSS in an external file that you import into the HTML as I suggested above. This is for maintainability reasons. – Alex Theedom Oct 22 '13 at 09:26
  • Sir I have used external css in my project but it's not make affect in Java Script so that's why I have to use inline. External CSS why not working on it I don't know. – Java Curious ღ Oct 22 '13 at 09:36
  • When I am providing inline css in sort.js file in tableRow function where we have write html then it's work. – Java Curious ღ Oct 22 '13 at 10:03
  • Sir it's done. The problem is that you have used id with div while I have used class, that's why it's behaving like that. Thank you so much sir. – Java Curious ღ Oct 22 '13 at 10:35