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

SAP UI5表格截止日期过滤失效求助(本地JSON数据)

Hey there! Let's work through fixing your date filtering issue in SAP UI5. From what you shared, you want to show all table rows where the "TO" date is on or before the date you input (like Feb 13, 2018) when clicking the "Display" button, but it's not working right now. Here's how to get this sorted:

First, Let's Address Date Format & Comparison

The main pitfall here is comparing dates as strings—this will lead to incorrect results because string comparison uses character order, not actual date logic. We need to convert both the input date and the "TO" field values into proper Date objects for accurate comparison.

Step 1: Ensure Your Date Input is Set Up Correctly (XML)

If you're using a sap.m.DatePicker (recommended for date inputs), set its format pattern to match your input style ("Feb 13, 2018") so it can properly parse the date into a Date object:

<!-- Add this to your XML view -->
<DatePicker 
  id="dpFilterDate" 
  formatPattern="MMM dd, yyyy" 
  placeholder="Enter date (e.g., Feb 13, 2018)" />
<Button 
  text="Display" 
  press=".onDisplayFilter" />

<!-- Your table definition -->
<Table id="yourTableId" items="{/yourJsonDataPath}">
  <!-- Columns including the "TO" date column -->
  <Column>
    <Text text="TO Date" />
  </Column>
  <ColumnListItem>
    <Text text="{path: 'TO', formatOptions: { pattern: 'dd-MM-yyyy' }}" />
    <!-- Other columns -->
  </ColumnListItem>
</Table>

Step 2: Add Filter Logic in the Controller

In your controller's button press handler, we'll:

  1. Grab the selected date as a Date object (no messy string parsing needed!)
  2. Create a custom filter to compare each row's "TO" date (converted from dd-MM-yyyy string to Date) against the input date
  3. Apply the filter to the table's binding

Here's the code:

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "sap/ui/model/Filter",
  "sap/m/MessageBox"
], function(Controller, Filter, MessageBox) {
  "use strict";

  return Controller.extend("your.namespace.Controller", {
    onDisplayFilter: function() {
      // Get the date picker control and selected date
      const oDatePicker = this.getView().byId("dpFilterDate");
      const oInputDate = oDatePicker.getDateValue();

      // Validate input
      if (!oInputDate) {
        MessageBox.error("Please select a valid date first!");
        return;
      }

      // Get the table's item binding
      const oTable = this.getView().byId("yourTableId");
      const oItemsBinding = oTable.getBinding("items");

      // Create a custom filter to compare dates
      const oDateFilter = new Filter({
        test: function(oContext) {
          // Get the "TO" date string from the model (dd-MM-yyyy format)
          const sToDateStr = oContext.getProperty("TO");
          // Split the string and convert to a Date object (months are 0-indexed in JS!)
          const [day, month, year] = sToDateStr.split("-");
          const oToDate = new Date(year, month - 1, day);

          // Return true if the row's TO date is on or before the input date
          return oToDate <= oInputDate;
        }
      });

      // Apply the filter to the table
      oItemsBinding.filter([oDateFilter]);
    }
  });
});

Key Notes to Avoid Issues

  • Never compare dates as strings: Even if they look consistent, string comparison won't respect date logic (e.g., "13-02-2018" vs "01-03-2018" would be sorted incorrectly as strings).
  • Use getDateValue(): The DatePicker's getDateValue() method gives you a ready-to-use Date object, which is far more reliable than parsing strings manually.
  • Handle month indexing: JavaScript dates use 0-indexed months (January = 0, February = 1), so we subtract 1 from the month value when creating the Date object from your dd-MM-yyyy string.

This should resolve your filtering issue and show all rows where the "TO" date is on or before the date you select.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:28:42