获取Lightning数据表中所选记录的记录Ids时出现问题。
这是我的控制器
<!-- 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。如果这里需要任何额外的信息,请告诉我。感谢你的帮助
发布于 2019-03-04 21:28:15
您必须在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
...
}https://stackoverflow.com/questions/54484732
复制相似问题