1

URL on a field on my lightning datatable Component used to be working. When I add a search function I get the error as

'Action failed: c:test_filterComponent$controller$doInit [component is not defined]'

My controller

({
doInit: function (cmp, event, helper) {
    cmp.set('v.mycolumns', [
     // {label: 'Trainee Name', fieldName: 'Trainee__c', type: 'text',initialWidth: 300, cellAttributes: {alignment: 'left'},sortable: true},
       
      {label: 'Trainee Name', fieldName: 'linkName', type: 'url', typeAttributes: {label: { fieldName: 'Trainee__c' }, initialWidth: 300,sortable: true}}, 
        {label: 'Field', fieldName: 'Workplace__c', type: 'text',initialWidth: 300, cellAttributes: {alignment: 'left'},sortable: true},
        {label: 'Department', fieldName: 'NZ_Ski_Dept_2021__c', type: 'text', initialWidth: 300, cellAttributes: {alignment: 'left'}, sortable: true},
        {label: 'Online', fieldName: 'NZSki_Induction_2021_Online_Mod_Format__c', type: 'text', cellAttributes: {alignment: 'center'},initialWidth: 100, sortable: true},
        {label: 'Standards', fieldName: 'NZSki_Induction_Mod_Format__c', type: 'text', cellAttributes: {alignment: 'center'}, initialWidth: 100, sortable: true}
        
    ]);
            
     var action = component.get("c.getTPlans");
    action.setCallback(this, function(response){
        var records =response.getReturnValue();
        records.forEach(function(record){
            // record['linkName'] = 'https://skillsactiveaotearoa.force.com/nzski3/s/detail/'+ record.TraineeRef__c;
            record['linkName'] = 'https://preprod-skillsactiveaotearoa.cs197.force.com/nzski3/s/detail/'+ record.TraineeRef__c;
        });
        component.set("v.mydata", records);
    });
    $A.enqueueAction(action); 
    
    var actionFilter = {"field":cmp.get("v.field"),"dept":cmp.get("v.dept")};
    helper.getData(cmp, actionFilter);
},


doSearch:function(cmp, event, helper){
    var actionFilter = {"field":cmp.get("v.field"),"dept":cmp.get("v.dept")};
    helper.getData(cmp, actionFilter);
},

updateColumnSorting: function (cmp, event, helper) {
    var fieldName = event.getParam('fieldName');
    var sortDirection = event.getParam('sortDirection');
    // assign the latest attribute with the sorted column fieldName and sorted direction
    cmp.set("v.sortedBy", fieldName);
    cmp.set("v.sortedDirection", sortDirection);
    helper.sortData(cmp, fieldName, sortDirection);
}

})

Helper

({
getData : function(cmp, actionFilter) {
    var action = cmp.get('c.getTPlans');
    action.setParams(actionFilter)
    action.setCallback(this, $A.getCallback(function (response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            cmp.set('v.mydata', response.getReturnValue());
        } else if (state === "ERROR") {
            var errors = response.getError();
            console.error(errors);
        }
    }));
    $A.enqueueAction(action);
},
sortData: function (cmp, fieldName, sortDirection) {
    var data = cmp.get("v.mydata");
    var reverse = sortDirection !== 'asc';
    //sorts the rows based on the column header that's clicked
    data.sort(this.sortBy(fieldName, reverse))
    cmp.set("v.mydata", data);
},
sortBy: function (field, reverse, primer) {
    var key = primer ?
        function(x) {return primer(x[field])} :
        function(x) {return x[field]};
    //checks if the two rows should switch places
    reverse = !reverse ? 1 : -1;
    return function (a, b) {
        return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
    }
}

})

Can someone help to find where I wrong

1 Answer 1

2

This looks like a simple copy-paste error to me.

Your function has a variable cmp and not component and hence all you need is to make sure you use cmp

var action = cmp.get("c.getTPlans");
action.setCallback(this, function(response){
    var records =response.getReturnValue();
    records.forEach(function(record){
        // record['linkName'] = 'https://skillsactiveaotearoa.force.com/nzski3/s/detail/'+ record.TraineeRef__c;
        record['linkName'] = 'https://preprod-skillsactiveaotearoa.cs197.force.com/nzski3/s/detail/'+ record.TraineeRef__c;
    });
    cmp.set("v.mydata", records);
});
$A.enqueueAction(action); 
3
  • 1
    Thanks a lot, it was an silly mistake
    – Jay Weera
    Apr 23, 2021 at 4:47
  • Values on link field can be seen only when initiating the page. After searching the results comes without values in link field. Is something wrong ?
    – Jay Weera
    Apr 23, 2021 at 7:11
  • Sorry you will need to post a new query highlighting the exact problem! Apr 23, 2021 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.