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

PrimeFaces DataTable每行日期差值计算问题求助

问题分析

当前所有行显示相同的日期差值,核心原因有两点:

  • 托管Bean中dosyalar是全局变量,仅在点击编辑按钮时被setPropertyActionListener赋值,表格渲染时复用了同一个dosyalar对象的计算结果。
  • 页面错误使用<p:columns>组件(该组件用于动态生成多列),而你只需要单列显示每行的差值,应该用普通<p:column>。
解决方案

1. 修改托管Bean代码

移除全局的difdaysevrak变量,新增一个接收Dosyalar对象的方法来计算每行的日期差值,同时用Java 8+的日期API替代老旧的Date和SimpleDateFormat(线程安全且代码更简洁):

@Component
@ViewScoped
@Data
public class DosyalarService implements Serializable {

  @Autowired private DosyalarRepository dosyalarRepository;

  private Dosyalar dosyalar; // 保留用于编辑功能
  private List<Dosyalar> dosyalarList;

  // 新增:接收当前行的Dosyalar对象,计算日期差值
  public String calculateDayDifference(Dosyalar item) {
    LocalDate today = LocalDate.now();
    LocalDate vekaletDate;

    if (item.getT_vekalet_trh() == null) {
      vekaletDate = today;
    } else {
      // 假设t_vekalet_trh是Date类型,转换为LocalDate
      vekaletDate = item.getT_vekalet_trh().toInstant()
                      .atZone(ZoneId.systemDefault())
                      .toLocalDate();
    }

    long diffDays = ChronoUnit.DAYS.between(vekaletDate, today);
    // 处理特殊日期调整(原代码中的t1判断)
    LocalDate specialDate = Instant.ofEpochMilli(1663240680000L)
                              .atZone(ZoneId.systemDefault())
                              .toLocalDate();
    if (!vekaletDate.isAfter(specialDate) && !today.isBefore(specialDate)) {
      diffDays -= 1;
    }

    return String.valueOf(Math.abs(diffDays));
  }
}

2. 修改页面代码

将错误的<p:columns>替换为普通<p:column>,并调用新增的calculateDayDifference方法,传入当前行的dosyalar对象:

<p:dataTable id="dosyalarListevrak" var="dosyalar" widgetVar="dosyalarListevrak1" value="#{dosyaListeService.dosyalarList}"
             style="table-layout: fixed; word-wrap: break-word; zoom:80%" 
             scrollable="true" allowUnsorting="true" sortMode="simple" showGridlines="true"
             emptyMessage="Aradığınız kriterlerle veri bulunamamıştır." >

    <!-- 原有列保持不变 -->
    <p:column headerText="E" style="width:5px">
        <p:commandButton icon="pi pi-user-edit" action="#{templateService.menu('dosyalarEdit')}" update="@this" ajax="false" styleClass="rounded-button ui-button-flat ui-button-plain" style="height:15px; margin:0">
            <f:setPropertyActionListener target="#{dosyalarService.dosyalar}" value="#{dosyalar}" />
        </p:commandButton>
    </p:column>
    <p:column  headerText="Dosya Bilgisi" sortBy="#{dosyalar.must_adi} #{dosyalar.must_soyadi}" filterBy="#{dosyalar.must_adi} #{dosyalar.must_soyadi}" filterMatchMode="contains"  style="width:100px" >
        <h:outputText value="#{dosyalar.must_adi} #{dosyalar.must_soyadi}" />
    </p:column>
    <p:column  headerText="Temsilci" sortBy="#{dosyalar.dtemsadi}" filterBy="#{dosyalar.dtemsadi}" filterMatchMode="contains"  style="width:100px">
        <h:outputText value="#{dosyalar.dtemsadi}" />
    </p:column>
    <p:column  headerText="Tür" sortBy="#{dosyalar.dtur}" filterBy="#{dosyalar.dtur}" filterMatchMode="contains"  style="width:100px">
        <h:outputText value="#{dosyalar.dtur}" />
    </p:column>
    <p:column  headerText="Eksper"  sortBy="#{dosyalar.d_eksadi}" filterBy="#{dosyalar.d_eksadi}" filterMatchMode="contains" style="width:100px">
        <h:outputText value="#{dosyalar.d_eksadi}" />
    </p:column>
    <p:column  headerText="Sigorta"  sortBy="#{dosyalar.d_sigorta}" filterBy="#{dosyalar.d_sigorta}" filterMatchMode="contains" style="width:100px">
        <h:outputText value="#{dosyalar.d_sigorta}" />
    </p:column>

    <!-- 修改后的日期差数列 -->
    <p:column headerText="Süre" style="width:100px">
        <h:outputText value="#{dosyalarService.calculateDayDifference(dosyalar)}" />
    </p:column>
    
</p:dataTable>

关键说明

  • 新增的calculateDayDifference方法接收每行的Dosyalar对象,确保为每行单独计算差值,不再依赖全局变量。
  • 使用Java 8的LocalDate和ChronoUnit替代老旧API,避免线程安全问题,代码更易维护。
  • 替换<p:columns>为<p:column>,因为你只需要单列显示差值,<p:columns>是用于动态生成多列场景的。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 05:50:56