function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
uranushkuranushk 

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;       
    }

以上、よろしくお願いいたします。

uranushkuranushk

自己レスですが、以下のようにしました。

他にシンプルな方法があれば教えてください。

 

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;
    }


}

timatima

VF Pageで変数を使ってあげれば、もう少しシンプルになるかと思います。

 

VF Page

<apex:variable value="0" var="rowIndex" />
<apex:dataTable value="{!tsamples}" var="sp">
	<apex:column headerValue="">
	    <apex:commandLink value="削除" action="{!del}">
	        <apex:param name="vf_rownumber" value="{!rowIndex}"/>
	    </apex:commandLink>
	    <apex:variable var="rowIndex" value="{!VALUE(rowIndex) + 1}" />
	</apex:column>
</apex:dataTable>

 

Apex

    public PageReference del() {
        Integer i = Integer.valueOf(ApexPages.currentPage().getParameters().get('vf_rownumber'));
		SampleList.remove(i);
        return null;
    }

 

いまさらなのですが、ご参考までに。