Проблемы с keyField в Lightning DataTable - PullRequest
0 голосов
/ 01 февраля 2019

Возникла проблема с получением идентификаторов записей выбранной записи в Lightning Datatable.

Вот мой контроллер

<!-- 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 возвращаетисправить выбранную строку как объект в массиве, но я не могу найти правильный синтаксис для доступа к этому идентификатору записи по некоторым причинам.Дайте мне знать, нужна ли здесь дополнительная информация.признателен за помощь

1 Ответ

0 голосов
/ 04 марта 2019

Вам придется перебирать выбранные строки в цикле 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
    ...
}
...