Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to use sortable.js

Posted on 2014-12-03
3
Medium Priority
?
236 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 60

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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

636 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