Solved

how to use sortable.js

Posted on 2014-12-03
3
205 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 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

12 Experts available now in Live!

Get 1:1 Help Now