You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无后台Bean时,如何根据dataTable行内outputText值设置selectOneMenu选中项?

问题及调整说明

编辑说明

需求已调整,仅需为noSelectionOption项添加条件标签即可满足需求,当前无需解决方案,但欢迎提供思路。调整后的代码如下:

<f:selectItem itemLabel="#{idMatch.assignedTo != null ? 'IN REVIEW' : 'Select Action'}" itemValue="" noSelectionOption="true" />

原问题

现有一个PrimeFaces dataTable,每行最后一列包含一个selectOneMenu下拉框。要求当同一行的idMatch.assignedTo不为null时,将该行的下拉框选中值设为"IN REVIEW"。

原页面代码

<h:form id="idForm">
    <p:dataTable id="dt_idtable" widgetVar="idTable" value="#{serviceBean.idEntries}" var="idMatch" >
        <p:column headerText="IDNUM">
            <h:outputText id="id_idnum" value="#{idMatch.idnum}" />
        </p:column>
        <p:column headerText="ASSIGNED TO">
            <h:outputText id="id_assignTo" value="#{idMatch.assignedTo}" />
        </p:column>
        <p:column headerText="ACTION">
            <p:selectOneMenu value="#{idMatch.selectedAction}" id="actionSelect" widgetVar="actionList" 
                onchange="setValuesAndSubmit('#{idMatch.idnum}', this.value)">
                <f:selectItem itemLabel="Select Action" itemValue="" noSelectionOption="true" />
                <f:selectItem itemLabel="ADD" itemValue="ADD" />
                <f:selectItem itemLabel="IN REVIEW" itemValue="IN REVIEW" />
            </p:selectOneMenu>
        </p:column>
    </p:dataTable>
</h:form>

问题细节与无效脚本

该下拉框未绑定到托管Bean,选中值仅作为参数传入JavaScript函数。已发现可通过PF('actionList').selectValue("IN REVIEW");设置单个下拉框的值,但无法遍历表格为每行设置对应值,以下是尝试过的无效JavaScript脚本:

function setActionInReview() {
    var actionSelect = document.querySelectorAll("#actionSelect"), i;
    for (i=0; i< actionSelect.length; ++i) {
        if (document.getElementById('idForm:id_assignTo').value != null) {
            PF('actionList').selectValue("IN REVIEW");
        }
    }
}

内容的提问来源于stack exchange,提问作者Chris C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 10:46:17