7

カスタム オブジェクトを反復処理してチェックボックスを生成するデータ テーブルがあります。2 ページ目では、これらのチェックボックスのどれが選択されているかを確認します。

VisualForce ページで:

 Age <apex:inputText value="{!age}" id="age" />
 <apex:dataTable value="{!Areas}" var="a">
      <apex:column >
      <apex:inputCheckbox value="{!a.name}" /> <apex:outputText value="{!a.name}" />
      </apex:column>
  </apex:dataTable>

コントローラーで:

 public String age {get; set; }
  public List<Area_Of_Interest__c> getAreas() {
      areas = [select id, name from Area_Of_Interest__c];
      return areas;
  }

2 番目のページでは、ユーザーがテキスト ボックスの「年齢」に入力した値を を使用して取得できます{!age}。どのチェックボックスがチェックされているかを取得するにはどうすればよいですか?

ありがとうございました。

4

2 に答える 2

4

わかりました、Javascript で処理したい場合は Pavel のメソッドを使用します。それ以外の場合は、次のようにコントローラー経由で処理します。追跡する対象のラッパー クラスを作成する必要があります。どのように機能するかはわかりませんが、ラッパークラスでブール変数に「選択済み」という名前を付けると、チェックボックスにマップされます。以下はコードです:

Visual force ページで、次のようにします。

<apex:dataTable value="{!Foos}" var="f">
    <apex:column >
        <apex:outputLabel value="{!f.foo.name}" /> <apex:inputCheckbox value="{!f.selected}" />
    </apex:column>
 </apex:dataTable>
 <apex:commandButton action="{!getPage2}" value="go"/>

コントローラーで、次の手順を実行します。1) 選択された inputCheckbox に何らかの形でマップされるブール値 "selected" を持つ Wrapper クラスを作成します。

public class wFoo {
    public Foo__c foo {get; set}
    public boolean selected {get; set;}

    public wFoo(Foo__c foo) {
        this.foo = foo;
        selected = false; //If you want all checkboxes initially selected, set this to true
    }
}

2) リスト変数を宣言する

public List<wFoo> foos {get; set;}
public List<Foo__c> selectedFoos {get; set;}

3) List のアクセサを定義する

public List<wFoo> getFoos() {
    if (foos == null) {
        foos = new List<wFoo>();
        for (Foo__c foo : [select id, name from Foo__c]) {
            foos.add(new wFoo(foo));
        }
    }
    return foos;
}

4) 選択したチェックボックスを処理する方法を定義し、それらを別のページで使用するためにリストに配置します

public void processSelectedFoos() {
    selectedFoos = new List<Foo__c>();
    for (wFoo foo : getFoos) {
        if (foo.selected = true) {
            selectedFoos.add(foo.foo); // This adds the wrapper wFoo's real Foo__c
        }
    }
}

5) 送信ボタンがクリックされたときに PageReference を次のページに戻すメソッドを定義する

public PageReference getPage2() {
    processSelectedFoos();
    return Page.Page2;
}
于 2013-02-22T20:08:04.010 に答える
3

私の現在のプロジェクトでは、同じ問題に直面していました。私は apex:pageBlockTable を使用しましたが、私のコードを使用できると思います (オブジェクト名のコードにいくつかの変更を加えました)。

<apex:pageBlockTable value="{!Areas}" var="areas">
    <apex:column width="25px">
        <apex:facet name="header">
            <input type="checkbox" onClick="selectAll();" />
        </apex:facet>
        <input type="checkbox" id="{!areas.Id}" onClick="saveSelection();" />
    </apex:column>
... some additional columns
</apex:pageBlockTable>

カスタムオブジェクトIDをhtmlの入力IDに配置しましたが、次のようになります

<input id="003R000000lCIq6IAG" onclick="saveSelection();" type="checkbox">
<input id="003R000000lCIoJIAW" onclick="saveSelection();" type="checkbox">

saveSelection() 関数は JavaScript で記述されています

<script>
var areaIds = [];

function saveSelection() {
    var selectedIds = areaIds.join('');
    $j(':checkbox').each(function(){
        if (this.checked) {
            if (selectedIds.indexOf(this.id) === -1) {
                areaIds.push(this.id);
                selectedIds = selectedIds + this.id;
            }
        } else {
            if (selectedIds.indexOf(this.id) !== -1) {
                for (i=0; i < areaIds.length; i++) {
                    if (areaIds[i] === this.id) {
                        areaIds.splice(i, 1);
                        selectedIds = areaIds.join('');
                    }
                }
            }
        }                      
    });
}

復元には次のコードが使用されました

function restoreSelection() {
    contIds = areaIds.join('');
    i = 0;
    $j(':checkbox').each(function(){ if(this.id !== ''){  if(contIds.indexOf(this.id) !== -1){this.checked=true;};}});

}

ここでは jQuery を使用します。つまり、ページにも次のコードを含める必要があります。

<apex:includeScript id="JQuery" value="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"/>
... some code
<script>
    window.$j = jQuery.noConflict();
    ... some code
</script>

js は、ページネーション ボタンからも関与しています。

<apex:panelGrid columns="7">
   <apex:commandButton status="fetchStatus" reRender="results" value="|<" action="{!first}" disabled="{!!hasPrevious}" title="First Page" onClick="saveSelection();" oncomplete="   restoreSelection()"/>
   <apex:commandButton status="fetchStatus" reRender="results" value="<" action="{!previous}" disabled="{!!hasPrevious}" title="Previous Page" onClick="saveSelection();"    oncomplete="restoreSelection()"/>
   <apex:commandButton status="fetchStatus" reRender="results" value=">" action="{!next}" disabled="{!!hasNext}" title="Next Page" onClick="saveSelection();" oncomplete="   restoreSelection()"/>
   <apex:commandButton status="fetchStatus" reRender="results" value=">|" action="{!last}" disabled="{!!hasNext}" title="Last Page" onClick="saveSelection();" oncomplete="   restoreSelection()" />
   <apex:outputText >{!(pageNumber * size)+1-size}-{!IF((pageNumber * size)>noOfRecords, noOfRecords,(pageNumber * size))} of {!noOfRecords}</apex:outputText>
   <apex:outputPanel style="color:#4AA02C;font-weight:bold">
       <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
   </apex:outputPanel> 
</apex:panelGrid>

これがお役に立てば幸いです。

于 2013-02-12T04:04:32.557 に答える