【问题标题】:Jquery Datatable not reordering my json properlyJquery Datatable 没有正确地重新排序我的 json
【发布时间】:2017-03-01 17:44:11
【问题描述】:

我正在使用:jquery.dataTables.js 来自:https://datatables.net

我希望我的 json 像现在一样显示,但是当在页面中显示时,显示乱序,我希望 json 显示如下:1,2,3,4,5 或者只是顺序是现在数字无关紧要,只要跟随 json。

所以在我的数据表中应该显示如下:eua,china,spain,spain,china。

这是我的奔跑:http://plnkr.co/edit/mQIpriwqOhB59QPYbKHj?p=preview

我有这个 json 文件:

{
  "data": [
    {
      "order": 1, 
      "place": "EUA", 
      "name": "Cannon Morin", 
      "delete": " <i class=\"fa fa-pencil-square\" aria-hidden=\"true\"></i> <i class=\"fa fa-minus-square\" aria-hidden=\"true\"></i>"
    }, 
    {
      "order": 2, 
      "place": "China", 
      "name": "Neva Allison", 
      "delete": " <i class=\"fa fa-pencil-square\" aria-hidden=\"true\"></i> <i class=\"fa fa-minus-square\" aria-hidden=\"true\"></i>"
    }, 
    {
      "order": 3, 
      "place": "Spain", 
      "name": "Rodriquez Gentry", 
      "delete": " <i class=\"fa fa-pencil-square\" aria-hidden=\"true\"></i> <i class=\"fa fa-minus-square\" aria-hidden=\"true\"></i>"
    }, 
    {
      "order": 4, 
      "place": "Spain", 
      "name": "Rodriquez Gentry", 
      "delete": " <i class=\"fa fa-pencil-square\" aria-hidden=\"true\"></i> <i class=\"fa fa-minus-square\" aria-hidden=\"true\"></i>"
    }, 
    {
      "order": 5, 
      "place": "China", 
      "name": "Neva Allison", 
      "delete": " <i class=\"fa fa-pencil-square\" aria-hidden=\"true\"></i> <i class=\"fa fa-minus-square\" aria-hidden=\"true\"></i>"
    }
  ]
}

css:

div.addRow {
  line-height: 45px;
  background-color: #fff;
  padding-left: 10px;
  border-bottom: 1px solid;
  border-top: 1px solid #e5e5e5;
}

html:

   <table id="example" class="display" width="100%" cellspacing="0">
    <thead>
      <tr>
        <th>place</th>
        <th>name</th>
        <th>order</th>
        <th>action</th>
      </tr>
    </thead>
  </table>


  <table id="newRow" style="display:none">
    <tbody>
      <tr>
        <td>
          <select id="selectbasic" name="selectbasic" class="form-control">
            <option value="1">option 1</option>
            <option value="2">option 2</option>
            <option value="2">option 3</option>
          </select>
        </td>
        <td>Drag and drop a name here
        </td>
        <td>
          insert order number here</td>
        <td><i class="fa fa-pencil-square" aria-hidden="true"></i>
          <i class="fa fa-minus-square" aria-hidden="true"></i> </td>
      </tr>
    </tbody>
  </table>

jquery:

 $(document).ready(function() {

      var table;

      $("#example").on("mousedown", "td .fa.fa-minus-square", function(e) {
        table.row($(this).closest("tr")).remove().draw();
      })

      $("#example").on('mousedown.edit', "i.fa.fa-pencil-square", function(e) {

        $(this).removeClass().addClass("fa fa-envelope-o");
        var $row = $(this).closest("tr").off("mousedown");
        var $tds = $row.find("td").not(':first').not(':last');

        $.each($tds, function(i, el) {
          var txt = $(this).text();
          $(this).html("").append("<input type='text' value=\"" + txt + "\">");
        });

      });

      $("#example").on('mousedown', "input", function(e) {
        e.stopPropagation();
      });

      $("#example").on('mousedown.save', "i.fa.fa-envelope-o", function(e) {

        $(this).removeClass().addClass("fa fa-pencil-square");
        var $row = $(this).closest("tr");
        var $tds = $row.find("td").not(':first').not(':last');

        $.each($tds, function(i, el) {
          var txt = $(this).find("input").val()
          $(this).html(txt);
        });
      });


      $("#example").on('mousedown', "#selectbasic", function(e) {
        e.stopPropagation();
      });


      var url = 'http://www.json-generator.com/api/json/get/ccTtqmPbkO?indent=2';
      table = $('#example').DataTable({
        ajax: url,
        order: [[ 0, "desc" ]],
        rowReorder: {
          dataSrc: 'place',
          selector: 'tr'
        },
        columns: [{
          data: 'place'
        }, {
          data: 'name'
        }, {
          data: 'order'
        }, {
          data: 'delete'
        }]
      });

      $('#example').css('border-bottom', 'none');
      $('<div class="addRow"><button id="addRow">Add New Row</button></div>').insertAfter('#example');

      // add row
      $('#addRow').click(function() {
        //t.row.add( [1,2,3] ).draw();
        var rowHtml = $("#newRow").find("tr")[0].outerHTML
        console.log(rowHtml);
        table.row.add($(rowHtml)).draw();
      });
    });

【问题讨论】:

    标签: javascript jquery json datatables


    【解决方案1】:

    您已明确按第一列降序排列表格:

        order: [[ 0, "desc" ]],
    

    并且由于您的第一列不是 order 列,因此它没有按您想要的方式排序。

    您的order 列列在第 3 位,因此从零开始的数字为 2,因此您应该这样做:

        order: [[ 2, "asc" ]],
    

    【讨论】:

      【解决方案2】:

      只需更改初始化中的顺序即可。即顺序:[[ 2, "asc" ]],

      完整代码如下

         table = $('#example').DataTable({
          ajax: url,
          order: [[ 2, "asc" ]],
          rowReorder: {
            dataSrc: 'place',
            selector: 'tr'
          },
          columns: [{
            data: 'place'
          }, {
            data: 'name'
          }, {
            data: 'order'
          }, {
            data: 'delete'
          }]
        });
      

      【讨论】:

        猜你喜欢
        • 2019-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 2014-08-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多