Solved

how to use sortable.js

Posted on 2014-12-03
3
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 52

Expert Comment

by:Huseyin KAHRAMAN
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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