Lightning DataTable中的keyField问题

时间:2019-02-01 17:52:32

标签: salesforce salesforce-lightning

在Lightning Datatable中获取所选记录的记录ID时遇到问题。

这是我的控制人

<!-- attributes -->
<aura:attribute name="dataArr" type="String[]"/>
<aura:attribute name="data" type="Object"/>
<aura:attribute name="columnsStr" type="String"/>
<aura:attribute name="columns" type="List"/>
<aura:attribute name="maxRowSelection" type="Integer" default="1"/>
<aura:attribute name="numOfRowsSelected" type="Integer" default="0"/>
<aura:attribute name="key" type="String" default="Id"/>
<aura:attribute name="recordId" type="String" />
<aura:attribute name="recordIds" type="String" />

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



<div style="height: 300px">
    <lightning:datatable keyField="{!v.key}"
            data="{! v.data }"
            columns="{! v.columns }"
            maxRowSelection="{! v.maxRowSelection }"
            onrowselection="{! c.setRecordId }"
             />
</div>

这是我的setRecordId函数

setRecordId : function(component, event, helper){

    var selectedRows = event.getParam('selectedRows');
    var key = component.get('v.key');
    var recIds = '';
    console.log(selectedRows);
    if(selectedRows){
        if(selectedRows.length === 1){
            console.log(selectedRows.id)
            console.log(selectedRows[key])
            console.log(selectedRows[0][key])
            component.set('v.recordId', selectedRows[0][key]);
        }
        else{
            for(let i = 0; i < selectedRows.length; i++){
                recIds += selectedRows[i][key] + ',';
            }
            component.set('v.recordIds', recIds);
            component.set('v.numOfRowsSelected', selectedRows.length);
        }
    }
},

Var selectedRows返回正确的选定行作为数组中的对象,但是由于某种原因,我似乎找不到正确的语法来访问记录ID。让我知道这里是否需要其他信息。 感谢帮助

1 个答案:

答案 0 :(得分:1)

您将必须在for循环中遍历选定的行,然后可以获取id引用- 像-

for(var i=0; i<selectedRows.length; i++){
    //This will give you the entire data for the row
    console.log(selectedRows[i]);

    //You can now fetch its Id as well as other parameters
    ...
}