根据行值在Lightning Datatable中隐藏按钮

时间:2018-07-04 10:48:38

标签: salesforce salesforce-lightning

我需要在同一行中根据团队组名称隐藏“闪电数据表”中的“查看”按钮 enter image description here

如果团队组名称为空,则必须隐藏查看按钮,如果团队组名称不为空,则我需要显示查看按钮

这是我的代码:

columns: [
    {label: "Course Title", fieldName: "CourseTitle", type: "text"},
    {label: "Team Group Name", fieldName: "TeamGroupName", type: "text"},
    {label: "Campus Name", fieldName: "CampusName", type: "text"},
    {label: "Course", fieldName: "Course", type: "text"},
    {label: "Section ID", fieldName: "SectionID", type: "text"},
    {label: "Session", fieldName: "Session", type: "text"},
    {label: "Course Level", fieldName: "CourseLevel", type: "text"},
    {label: "Term Length", fieldName: "TermLength", type: "text"},
    {type: "button", typeAttributes: {
            label: 'View',
            name: 'View',
            title: 'View',
            disabled: false,
            value: 'view',
            iconPosition: 'left'
        }}
]

组件:

<aura:component implements="force:appHostable"
controller="Teams_Controller">

<aura:attribute type="Account[]" name="acctList"/>
<aura:attribute name="mycolumns" type="List"/>

<aura:handler name="init" value="{!this}" action="{!c.fetchAccounts}"/>

<lightning:datatable data="{! v.acctList }" 
                     columns="{! v.mycolumns }" 
                     keyField="id"
                     hideCheckboxColumn="false"
                     onrowaction="{!c.viewRecord}"/>

1 个答案:

答案 0 :(得分:-2)

我遇到了和你一样的问题。
我设法通过遵循解决方案here来解决它。

编辑:我也在此发布解决方案。应该与您的情况类似。

组件:

<aura:component implements="force:appHostable"
   controller="AccountListController">

<aura:attribute type="AccountWrapper[]" name="acctList"/>
<aura:attribute name="mycolumns" type="List"/>

<aura:handler name="init" value="{!this}" action="{!c.fetchAccounts}"/>

<lightning:datatable data="{! v.acctList }" 
                     columns="{! v.mycolumns }" 
                     keyField="id"
                     hideCheckboxColumn="true"
                     onrowaction="{!c.viewRecord}"/>

组件控制器:

({

fetchAccounts : function(component, event, helper) {
    component.set('v.mycolumns', [
        {label: 'Account Name', fieldName: 'accName', type: 'text'},
        {label: 'Industry', fieldName: 'accIndustry', type: 'text'},
        {label: 'Type', fieldName: 'accType', type: 'text'},
        {label: 'Active?', fieldName: 'accActive', type: 'text'},
        {type: "button", typeAttributes: {
            label: 'View',
            name: 'View',
            title: 'View',
            disabled: { fieldName: 'isActive'},
            value: 'view',
            iconPosition: 'left'
        }}
    ]);
    var action = component.get("c.fetchAccts");
    action.setParams({
    });
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            component.set("v.acctList", response.getReturnValue());
        }
    });
    $A.enqueueAction(action);
},
viewRecord : function(component, event, helper) {
    var recId = event.getParam('row').accId;
    var actionName = event.getParam('action').name;
    if ( actionName == 'View') {
        var viewRecordEvent = $A.get("e.force:navigateToURL");
        viewRecordEvent.setParams({
            "url": "/" + recId
        });
        viewRecordEvent.fire();
    }
}
})

顶点类:

public class AccountWrapper {

@AuraEnabled
public String accName;
@AuraEnabled
public Boolean isActive;
@AuraEnabled
public String accIndustry;
@AuraEnabled
public String accType;
@AuraEnabled
public String accActive;   
@AuraEnabled
public String accId;
}

AccountListController:

public class AccountListController {

@AuraEnabled
public static List < AccountWrapper > fetchAccts() {
    List < AccountWrapper > listAcctWrapper = new List < AccountWrapper >();
    for ( Account acc : [ SELECT Id, Name, Industry, Type, Active__c FROM Account LIMIT 10 ] ) {
        AccountWrapper AccountWrap = new AccountWrapper();
        AccountWrap.accName = acc.Name;
        AccountWrap.isActive = acc.Active__c == 'Yes' ? true : false;
        AccountWrap.accIndustry = acc.Industry;
        AccountWrap.accType = acc.Type;
        AccountWrap.accActive = acc.Active__c;
        AccountWrap.accId = acc.Id;
        listAcctWrapper.add(AccountWrap);
    }
    return listAcctWrapper;
  }
}

希望它对您有帮助。