Get Row Data and Pass to my JavaScript Function

Get Row Data and Pass to my JavaScript Function

zezuzezu Posts: 2Questions: 1Answers: 0
edited December 2017 in Free community support

I am adding a view/Edit Button to each row of my table , On click i have to get the data and pass it to my javascript function , but unable to do so ,

Adding the button/links like this

 "columnDefs": [ {
        "targets": -1,
        "data": null,
        "defaultContent": 
             '<a id="btn-view">View-</a>'
          +  '<a class="btn-edit">Edit</a>'
        } ]

   $('#allMemberTable tbody').on('click', '.btn-edit', function (e) {
 
      var data = table.row( $(this).parents('tr') ).data();
       console.log("data ="+data[0]);     
                 
       // Getting Row data scuccessfully 
      test(data) ;   // This is not getting called ,   it is not recognizing this metod , giving error , test is not a function , i tried with this.test() , but same 
 
   });

 // Getting Row data scuccessfully 
      test(data) ;   // This is not getting called ,   it is not recognizing this metod , giving error , test is not a function , i tried with this.test() , but same 

please correct me or give any other way around ( sorry for this basic question)

EDIT: Reformatted post using Markdown

Answers

  • kthorngrenkthorngren Posts: 20,583Questions: 26Answers: 4,823

    Its hard to tell with your snippets of code. Where do you have your test function?

    Kevin

  • allanallan Posts: 62,241Questions: 1Answers: 10,211 Site admin

    Agreed - it looks like you simply don't have a test function in that scope.

    Allan

  • zezuzezu Posts: 2Questions: 1Answers: 0
    edited December 2017
    export class ListMembersComponent implements OnInit {
    
    setTableData(tableId,arr){
    
        if(!$(tableId).length) return;
        var table=$(tableId).DataTable({
            "aaSorting": [],
             destroy: true,
            "iDisplayLength": 5,
            
            "columnDefs": [ {
            "targets": -1,
            "data": null,
            "defaultContent": 
                 '<button id="btn-view">button-</button>'
             // +  '<a class="btn-edit">Edit</a>'
            } ]
        });   
        table.clear().draw();
        
        arr.forEach(function (obj,i) {
    
            var b = [obj.memberId,obj.memberName,obj.workPlace ,obj.salary,
                obj.category,obj.paymentType,obj.amount];        
                
                table.row.add(b);
        });
    
       $('#allMemberTable tbody').on('click', '.btn-edit', function (e) {
     
         var data = table.row( $(this).parents('tr') ).data();
          console.log("data ="+data[0]);    
                     
          // Getting Row data scuccessfully
         test(data) ;   // This is not getting called ,   it is not recognizing this metod , giving error , test is not a function , i tried with this.test() , but same
     
      });
    
      table.draw(); 
    
      }
    
    
      test(data){
        console.log("inside test"+data);
      }
    
    }
    

    These are two methods in one angular2 class , I call setTableData to set the table data, and from that method i want to call other method e.g. test(data).

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 62,241Questions: 1Answers: 10,211 Site admin

    I still don't see a valid function called test anywhere. This code isn't actually valid Javascript:

      test(data){
        console.log("inside test"+data);
      }
    

    Can you link to a test case showing the issue please?

    Allan

This discussion has been closed.