2013-02-11 27 views
7

Tôi có một bảng dữ liệu lặp qua một đối tượng tùy chỉnh và tạo ra các hộp kiểm. Trên trang thứ hai, tôi muốn xác định hộp kiểm nào trong số các hộp kiểm này đã được chọn.Cách tìm ra hộp kiểm nào đã được chọn trên trang tiếp theo trong VisualForce?

Trong trang 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> 

Trong Controller:

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

Trên trang thứ hai của tôi, tôi có thể lấy giá trị mà người dùng đưa vào textbox "tuổi" bằng cách sử dụng {!age} . Tôi làm cách nào để truy xuất hộp kiểm nào đã được kiểm tra?

Cảm ơn bạn.

Trả lời

4

Ok, nếu bạn muốn xử lý nó bằng Javascript, hãy sử dụng phương thức của Pavel, nếu không, hãy sử dụng phương pháp sau để thực hiện điều đó thông qua bộ điều khiển. Bạn phải tạo một lớp bao bọc cho bất kỳ thứ gì bạn muốn theo dõi. Tôi không chắc nó hoạt động như thế nào, nhưng bằng cách nào đó nếu bạn đặt tên biến boolean "đã chọn" trong lớp trình bao bọc của bạn, nó sẽ được ánh xạ tới hộp kiểm.Dưới đây là các mã:

Vì vậy, trong trang lực lượng trực quan của bạn, làm:

<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"/> 

Trong điều khiển của bạn, làm như sau: 1) Thực hiện một lớp Wrapper với boolean "được chọn", mà bằng cách nào đó bản đồ đến inputCheckbox chọn:

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) khai báo các biến danh sách

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

3) Xác định accessor đăng ký vào Danh

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) Xác định phương pháp để xử lý các hộp kiểm chọn và đặt chúng trong một danh sách để sử dụng trên một trang

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) Xác định phương pháp để trả lại PageReference cho trang tiếp theo khi nút gửi được nhấp vào

public PageReference getPage2() { 
    processSelectedFoos(); 
    return Page.Page2; 
} 
3

Trong dự án hiện tại của tôi, tôi đã phải đối mặt với cùng một vấn đề. Tôi đã từng đỉnh: pageBlockTable, nhưng tôi đoán rằng bạn có thể sử dụng mã của tôi (tôi thực hiện một số thay đổi trong mã của tôi cho một tên đối tượng)

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

tôi đã đặt id đối tượng tùy chỉnh để các input id trong html, và nó trông như

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

The (chức năng saveSelection) đã viết trên 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(''); 
        } 
       } 
      } 
     }      
    }); 
} 

và cho khôi phục lại được sử dụng đoạn mã sau

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

} 

tôi sử dụng jQuery đây, có nghĩa là bạn nên bao gồm các mã sau đây vào trang của bạn quá

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

Các js cũng tham gia từ nút pagination:

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

Tôi hy vọng điều này có thể giúp bạn.

+0

Khái niệm và triển khai hoàn hảo! Cảm ơn @Pavel – highfive

Các vấn đề liên quan