?
Solved

how to use sortable.js

Posted on 2014-12-03
3
Medium Priority
?
232 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 2000 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 57

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

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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 Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
Suggested Courses

752 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