I have written the following code that displays a table of 4 columns on ajax success. I wish to sort this table based on the 4 columns. I tried doing it using some readymade .js files available online, but didn't succeed. How can I achieve the requirement?
function Success(data)
{
var obj;
obj=JSON.parse(data);
document.write("<html><head><title>Browser Service</title></head>");
**document.write("<script type='text/javascript', src='sorttable.js'>
<\/script>");**
document.write("<body bgcolor=#E0DCDC>");
document.write("<div id=example-table>");
document.write("<h2 align=center>Browser Version Details</h2>");
document.write("<table class='**sortable**', border-collapse=collapse,
width=60%, border=1px solid #ddd, align=center>");
document.write("<tr bgcolor=#209D9D>");
document.write("<th height=50>");
document.write("Name");
document.write("</th>");
document.write("<th height=50>");
document.write("Number1");
document.write("</th>");
document.write("<th height=50>");
document.write("Number2");
document.write("</th>");
document.write("<th height=50>");
document.write("Number3");
document.write("</th>");
document.write("</tr>");
document.write("<br>");
for(i=0; i<obj.length; i=i+4)
{
document.write("<tr bgcolor=#ffffff>");
document.write("<td align=center>");
document.write(obj[i]);
document.write("</td>");
document.write("<td align=center>");
document.write(obj[i+1]);
document.write("</td>");
document.write("<td align=center>");
document.write(obj[i+2]);
document.write("</td>");
document.write("<td align=center>");
document.write(obj[i+3]);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
document.write("</div>");
document.write("</body></html>");
}
</script>
I have added a readily available sorttable.js file to achieve the purpose.
I followed the link: https://www.kryogenix.org/code/browser/sorttable/
I felt like helping you out because I remember trying to create markup for tables and it can be tedious.
Some pointers:
thelements so that when you click on different columns, the data is sorted and then the markup is remade via a function. Note that because the markup changes, you then have to reattach the event listeners to thethelements so that you can sort by another column if you like.I put some random data in for illustrative purposes.