Jquery Datatables 编辑行

7

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

问题1 - 用户添加新行后,拖放功能无法正常工作。

我需要的是: 在单击铅笔后使该行可编辑。

类似于此示例: https://editor.datatables.net/examples/simple/inTableControls.html

html:

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

<button id="addRow">Add New Row</button>
<table id="newRow">
  <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>DVap
       </td>
      <td>
       www</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 dt = $('#example').dataTable();
    dt.fnDestroy();
  });

  $(document).ready(function() {
    var url = 'http://www.json-generator.com/api/json/get/ccTtqmPbkO?indent=2';
    var table = $('#example').DataTable({
      ajax: url,
      rowReorder: {
        dataSrc: 'order',
      },
      columns: [{
        data: 'order'
      }, {
        data: 'place'
      }, {
        data: 'name'
      }, {
        data: 'delete'
      }],
      "initComplete": function(oSettings) {
        $(this).on('click', "i.fa.fa-minus-square", function(e) {
          table.row( $(this).closest('tr') ).remove().draw();
        });
      }
    });

    // 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();
    });
  });

jsfiddle: http://jsfiddle.net/5L2qy092/5/

3个回答

13
现在您可以拖放整行,而不仅仅是第一个td。
另外,编辑功能直接嵌入表格中。 我相信这就是您想要的:实际演示
<script>
    $(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,
        rowReorder: {
          dataSrc: 'order',
          selector: 'tr'
        },
        columns: [{
          data: 'order'
        }, {
          data: 'place'
        }, {
          data: 'name'
        }, {
          data: 'delete'
        }]
      });

      // 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();
      });
    });
  </script>

谢谢Offir的帮助,最后一个问题,可以把添加按钮放在行内而不是外部吗?就像这个例子一样:http://jsfiddle.net/5L2qy092/8/ - raduken
@Raduken,这是链接:http://plnkr.co/edit/B9fJQwgdLXEqBhrIJI76?p=preview。 你应该将帮助过你的答案标记为答案。 - Offir
一切都运作得很完美,但现在表格出了问题,那个按钮能不能也像示例中的一行一样呢?jsfiddle.net/5L2qy092/8 谢谢。 - raduken
嗨Offir,你知道为什么如果我在ajax调用中更改以下内容:columns:[{ data: 'place' }, { data: 'name' }, { data: 'order' }, { data: 'delete' }],新行会从顶部开始而不是底部,并且拖放无法正常工作吗? - raduken
1
嗨@Raduken,试着解决一下,你可以轻松地进行调试。如果仍然没有成功,那就提出一个新问题吧。 - Offir

5

我使用了以下代码,在DataTable中使用模态框来编辑或更新特定行索引。大多数示例都是在单击数据表的某个部分后可以进行更新。不幸的是,我需要使用Bootstrap的模态框进行更新:

var table = $('#tblSchedule').DataTable();

    table.row($('#hdnRowClicked').val()).data([
                "Tiger Nixon",
                "System Architect",
                "$3,120",
                "2011/04/25",
                "Edinburgh",
                "5421",
                "Tiger Nixon",
                "System Architect",
                "$3,120",
                "<p>Hello</p>"
            ]).draw();

为了获得行索引,我使用具有ID为hdnRowClicked的隐藏标签元素,在某人点击我的带有类btn-edit的编辑按钮时保存了行索引:
$('#tblRecord .btn-edit').click(function () {
        $('#hdnRowClicked').val($(this).parents('tr').index());
    });

3
我为您提供一个简单的方法来实现这个目标:

我给你一个简单的方法:

<div id="dialog" title="Basic dialog">


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

<button id="addRow">Add New Row</button>
<table id="newRow">
  <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>DVap
       </td>
      <td>
       www</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>



$(document).ready(function() {
    var dt = $('#example').dataTable();
    dt.fnDestroy();
  });

  $(document).ready(function() {
    var url = 'http://www.json-generator.com/api/json/get/ccTtqmPbkO?indent=2';
    var table = $('#example').DataTable({
      ajax: url,
      rowReorder: {
        dataSrc: 'order',
      },
      columns: [{
        data: 'order',
        type: 'text'
      }, {
        data: 'place',
        type: 'text',
        edit: true
      }, {
        data: 'name',
        type: 'text',
        edit: true
      }, {
        data: 'delete',
        type: 'text'
      }],
      "initComplete": function(oSettings) {
        $(this).on('click', "i.fa.fa-minus-square", function(e) {
          table.row( $(this).closest('tr') ).remove().draw();
        });


        $(this).on('click', 'i.fa.fa-pencil-square', function(e){
            var rowData = table.row($(this).closest('tr')).data();
          var columns = table.settings().pop().aoColumns;
          var column = columns[table.column($(this).closest('td')).index()];
          var rowIndex = table.row($(this).closest('tr')).index();

          var html = '<form id="form">';
          for(var col in columns){
            if(columns[col].type === 'text' && columns[col].edit){
              html += '<input type="text" value="'+rowData[columns[col].data]+'" name="'+columns[col].data+'" placeholder="'+columns[col].data+'"/><br />';
            }
          }

          html += '<input type="hidden" name="rowIndex" id="rowIndex" value="'+rowIndex+'" />';
          html += '<input type="submit" id="update"/></form>';
          $('#dialog').html(html);
          $( "#dialog" ).modal();
        });

      }
    });

    $('body').on('click', '#update', function(e) {
        e.preventDefault();
        var data = $('#form').serializeArray();
      var rowIndex = $('#rowIndex').val();
      var rowData = table.row(rowIndex).data();
      var newData = {};

      newData['order'] = rowData['order'];
      newData['delete'] = rowData['delete'];

      for(var d in data){
        newData[data[d]['name']] = data[d]['value'];
      }

      table
          .row(rowIndex)
          .data(newData)
          .draw();
    });
  });

http://jsfiddle.net/5L2qy092/7/


非常感谢,这就是我要找的,我可以有一个保存按钮而不是提交吗?当我点击保存时,保存行并关闭模态框?谢谢 - raduken
1
@Raduken,不是要贬低rad11,他提供的fiddler并没有按预期工作。添加新行不起作用,保存编辑更改也不起作用。 - Offir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接