2017-06-06 4 views
0

特定のボタンがクリックされたときにテーブルの列の値を並べ替えようとしています。私はまず、あらかじめ定義された値でテーブルをソートする方法を理解したいと思います(テーブルに実際の値で2行含まれています)。第二に、私はテーブル内の事前定義された値を並べ替えることが終わった後です。ランダムに任意の値や文字を生成できる「行の作成」ボタンを追加できます。それでも、ボタンをクリックすると、値がソートされます。 私のコードが壊れていて、なぜそれがわからないのですか?プラグインなしのjqueryを使用してテーブルの列を並べ替える

私は立ち往生しており、多くの助けを見つけることができません!事前にみんなありがとう!あなたの最初の行で

//add row function 
 
$(document).ready(function() { 
 
    $("#add_row").click(function() { 
 
    event.preventDefault(); 
 
    var new1 = $("<tr><td>6</td><td>1</td><td><input id ='dr' class='btn btn-danger' type ='button' value= 'delete'></td></tr>").on('click', function() { 
 
     $(this).find('td').toggleClass('highlighted') 
 
    }); 
 
    $('#myTable').append(new1); 
 
    }); 
 

 
    //delete function 
 
    $('#myTable').on('click', '#dr', function() { 
 
    $(this).parent().parent().remove(); 
 
    }); 
 

 

 
    //sort function 
 
    $('#sort1,#sort2').on('click', function() { 
 
    var n1 = $(this).index(); 
 
    console.log(n1); 
 
    var n2 = $('#myTable').find('tbody >tr:has(td)').get(); 
 
    console.log(n2); 
 
    n1 += 1; 
 
    n2.sort(function(a, b) { 
 
     var vA = $(a).children(':nth-child(' + n1 + ')').text(); 
 
     var vB = $(b).children(':nth-child(' + n1 + ')').text(); 
 
     if (vA < vB) { 
 
     return -1; 
 
     } else if (vA > vB) { 
 
     return 1; 
 
     } else { 
 
     return 0 
 
     }; 
 
    }); 
 

 
    $.each(n2, function(index, row) { 
 
     $('tbody').append(row); 
 
    }); 
 
    }); 
 
});
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <title>create table</title> 
 
    <meta charset="utf-8"> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 

 
    <style type="text/css"> 
 
    table, 
 
    td { 
 
     border: 1px solid black; 
 
    } 
 
    
 
    .highlighted { 
 
     background-color: yellow; 
 
    } 
 
    </style> 
 

 
</head> 
 

 
<body> 
 
    <div class="container"> 
 
    <p>Click the buttons to create and delete row(s) for the table.</p> 
 

 
    <table id="myTable" class="table" style="width: 80%"> 
 
     <tbody> 
 
     <tr> 
 
      <td>Name<button type="button" id="sort1" class="btn btn-default btn-xs pull-right glyphicon glyphicon-triangle-bottom"></button></td> 
 
      <td>Value<button type="button" id="sort2" class="btn btn-default btn-xs pull-right glyphicon glyphicon-triangle-bottom"></button></td> 
 
      <td>Deletable</td> 
 
     </tr> 
 
     <tr> 
 
      <td>3</td> 
 
      <td>4</td> 
 
      <td>5</td> 
 
     </tr> 
 
     <tr> 
 
      <td>1</td> 
 
      <td>2</td> 
 
      <td>3</td> 
 
     </tr> 
 
    </table> 
 
    <br> 
 
    <button type="button" id="add_row" class="btn btn-primary">Create row</button> 
 
    </div> 
 
</body>

+0

それが正常に動作します。この[解答](https://stackoverflow.com/questions/664802/jquery-sorting-a-table-without-the-plugin)を参照してください。 –

+1

@Haze、私はその前にその答えを見ましたが、私はそれが過度に複雑であると思っています、そして、それはjqueryでなくjavascriptに大きく焦点を当てています。しかし、あなたの入力にも感謝!! –

答えて

1

あなたは<th>秒に<td>タグを変更した場合、彼らがどこにあるか、その後ソートはそれらを残すだろう。

<th>Name<button type="button" id="sort1" class="btn btn-default btn-xs pull-right glyphicon glyphicon-triangle-bottom"></button></th> 
<th>Value<button type="button" id="sort2" class="btn btn-default btn-xs pull-right glyphicon glyphicon-triangle-bottom"></button></th> 
<th>Deletable</th> 

そして、あなたのコードでは、必ずボタンのインデックスではなく、列のある$(this)のインデックスを取得クリックしたボタンのインデックスを取得。だから、

var n1 = $(this).closest("th").index();

にその行を変更してみてください、それが動作するはずです。

//add row function 
 
$(document).ready(function() { 
 
    $("#add_row").click(function() { 
 
    event.preventDefault(); 
 
    var new1 = $("<tr><td>6</td><td>1</td><td><input id ='dr' class='btn btn-danger' type ='button' value= 'delete'></td></tr>").on('click', function() { 
 
     $(this).find('td').toggleClass('highlighted'); 
 
    }); 
 
    $('#myTable').append(new1); 
 
    }); 
 

 
    //delete function 
 
    $('#myTable').on('click', '#dr', function() { 
 
    $(this).parent().parent().remove(); 
 
    }); 
 

 

 
    //sort function 
 
    $('#sort1,#sort2').on('click', function() { 
 
    var n1 = $(this).closest("th").index() + 1; 
 
    console.log(n1 - 1); 
 
    var n2 = $('#myTable').find('tbody >tr:has(td)').get(); 
 
    console.log(n2); 
 
    n2.sort(function(a, b) { 
 
     var vA = $(a).children(':nth-child(' + n1 + ')').text(); 
 
     var vB = $(b).children(':nth-child(' + n1 + ')').text(); 
 
     if (vA < vB) return -1; 
 
     if (vA > vB) return 1; 
 
     return 0; 
 
    }); 
 

 
    $.each(n2, function(index, row) { 
 
     $('tbody').append(row); 
 
    }); 
 
    }); 
 
});
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <title>create table</title> 
 
    <meta charset="utf-8"> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 

 
    <style type="text/css"> 
 
    table, 
 
    td { 
 
     border: 1px solid black; 
 
    } 
 
    
 
    .highlighted { 
 
     background-color: yellow; 
 
    } 
 
    </style> 
 

 
</head> 
 

 
<body> 
 
    <div class="container"> 
 
    <p>Click the buttons to create and delete row(s) for the table.</p> 
 

 
    <table id="myTable" class="table" style="width: 80%"> 
 
     <tbody> 
 
     <tr> 
 
      <th>Name<button type="button" id="sort1" class="btn btn-default btn-xs pull-right glyphicon glyphicon-triangle-bottom"></button></th> 
 
      <th>Value<button type="button" id="sort2" class="btn btn-default btn-xs pull-right glyphicon glyphicon-triangle-bottom"></button></th> 
 
      <th>Deletable</th> 
 
     </tr> 
 
     <tr> 
 
      <td>3</td> 
 
      <td>4</td> 
 
      <td>5</td> 
 
     </tr> 
 
     <tr> 
 
      <td>1</td> 
 
      <td>2</td> 
 
      <td>3</td> 
 
     </tr> 
 
    </table> 
 
    <br> 
 
    <button type="button" id="add_row" class="btn btn-primary">Create row</button> 
 
    </div> 
 
</body>

+0

うわー。出来た!!ありがとう! –

+0

@ YeWangあなたの問題を解決した場合は、回答を受け入れたものとみなしてください。 – Botimoo

関連する問題