Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Datatables Editing row

I am using: jquery.dataTables.js from: https://datatables.net

Issue 1 - Drag and Drop does not work after the user add a new row

What I need: make the row editable after click in the pencil.

similar to this sample: 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/

like image 296
raduken Avatar asked Feb 27 '17 17:02

raduken


People also ask

How do you edit data in a DataTable?

The edit button type is added to DataTables by Editor and provides a pre-defined button that will call the edit() method to trigger the editing of the selected rows in the DataTable.

How to inline edit DataTable?

The most common use case is to simply click on the cell you want to edit and then hit return once the edit is complete. This will save the data and the row will be immediately updated. This example shows inline editing on all data columns in the table.

How do you make a column editable in a DataTable?

on('click', function () { $("#myModal"). hide(); }); }); There are three columns in the table and I want to make a specific columns cell-like editable and show an input box and get the value edited to send. use contenteditable="true" attribute on those cells.

What is inline editing?

In-line editing lets you make quick changes without going to a new page. You can change daily budgets, ads, keywords, placements, and bids. If a cell is editable it will show a pencil icon. Click the cell to edit its value.


2 Answers

Now you can drag and drop with all the row and not only the first td.
Also the edit is inline the table. I believe this is what you wanted: WORKING DEMO.

<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>
like image 191
Offir Avatar answered Oct 16 '22 11:10

Offir


I used this code to edit or update specific row index on DataTable using Modal in which its a different section of my page. Most example points when you click a part of your datatable, from there you can update it. Unfortunately, I need to update it using Modal of 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();

To get row index, I saved the row index whenever someone click my edit button with class btn-edit using hidden tag element with an ID of hdnRowClicked:

$('#tblRecord .btn-edit').click(function () {
        $('#hdnRowClicked').val($(this).parents('tr').index());
    });
like image 5
Willy David Jr Avatar answered Oct 16 '22 10:10

Willy David Jr