Javascript排序错误与排序功能可能简单修复

时间:2015-02-11 13:43:45

标签: javascript sorting

可能是一个简单的修复,表格的其余部分在此处排序 (在顶部) picture of table problem 该表将按正确的顺序一次又一次地工作(来自源的数据不会改变)但是当它有或没有时真的很冒险

想法?

萨姆

继承代码

//first define a function
var sortTable = function() {
    $("#tableid tbody tr").detach().sort(function(a, b) {
        var dataA = $(a).find("td:eq(3)").text().trim();
        var dataB = $(b).find("td:eq(3)").text().trim();
        return parseFloat(dataA.substring(1)) - parseFloat(dataB.substring(
            1));
    }).appendTo('#tableid');
};
//include two files where rows are loaded
//1.js
$.ajax({
    type: 'GET',
    crossDomain: true,
    dataType: 'json',
    url: 'url1',
    success: function(json) {
        //var json = $.parseJSON(data);
        for (var i = 0; i < json.results.length; i++) {
            var section = json.results[i].section;
            var no = json.results[i].avalible;
            var price = json.results[i].price;
            var button =
                "<button class='redirect-button' data-url='LINK'>Compare</button>";
            $("#tableid").append("<tr  ><td>" + section +
                "</td><td>" + no + "</td><td>" + price +
                "</td><td>" + button + "</td></tr>");
            $("#tableid").find(".redirect-button").click(function() {
                location.href = $(this).attr("data-url");
            });
        }
        sortTable();
    },
    error: function(error) {
        console.log(error);
    }
});
//and here is the 2nd js file
$.ajax({
    type: 'GET',
    crossDomain: true,
    dataType: 'json',
    url: 'url2',
    success: function(json) {
        //var json = $.parseJSON(data);
        for (var i = 0; i < json.results.length; i++) {
            var section = json.results[i].section;
            var no = json.results[i].avalible;
            var price = json.results[i].price;
            var button =
                "<button class='redirect-button' data-url='LINK'>Click Here</button>";
            $("#tableid").append("<tr><td>" + section +
                "</td><td>" + no + "</td><td>" + price +
                "</td><td>" + button + "</td></tr>");
            $("#tableid").find(".redirect-button").click(function() {
                location.href = $(this).attr("data-url");
            });
        }
        sortTable();
    },
    error: function(error) {
        console.log(error);
    }
});

1 个答案:

答案 0 :(得分:0)

关于空格的排序,但也链接到你的jquery选择器。

通常,数组是基于Jquery的0索引,然后如果你想按照你的价格排序2nd column (0-index based),你需要做如下:

var sortTable = function() {
    $("#tableid tbody tr").detach().sort(function(a, b) {
        var dataA = $(a).find("td:eq(2)").text().replace(/\s/g, "");
        var dataB = $(b).find("td:eq(2)").text().replace(/\s/g, "");
        return parseFloat(dataA.substring(1)) - parseFloat(dataB.substring(
            1));
    }).appendTo('#tableid');
};

var json = {results:[{price:"$12 .45"}, {price:"$13 .45"}, {price:"$12 .05"}, ]}
for (var i = 0; i < json.results.length; i++) {
  var section = json.results[i].section;
  var no = json.results[i].avalible;
  var price = json.results[i].price;
  $("#tableid").append("<tr ><td>section</td><td>no</td><td>" + price);
  
}
sortTable();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tableid"></table>

`