1

I want to sort the table to be sorted by following order. I used JQuery table to display the JSON data. I tried using tablesorter plugin and I failed to configure it. Is there any way to sort this table.

  1. Failed
  2. Aborted
  3. Success

JSON Data

{
      "Product":"APIM",
      "Day01":"Success",
      "Day02":"Aborted",
      "Day03":"Failed",
      "Day04":"Failed",
      "Day05":"Failed",
      "Day06":"Failed",
      "Day07":"Success"
   },
   {
      "Product":"AppFactory",
      "Day01":"Aborted",
      "Day02":"Success",
      "Day03":"Success",
      "Day04":"Success",
      "Day05":"Success",
      "Day06":"Success",
      "Day07":"Success"
   },

Append Data (dataBind.js)

$.post("/portal/controllers/apis/test.jag", {
    action: "getData"
}, function(data) {

    var result = JSON.parse(data);
    console.log("----------------------------------start----------------------------");
    var Day = result[result.length - 1].Days;

    $("#tableid").append("<thead><tr><th> Product </th> <th >" + Day[0].substring(5, 11) + "</th> <th>" + Day[1].substring(5, 11) + "</th> <th>" + Day[2].substring(5, 11) + "</th><th>" + Day[3].substring(5, 11) + "</th><th>" + Day[4].substring(5, 11) + "</th><th>" + Day[5].substring(5, 11) + "</th><th> Current </th><th> Failed Components </th><th> Failed Duration </th></tr></thead>");

    for (var i = 0; i < result.length; i++) {
        $("#tableid").append("<tbody><tr><td>" + result[i].product + "</td><td><img src='images/" + result[i].Day01 + ".png' /></td><td><img src='images/" + result[i].Day02 + ".png' /><td><img src='images/" + result[i].Day03 + ".png' /></td><td><img src='images/" + result[i].Day04 + ".png' /></td><td><img src='images/" + result[i].Day05 + ".png' /></td><td><img src='images/" + result[i].Day06 + ".png' /></td><td><img src='images/" + result[i].Day07 + ".png' /></td><td>" + result[i].Failed.Component + "</td><td>" + result[i].Failed.Duration + "</td></tr></tbody>");

    }

    console.log("----------------------------------End Table----------------------------");
});

HTML

    <html>
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Build Data Table</title>
    
    <link href="css/style.css" rel="stylesheet" type="text/css">
    <script language="javascript" type="text/javascript" src="js/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="js/dataBind.js"></script>

    </head>
    
    <body style="height: 1100px;">
    <div class="CSSTableGenerator" >
    <table id = "tableid">
    
    </table>
    </div>
    
    </body>
    </html>
2
  • You have 7 days. How are you supposed to sort it by status for 7 days? Commented Dec 8, 2015 at 5:18
  • Oh! forgot to mention. I want to sort only the column of Day07 which is the last day. Commented Dec 8, 2015 at 5:27

1 Answer 1

1

Just apply Array.prototype.sort with a custom comparing function:

var sortOrder = ['Failed', 'Aborted', 'Success']; // <--- Look here

$.post("/portal/controllers/apis/test.jag", {
    action: "getData"
}, function(data) {
    var result = JSON.parse(data);
    var Day = result[result.length - 1].Days;

    /* Look here: */
    result = result.sort(function(a, b) { 
        return sortOrder.indexOf(a.Day07) - sortOrder.indexOf(b.Day07);
    });
    /* ---------- */

    $("#tableid").append("<thead><tr><th> Product </th> <th >" + Day[0].substring(5, 11) + "</th> <th>" + Day[1].substring(5, 11) + "</th> <th>" + Day[2].substring(5, 11) + "</th><th>" + Day[3].substring(5, 11) + "</th><th>" + Day[4].substring(5, 11) + "</th><th>" + Day[5].substring(5, 11) + "</th><th> Current </th><th> Failed Components </th><th> Failed Duration </th></tr></thead>");

    for (var i = 0; i < result.length; i++) {
        $("#tableid").append("<tbody><tr><td>" + result[i].product + "</td><td><img src='images/" + result[i].Day01 + ".png' /></td><td><img src='images/" + result[i].Day02 + ".png' /><td><img src='images/" + result[i].Day03 + ".png' /></td><td><img src='images/" + result[i].Day04 + ".png' /></td><td><img src='images/" + result[i].Day05 + ".png' /></td><td><img src='images/" + result[i].Day06 + ".png' /></td><td><img src='images/" + result[i].Day07 + ".png' /></td><td>" + result[i].Failed.Component + "</td><td>" + result[i].Failed.Duration + "</td></tr></tbody>");
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Yeldar for quick answer it saved my day. I was thinking to go for different table format where I can sort that column. One more thing, how can I sort data order of 1st Day07 column then Day06 column then Day05 likewise..
@ThilinaAkalanka It would be pretty much harder :) You can refer to this SO questions: stackoverflow.com/questions/2784230/… and stackoverflow.com/questions/6913512/….
github.com/Teun/thenBy.js This helped me to solve it. It's an improved solution of yours. Thanks again for your guidance much appreciated.

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.