我想创建一个数据表,其中每个单元格都是可单击的。我假设我可以用 apex:outputlink
填充每个单元格,并处理可点击部分以及为每次点击调用我的 Controller 。我需要答案的一个大问题是如何将有关实际单击了哪个单元格(即:哪一行和哪一列)的信息传递给我的顶点 Controller 。
非常感谢对此的任何帮助。
最佳答案
这很容易。只需定义一个操作函数来捕获数据表中的值:
1)首先定义我们将传递给 Controller 的三个变量:raw-id、cell-value、cell-type
public String clickedRowId { get; set; }
public String clickedCellValue { get; set; }
public String clickedCellType { get; set; }
public PageReference readCellMethod(){
System.debug('#### clickedRowId: ' + clickedRowId);
System.debug('#### clickedCellValue: ' + clickedCellValue);
System.debug('#### clickedCellType: ' + clickedCellType);
return null;
}
2) 其次,我们创建一个操作函数,它调用我们的 apex 方法并向其传递三个变量:
<apex:actionFunction name="readCell" action="{!readCellMethod}">
<apex:param name="P1" value="" assignTo="{!clickedRowId}"/>
<apex:param name="P2" value="" assignTo="{!clickedCellValue}"/>
<apex:param name="P3" value="" assignTo="{!clickedCellType}"/>
</apex:actionFunction>
3) 第三,我们创建 dataTable,其中每个单元格都有 onClick 监听器:
<apex:pageBlockTable value="{!someArray}" var="item">
<apex:column value="{!item.name}" onclick="readCell('{!item.id}','{!item.name}','name')" />
<apex:column value="{!item.CustomField1__c}" onclick="readCell('{!item.id}','{!item.CustomField1__c}','custom1')" />
<apex:column value="{!item.CustomField2__c}" onclick="readCell('{!item.id}','{!item.CustomField2__c}','custom2')" />
</apex:pageBlockTable>
我们可以像任何其他 JavaScript 函数一样访问我们的 actionFunction。如果用户单击单元格 - 三个变量将被发送到 actionFunction,然后发送到 Controller 。
关于datatable - Visualforce - 可点击的数据表单元格 - 如何获取列和行信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11939740/