Solved

how to use sortable.js

Posted on 2014-12-03
3
214 Views
Last Modified: 2014-12-04
Hey Experts. I have a table I am generating through Javascript from a JSON string and am printing it out into a HTML table, what I am trying to figure out is how I can sort the table. I tried a few different things and downloaded the library and css files here. sortable.js  Here is the link to the test page I am working on this at. test  Basically I want to be able to sort this table by price, ascending or descending. Though if I could sort by clicking the column heading that would be good also.  Here is the code I am working with. It seems to be picking up the CSS and the headers look like they are clickable but I can not click them to sort the columns. I did find some places that suggested using Sortable.init() with this but I do not understand how it is related. If anyone understands what I am missing here please let me know, thanks.

 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>JSON Sample</title>
        <link rel="stylesheet" href="sortable/css/sortable-theme-bootstrap.css" />
</head>
<body>
    <div id="placeholder"></div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" ></script>    
    <script src="sortable/js/sortable.min.js"></script>

ISBN: <input type="text" id="field1" value="047029437X"><br>

<button onclick="myFunction()">Submit</button>
<h2>JSON Object Creation in JavaScript</h2>

<p id="demo"></p>
<table id="demo2" class="sortable-theme-dark" data-sortable></table>
<pre id="json"></pre>

<script>
function myFunction() {
    var x = "http://www.jtsbooks.com/more/buybook.php?isbn=";
var y = document.getElementById("field1").value
    
    $.getJSON(x + y,function(result){
      $.each(result, function(i, field){
      });
text = "";
var t;
      console.log(result); 
var bookinfo = result.response.page 

  document.getElementById("json").innerHTML = JSON.stringify(bookinfo, undefined, 2);
for (t = 0; t < bookinfo.offers.condition.length; t++) {
  //  text += bookinfo.offers.condition[t]["@attributes"].name + "<br>";
    for (p = 0; p < bookinfo.offers.condition[t].offer.length; p++) {
    	text += "<tr><td>" + bookinfo.offers.condition[t].offer[p].isbn13 + "</td>" +
    	"<td>" + bookinfo.offers.condition[t].offer[p].isbn10 + "</td>" +
    	"<td>" + bookinfo.offers.condition[t].offer[p].merchant_name + "</td>" +
    	"<td>" + bookinfo.offers.condition[t].offer[p].price + "</td>" +
    	"<td>" + bookinfo.offers.condition[t]["@attributes"].name + "</td></tr>";
    	}; 
};
document.getElementById("demo2").innerHTML = "<thead><tr><th>ISBN13</th><th>ISBN10</th><th>Merchant Name</th><th>Price</th><th>Condition</th></tr></thead><tbody>" + text + 
"</tbody>";

  });
}
</script>
</body>
</html>

Open in new window

0
Comment
Question by:jcnelson84
3 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40479603
After you have got the json and built the table you need to call

Sortable.init()

Open in new window

0
 
LVL 51

Expert Comment

by:HainKurt
ID: 40479681
are you using sortable anywhere?

$( "#demo2" ).sortable();

after building your table?
0
 

Author Closing Comment

by:jcnelson84
ID: 40480804
Thanks, It worked to put Sortable.init() after the table. I had added it to the code before but I must have had it in the wrong place so it did not work. Thanks for the clarification.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now