如何在p:dataTable选中行时查询字段?依赖字段获取求助
It sounds like the core problem here is that when you select a row in your dataTable, the dependent Cashaccount field isn’t populating correctly—even though your Portfolio dropdown’s AJAX listener works when you manually select a portfolio. Let’s break down the most likely fixes:
1. Ensure Row Selection Triggers the Portfolio-Dependent Logic
When you select a dataTable row, you’re probably setting the portfolio value in your beanCashMvt directly, but not invoking the same logic that loads cash accounts as your onPortfolioChange listener does.
Fix this by either:
- Calling your
onPortfolioChangemethod from your row selection listener, or - Adding the cash account loading logic directly into your row selection handler.
Example Java code for your bean:
public void onRowSelect(SelectEvent event) { CashMvt selectedRow = (CashMvt) event.getObject(); beanCashMvt.setPortfolio(selectedRow.getPortfolio()); // Reuse the existing portfolio change logic onPortfolioChange(null); // Pass null if the method doesn't require the event }
And update your dataTable’s AJAX event to include this listener:
<p:dataTable ...> <p:ajax event="rowSelect" listener="#{cashMvtView.onRowSelect}" update="Portfolio Cashaccount" /> </p:dataTable>
2. Verify Your onPortfolioChange Listener is Working Correctly
Double-check that your listener is actually fetching and storing the cash accounts properly:
- Make sure it’s retrieving the correct cash accounts for the selected portfolio from your service/repository.
- Ensure the fetched list is stored in a bean property that’s accessible via a getter (so the
Cashaccountdropdown can use it).
Example listener code:
public void onPortfolioChange(AjaxBehaviorEvent event) { // Fetch cash accounts based on the selected portfolio this.cashAccounts = cashAccountService.findByPortfolio(beanCashMvt.getPortfolio()); // Optional: Log to confirm data is loaded System.out.println("Loaded " + cashAccounts.size() + " cash accounts for portfolio: " + beanCashMvt.getPortfolio()); }
3. Confirm the Cashaccount Dropdown is Bound Correctly
Ensure your Cashaccount selectOneMenu is using the right collection for its options. It should be bound to the same cashAccounts property populated by your listener:
<p:selectOneMenu id="Cashaccount" value="#{cashMvtView.beanCashMvt.cashAccount}" style="width:120px"> <f:selectItems value="#{cashMvtView.cashAccounts}" var="account" itemLabel="#{account.accountNumber}" itemValue="#{account}" /> </p:selectOneMenu>
4. Check Bean Scope
Make sure your cashMvtView bean is using @ViewScoped (not @RequestScoped). Request-scoped beans reset their state on every AJAX call, so the cashAccounts list would be empty after row selection.
5. Debug the Flow
Add debug statements to track the state of your portfolio and cash accounts during row selection:
- Check if the portfolio is correctly set when a row is selected.
- Confirm that the
cashAccountslist is populated after callingonPortfolioChange.
If you still run into issues, sharing the full code for your Cashaccount dropdown and dataTable row selection event would help narrow things down further!
内容的提问来源于stack exchange,提问作者Noureddine

