I am planning to implement server-side sorting and paging for a data table to be shown on the webpage. The data table is a Javascript managed HTML table with external CSS. Data will be pulled from the server-side by Ajax. I am thinking about creating a class on the server-side to represent the data table but have no idea how to implement the sorting on different columns. The sorting should be general enough for any class that backs up the table.
Edit: a backup class such as this:
public class Inventory
{
private int itemsLeft = 0;
private float price = 0.0f;
private boolean status = false;
private int itemsSold = 0;
public int getItemsSold()
{
return itemsSold;
}
public void setItemsSold(int itemsSold)
{
this.itemsSold = itemsSold;
}
//... and other getters and setters
}
Each of the private field will be one of the column for the data table and the whole table will be represented as an ArrayList of Inventory. Each instance of the back up class consists of one database table row.
When the header of any sortable column is clicked, the index or name of the column will be sent to the server for sorting data according to the selected column.
I am now stuck at how to make a general sorting function for any back up class. Your suggestions will be much appreciated.
Thanks to Amir and cuberoot, after some work, I finally come up with this:
This can be used as:
Here field_name is the name of the field to be sorted on. It is sent to the server as a request parameter when the header of the table is clicked. There is a requirement with this approach: all the sortable fields in the domain object must implement Comparable interface. Also there must be JavaBean style getter and setter methods for the fields of interests.