You need to sign in to do that
Don't have an account?
pageblocktableでクリックした行位置をapexコード内で参照する方法
初歩的な質問かも知れませんが、件名にあるとおりpageblocktableでクリックした行位置をapexコードから参照する方法がわかりません。
下記のページ内容は、commandLink("削除")をクリックしたときにactionが実行され、そのapexコードの中で行位置を参照し、List変数にあるレコード要素を削除するというものです。
-- ページ内容
<apex:page Controller="c_OBJ" sidebar="false">
<apex:pageBlockTable value="{!objs}" var="objs">
<apex:column >
<apex:commandLink action="{!del}" value="削除" />
</apex:column>
<apex:column headerValue="sample">
<apex:inputField value="{!objs.field1__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:page>
-- Apexコード(抜粋)
public PageReference del() {
obj.remove(※ここに挿入したい);
return null;
}
以上、よろしくお願いいたします。
自己レスですが、以下のようにしました。
他にシンプルな方法があれば教えてください。
VF:
<apex:page controller="vc_sample">
<apex:form >
<apex:pageblock >
<apex:pageBlockButtons >
<apex:commandButton action="{!add}" value="追加"/>
</apex:pageBlockButtons>
<apex:pageblockTable value="{!tsamples}" var="sp">
<apex:column headerValue="">
<apex:commandLink value="削除" action="{!del}">
<apex:param name="vf_rownumber" value="{!sp.vf_rownumber__c}"/>
</apex:commandLink>
</apex:column>
<apex:column headerValue="名前">
<apex:inputField value="{!sp.name}"/>
</apex:column>
</apex:pageblockTable>
</apex:pageblock>
</apex:form>
</apex:page>
APEX:
public class vc_sample {
List<vo_sample__c> SampleList;
double listrenban = 1;
public List<vo_sample__c> gettsamples() {
if (SampleList == null) {
SampleList = [select name, vf_rownumber__c from vo_sample__c];
}
return SampleList;
}
public void settsamples(List<vo_sample__c> tsamples) {
SampleList = tsamples;
}
public PageReference add() {
SampleList.add(new vo_sample__c(vf_rownumber__c = listrenban));
listrenban = listrenban + 1;
return null;
}
public PageReference del() {
Integer i = 0;
double currentrow = Double.valueof(ApexPages.currentPage().getParameters().get('vf_rownumber'));
for ( vo_sample__c obj : SampleList ) {
if ( obj.vf_rownumber__c == currentrow ) {
SampleList.remove(i);
return null;
}
i = i + 1;
}
return null;
}
}
VF Pageで変数を使ってあげれば、もう少しシンプルになるかと思います。
VF Page
Apex
いまさらなのですが、ご参考までに。