简体   繁体   中英

Datatable activate single cell editing onclick (part 2)

Continuing from my previous problem found here:

Datatable activate single cell editing onclick

I was finally able to get just a single cell to activate an input field onclick.

My new problem is, although I can click on a single cell and activate it, when I use the datatable filter search or go to another page of the datatable, the cell activation no longer works.

$.ajax({
  url: 'api/searchVoyageInfo.php',
  type: 'POST',
  data: '',
  dataType: 'html',
  success: function(data, textStatus, jqXHR){
    var jsonObject = JSON.parse(data); 
    var table = $('#example1').DataTable({
      "data": jsonObject,
      "columns": [{ 
        { "data": "COLUMN1" },  
        { 
          "data": "COLUMN2",
          "fnCreatedCell": function (nTd, sData, oData, iRow, iCol)
          {
            $(nTd).html("<a href='#' class='checkBound"+oData.VOYID+"' id='checkBound' 
                         data-uid='"+oData.VOYID+"'>"+oData.COLUMN2+"</a>
                         <input type='text' class='editbound"+oData.VOYID+"' 
                         id='editbound' data-uid='"+oData.VOYID+"' 
                         value='"+oData.BOUND+" display: none;' />");
          }
        },
        { "data": "COLUMN3" },
        // few more columns
      }],
      "iDisplayLength": 50,
      "paging": true,
      "bDestroy": true,
      "autoWidth": true,
      "dom": 'Bfrtip',
       "buttons": [
        // some extend buttons
      ]
    });
  },
  error: function(// some stuff){
    // do some other stuff
    // this part is not important
  }
});

If you'll take notice to the COLUMN2 section above, I added a VOYID value to the checkBound class of the href. I also added the VOYID to the editbound class of the input. Doing this prevented every single checkBound class from activating the input using the onclick event below:

$('#example1').on('click', 'tr > td > a.checkBound', function(e)
{
  e.preventDefault();
  var $dataTable = $('#example1').DataTable();
  var tr = $(this).closest('tr');
  var data = $dataTable.rows().data();
  var rowData = data[tr.index()];

  var voyid = rowData.VOYID;

  console.log(voyid);

  $('.checkBound'+voyid).hide();
  $('.editbound'+voyid).show();
});

Using all of the above, on the first page of the datatable, I can successfully activate the input from a single cell onclick. But it only works on the first page. I cannot activate the input on any other page.

I tried switching the onclick event from using the class to the id achieving the same results.

How can I solve this problem?

You can hide the link and show the field with the following code in your click event handler

e.preventDefault();
$(this).hide().next().show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM