what is the corresponding of .fnGetPosition in Datatable ?

what is the corresponding of .fnGetPosition in Datatable ?

mlotfimlotfi Posts: 60Questions: 5Answers: 0
edited March 2015 in Free community support

Hi,
what is the corresponding of .fnGetPosition in Datatable ?

I am using Datatable with capital T :

 $(document).ready(function() { 
     //  $(".button").button();
       var oTable = $('#metadataListTable').DataTable({  
........

var target_row = $(this).closest("tr").get(0); 
var aPos = oTable.fnGetPosition(target_row);  <--------- undefined
oTable.fnUpdate('T',aPos,9);

Thanks.

Replies

  • allanallan Posts: 63,364Questions: 1Answers: 10,449 Site admin
  • mlotfimlotfi Posts: 60Questions: 5Answers: 0
    edited March 2015

    Thanks allan,

    I got confused , I don't know how to do the conversion :


    $(document).on('click','#metadataListTable .delete', function () { event.preventDefault(); var ans = confirm("Do you want to delete Metadata?"); if(ans==true){ var nRow = $(this).parents('tr')[0]; // START CONVERSION var target_row = $(this).closest("tr").get(0); // ???????? var aPos = oTable.fnGetPosition(target_row); // ?????? oTable.fnUpdate('T',aPos,9); // ???????? // END CONVERSION var data = oTable.row( $(this).parents('tr') ).data(); var ifaceData = data[10].replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"'); $.ajax({ url: "deleteMetadata.do", type: 'POST', data:{ id : nRow.id, flag: "T", ifData: ifaceData, env: data[11], }, success: function(response) { toastr.success(response.message); } }); } });
  • allanallan Posts: 63,364Questions: 1Answers: 10,449 Site admin
    table.cell( $(this).closest('tr'), 9 ).data( 'T' );
    

    You may want to call draw() as well.

    Allan

  • mlotfimlotfi Posts: 60Questions: 5Answers: 0

    Thank you allan, it works now

    I have a problem reading row data :

    oTable.cell( $(this).closest('tr'), 9 ).data( 'T' ).draw();
    var data = oTable.row( $(this).closest('tr') ).data();
    //the next line has this error :
    //Uncaught TypeError: Cannot read property '10' of undefined
    console.log(data[10]);
    
  • mlotfimlotfi Posts: 60Questions: 5Answers: 0

    Hi allan,

    I found the solution :

    when you put this order :

    oTable.cell( $(this).closest('tr'), 9 ).data( 'T' ).draw();
    var data = oTable.row( $(this).closest('tr') ).data();
    

    data will be undefined

    but when you swap the above lines :

    var data = oTable.row( $(this).closest('tr') ).data();
    oTable.cell( $(this).closest('tr'), 9 ).data( 'T' ).draw();
    

    data is defined.

    this is very strange.

  • mlotfimlotfi Posts: 60Questions: 5Answers: 0
    edited March 2015

    There is a problem when trying to update two cells in the same event (for example click), the first cell data got updated but the second one throw an error :

    Uncaught TypeError: Cannot read property 'row' of undefined

    here is my code :

    $(document).on('click','#metadataListTable .delete', function () { 
                     event.preventDefault();
                    var ans = confirm("Do you want to delete Metadata?");
                    if(ans==true){
                     var nRow = $(this).parents('tr')[0];
     
                   var data = oTable.row($(this).closest('tr')).data();
    
       oTable.cell( $(this).closest('tr'), 9 ).data( 'T' ).draw(); // UPDATED
       oTable.cell( $(this).closest('tr'), 8 ).data('<td class="center"><a href="#" class="button enable" style="margin:10px;margin-right:30px;">Enable</a></td>'); // NOT UPDATED
                      
                     var data = oTable.row( $(this).parents('tr') ).data();
                     var ifaceData = data[10].replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"');
                      
                     $.ajax({
                      url: "deleteMetadata.do",
                      type: 'POST',
                      data:{
                          id : nRow.id,
                          flag: "T",
                          ifData:  ifaceData,
                          env:  data[11],
                      },
                     success: function(response) {
                       toastr.success(response.message);
                     }
                  });           
                }
           });
    
    
  • allanallan Posts: 63,364Questions: 1Answers: 10,449 Site admin

    Are you using server-side processing (without a link to a test case, as required in the forum rules, it is hard to know the full picture). If so, then you don't want to call draw() as that would refresh the page.

    Beyond that, I think we probably would need a link to the page to understand what is going wrong.

    Allan

  • mlotfimlotfi Posts: 60Questions: 5Answers: 0
    edited March 2015

    I created a jsfiddle, the update of the two cells data works fine, but locally in my machine it does not, here is the fiddle :

    http://jsfiddle.net/mlotfi/1z3bnwwp/

    in my local code when I switch the above two lines to :

     oTable.cell( $(this).closest('tr'), 8 ).
    data('<td class="center"><a href="#" class="button enable" style="margin:10px;margin-right:30px;">Enable</a></td>');
     //  UPDATED
    
    oTable.cell( $(this).closest('tr'), 9 ).data( 'T' ).draw(); // NOT UPDATED
    
    

    I tried this test :

                    var data = oTable.row($(this).closest('tr')).data();
                    console.log(data);
                     oTable.cell( $(this).closest('tr'), 9 ).data( 'T' );
                     var data2 = oTable.row($(this).closest('tr')).data();
                     console.log(data2); // <------- ------------- Undefined
    

    Always the the first cell get updated but not the second one, it looks like the row got lost, anything to handle this problem will be appreciated.

  • allanallan Posts: 63,364Questions: 1Answers: 10,449 Site admin

    I would need a test case that shows the problem I'm afraid. As you noted, the test case you've provided doesn't appear to show a problem, so I can't fix it as there is nothing to fix! :-)

    Allan

This discussion has been closed.