当列 = 4 行 = 1 时,<p:commandButton> 在 p:dataGrid 内部不起作用

<p:commandButton> doesn't work inside of p:dataGrid when column = 4 row = 1

我只想为 p:dataGrid 列表中的每个项目添加 p:commandButton。

But, when I set columns = 4 and rows = 1 from properties p:dataGrid. I can't get value from commandButton anymore in rest of list (except for the first item in list).

尝试更改为 h:commandButton、p:commandLink、ajax= false;

<p:dataGrid var="list" value="#{myBean.listCompany}" columns="4" layout="grid"rows="1" paginator="true" paginatorPosition="bottom" paginatorTemplate="{PreviousPageLink} {NextPageLink} ">
                                        
<div class="card" style="height: 350px">
  <p:column>    
     <p:panel>
         <h:panelGrid columns="1">
        <p:commandButton value="Choose" action="#{myBean.selectCompany(list.companyName)}">
        </p:commandButton>
      </h:panelGrid> 
      </p:panel> 
     </p:column>
  </div>
</p:dataGrid>

Based on this , https://knowles.co.za/primefaces-actions-not-firing-in-last-rows-of-datagrid/

The quick fix is just to set the number of rows correctly, to fill the number of columns you have. As long as the number of rows is a multiple of the number of columns (eg, 2 columns, 6 rows) you should be fine.

但是,这不是我想要的解决方案,我需要显示 p:dataGrid 4 列和 1 行。

如果我理解你想要在一行中显示 #{myBean.listCompany} 中的 4 个元素,你应该设置 colums="4"rows="4"

这样rowscolumns个数的倍数,乘数是1

此处的行似乎是每页要显示的 #{myBean.listCompany} 元素的总数。如果这与多个列不匹配,则渲染和组件输入处理之间似乎存在某种不匹配。

您可以添加一个 rowIndexVar="rowIndex" 并将其与每个元素一起输出,以使数据网格中的一行可见。

<p:dataGrid var="list" value="#{myBean.listCompany}" columns="4"
    layout="grid" rows="4" paginator="true" paginatorPosition="bottom"
    paginatorTemplate="{PreviousPageLink} {NextPageLink} " rowIndexVar="rowIndex">

    <div class="card" style="height: 350px">
        <p:column>
            <p:panel>
                <h:panelGrid columns="1">
                    <p:commandButton value="Choose Row #{rowIndex}"
                        action="#{myBean.selectCompany(list.companyName)}">
                    </p:commandButton>
                </h:panelGrid>
            </p:panel>
        </p:column>
    </div>
</p:dataGrid>