2017-08-11 13 views
5

Tôi có primefaces bước sử dụng thẻ <p:steps> như dưới đây:p: bước nhưng cho phép nhấp chuột trên tất cả các bước

<p:steps activeIndex="3" styleClass="custom" readonly="false" style="padding: 20px;"> 
    <p:menuitem value="step 1." actionListener="#{masterController.menuSales(preferencesController)}" update="mainPanel"/> 
    <p:menuitem value="step 2." actionListener="#{masterController.menuCustomer(preferencesController)}" update="mainPanel"/> 
    <p:menuitem value="step 3." actionListener="#{masterController.menuItem(preferencesController)}" update="mainPanel"/> 
    <p:menuitem value="step 4"/> 
</p:steps> 

Và kết quả là như thế này:

enter image description here

tôi có thể nhấp vào bước 1 nhưng không phải bước 3 và 4. Làm cách nào tôi có thể bật nhấp chuột cho tất cả các bước?

+0

Bạn đang cố gắng để thực hiện sử dụng 'p: steps'? –

+0

để cho người dùng biết bước anh ta phải làm để hoàn thành hướng dẫn, nhưng người dùng có thể nhấp vào bước tới bước kế tiếp/prev thay vì nhấp vào liên kết từ menu chính –

+1

Âm thanh như chức năng bạn muốn p: tabView thay thế. Bạn có thể đã chọn p: các bước vì lý do thẩm mỹ, điều này không được thông báo. –

Trả lời

4

Ồ, đó là một câu hỏi hay!

Tôi đã thử nhiều thứ với API hiện tại để hoàn thành, nhưng có vẻ như không thể thực hiện với các tùy chọn hiện tại của chúng tôi.

Để giải quyết việc này, tôi đã viết một renderer tùy chỉnh cho các thành phần bước:

Hầu hết các mã dưới đây là như nhau từ GitHub của PrimeFaces. Tôi chỉ thay đổi một vài điều để giải quyết vấn đề cụ thể này.

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.LinkedHashMap; 
import java.util.List; 
import java.util.Map; 
import javax.faces.FacesException; 
import javax.faces.component.UIComponent; 
import javax.faces.context.FacesContext; 
import javax.faces.context.ResponseWriter; 
import org.primefaces.component.api.AjaxSource; 
import org.primefaces.component.api.UIOutcomeTarget; 
import org.primefaces.component.steps.Steps; 
import org.primefaces.component.steps.StepsRenderer; 
import org.primefaces.model.menu.MenuItem; 
import org.primefaces.util.ComponentTraversalUtils; 

public class CustomStepsRenderer extends StepsRenderer { 

@Override 
protected void encodeItem(FacesContext context, Steps steps, MenuItem item, int activeIndex, int index) throws IOException { 
    ResponseWriter writer = context.getResponseWriter(); 
    String itemClass; 

    if (steps.isReadonly()) { 
     itemClass = (index == activeIndex) ? Steps.ACTIVE_ITEM_CLASS : Steps.INACTIVE_ITEM_CLASS; 
    } else { 
     if (index == activeIndex) { 
      itemClass = Steps.ACTIVE_ITEM_CLASS; 
     } 
     else { 
      itemClass = Steps.VISITED_ITEM_CLASS; 
     } 
    } 

    String containerStyle = item.getContainerStyle(); 
    String containerStyleClass = item.getContainerStyleClass(); 

    if (containerStyleClass != null) { 
     itemClass = itemClass + " " + containerStyleClass; 
    } 

    //header container 
    writer.startElement("li", null); 
    writer.writeAttribute("class", itemClass, null); 
    writer.writeAttribute("role", "tab", null); 
    if (containerStyle != null) { 
     writer.writeAttribute("style", containerStyle, null); 
    } 

    encodeMenuItem(context, steps, item, activeIndex, index); 

    writer.endElement("li"); 
} 

@Override 
protected void encodeMenuItem(FacesContext context, Steps steps, MenuItem menuitem, int activeIndex, int index) throws IOException {   
    ResponseWriter writer = context.getResponseWriter(); 
    String title = menuitem.getTitle(); 
    String style = menuitem.getStyle(); 
    String styleClass = this.getLinkStyleClass(menuitem); 

    writer.startElement("a", null); 
    writer.writeAttribute("tabindex", "-1", null); 
    if (shouldRenderId(menuitem)) { 
     writer.writeAttribute("id", menuitem.getClientId(), null); 
    } 
    if (title != null) { 
     writer.writeAttribute("title", title, null); 
    } 

    writer.writeAttribute("class", styleClass, null); 

    if (style != null) { 
     writer.writeAttribute("style", style, null); 
    } 

    if (steps.isReadonly() || menuitem.isDisabled()) { 
     writer.writeAttribute("href", "#", null); 
     writer.writeAttribute("onclick", "return false;", null); 
    } else { 
     String onclick = menuitem.getOnclick(); 

     //GET 
     if (menuitem.getUrl() != null || menuitem.getOutcome() != null) { 
      String targetURL = getTargetURL(context, (UIOutcomeTarget) menuitem); 
      writer.writeAttribute("href", targetURL, null); 

      if (menuitem.getTarget() != null) { 
       writer.writeAttribute("target", menuitem.getTarget(), null); 
      } 
     } //POST 
     else { 
      writer.writeAttribute("href", "#", null); 

      UIComponent form = ComponentTraversalUtils.closestForm(context, steps); 
      if (form == null) { 
       throw new FacesException("MenuItem must be inside a form element"); 
      } 

      String command; 
      if (menuitem.isDynamic()) { 
       String menuClientId = steps.getClientId(context); 
       Map<String, List<String>> params = menuitem.getParams(); 
       if (params == null) { 
        params = new LinkedHashMap<String, List<String>>(); 
       } 
       List<String> idParams = new ArrayList<String>(); 
       idParams.add(menuitem.getId()); 
       params.put(menuClientId + "_menuid", idParams); 

       command = menuitem.isAjax() 
         ? buildAjaxRequest(context, steps, (AjaxSource) menuitem, form, params) 
         : buildNonAjaxRequest(context, steps, form, menuClientId, params, true); 
      } else { 
       command = menuitem.isAjax() 
         ? buildAjaxRequest(context, (AjaxSource) menuitem, form) 
         : buildNonAjaxRequest(context, ((UIComponent) menuitem), form, ((UIComponent) menuitem).getClientId(context), true); 
      } 

      onclick = (onclick == null) ? command : onclick + ";" + command; 
     } 

     if (onclick != null) { 
      writer.writeAttribute("onclick", onclick, null); 
     } 
    } 

    writer.startElement("span", steps); 
    writer.writeAttribute("class", Steps.STEP_NUMBER_CLASS, null); 
    writer.writeText((index + 1), null); 
    writer.endElement("span"); 

    Object value = menuitem.getValue(); 
    if (value != null) { 
     writer.startElement("span", steps); 
     writer.writeAttribute("class", Steps.STEP_TITLE_CLASS, null); 
     writer.writeText(value, null); 
     writer.endElement("span"); 
    } 

    writer.endElement("a"); 
} 

Sau đó, đăng ký renderer mới này trong tập tin faces-config.xml của bạn:

<render-kit> 
     <renderer> 
      <component-family>org.primefaces.component</component-family> 
      <renderer-type>org.primefaces.component.StepsRenderer</renderer-type> 
      <renderer-class>YOUR_PACKAGE.CustomStepsRenderer</renderer-class> 
     </renderer> 
    </render-kit> 

Đừng quên thay đổi YOUR_PACKAGE đến vị trí gói CustomStepsRenderer của bạn.

Sau đó, chỉ cần xây dựng/tái triển khai ứng dụng của bạn và tất cả mọi thứ sẽ làm việc tốt:

enter image description here

+1

cảm ơn câu trả lời của bạn –

2

Vâng, p:stepsp:wizard là các thành phần trong bộ PrimeFaces thành phần đại diện hoặc chỉ ra các bước (s) trong một quy trình làm việc để quản lý nhiều bước của hình thức duy nhất (từng bước) cho quá trình simplication và có thể được sử dụng hoán đổi cho nhau nếu bạn hiểu cách sử dụng đúng cách (tùy theo yêu cầu).

Để sử dụng thành phần p:steps, bạn nên đảm bảo rằng (các) bước tiếp theo sẽ chỉ được hiển thị khi bước hiện tại được xử lý hoàn toàn và dữ liệu bắt buộc được thu thập. Giả sử quy trình mua sắm trực tuyến, nơi xử lý thanh toán là bước cuối cùng và sẽ xuất hiện khi và chỉ khi bạn có bất kỳ mặt hàng nào trong giỏ hàng và đã cung cấp thông tin khác (nếu có).

Kịch bản trên cũng có thể được triển khai bằng cách sử dụng thành phần p:wizard. Trường hợp chỉ có bước hiện tại được xử lý một phần và bước tiếp theo được hiển thị nếu bước hiện tại vượt qua các xác nhận hợp lệ. Tuy nhiên, thành phần p:wizard có tính khả thi để ghi đè hành vi mặc định của nó bằng cách kiểm soát luồng thuật sĩ, hiển thị các nút tiếp theo trước đây với các trình xử lý hành động tùy chỉnh và bỏ qua xác thực để xem các bước tiếp theo.

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