Now.js Framework Documentation

Now.js Framework Documentation

DateElementFactory

TH 13 Nov 2026 06:18

DateElementFactory

EmbeddedDateTime A hand-built date/time picker that replaces the native <input type="date"> and friends, so the calendar UI looks and behaves the same across browsers. Supports date, time and datetime-local modes. Instantiated once per element by DateElementFactory; the original input is hidden but kept so the form still submits its value.

หน้านี้สร้างจากซอร์สโดยตรงเมื่อ 2026-08-21 คำอธิบายมาจาก JSDoc ในไฟล์
รันซ้ำได้ด้วย docs_scaffold เมื่อโค้ดเปลี่ยน อย่าแก้ด้วยมือถ้าตั้งใจให้ generate ต่อ

ที่มา

  • ไฟล์: Now/js/DateElementFactory.js (1915 บรรทัด)
  • รูปแบบ: ES class

เมธอดสาธารณะ (17)

applyRangePreset(key)

Sets the range to a preset (today, yesterday, 7d, 30d, thismonth, lastmonth, thisyear) and closes the picker. Range mode only.

close()

Close the picker dropdown. Delegates to the shared DropdownPanel, which triggers the picker's own close callback.

static create(def)

Create a date field from a definition (ElementManager.create()). The input gets its type here ('date', 'datetime-local'; 'daterange' is a date input in range mode), because the picker takes its mode from that type.

static createInstance(element, config = {})

Get or create the element's instance state, same as the base ElementFactory.

destroy()

Tear the picker down: close it if open and remove its locale listener.

getDate()

The currently selected date.

getElement()

The wrapper element this picker rendered into.

getRange()

The two sides of a range picker: {start, end}, '' for an empty side, or null when the picker is not in range mode.

getValue()

The value currently stored for form submission.

nextMonth()

Advance the visible calendar by one month, rolling over into the next year.

open()

Open the picker dropdown. Ignored while the field is disabled, read-only, or already open. For a time-only picker the calendar grid is hidden and only the time controls show.

previousMonth()

Step the visible calendar back one month, rolling over into the previous year.

selectDate(date)

Choose a date, combining it with the current time for datetime-local mode.

setValue(value)

Set the picker's value programmatically. An empty value clears both the date and time state, not just the display.

setRange(start, end)

Sets both sides of a range picker without announcing a change (loading saved state is not an edit). An end before the start is swapped.

static setupElement(instance)

Replace the native date input with an EmbeddedDateTime picker. A definition given to ElementManager.create() reaches the picker as the attributes it reads: rangeEnd (data-range-end), rangePresets, rangeMaxDays, min and max.

toggle()

Open the picker if closed, close it if open.

ภาษา

ชื่อวันและเดือนใช้ภาษาปัจจุบันของ I18nManager ถ้ายังไม่มี (ปิด i18n หรือก่อน Now.init() โหลดภาษาเสร็จ)
ใช้ <html lang> แล้ว en — Utils.date.getLocale() และ CalendarManager ใช้ลำดับเดียวกัน

เหตุการณ์

เหตุการณ์เหล่านี้เป็น DOM CustomEvent ที่ dispatch บนอีลิเมนต์ จึงต้องรับด้วย
element.addEventListener() และอ่านค่าจาก event.detail

Event เมื่อเกิด event.detail
gdatetime:open เปิดปฏิทิน {type, value}
gdatetime:change เลือกวันหรือเวลา {type, value, date}
gdatetime:close ปิดปฏิทิน {type, value}
gdatetime:rangechange ตัวเลือกช่วงวันที่ได้ช่วงใหม่ (bubbles) {start, end, names: {start, end}}
element:change ค่าของ element เปลี่ยน {elementId, value, type}

ช่วงวันที่

input วันที่ที่มี data-range-end คือตัวเลือกช่วงวันที่: ช่องนี้เก็บวันเริ่ม ช่องที่ระบุชื่อไว้เก็บวันสิ้นสุด
และปฏิทินเดียวตั้งค่าทั้งสองฝั่ง เว้นว่างฝั่งใดฝั่งหนึ่งได้

<form data-form="report">
  <span class="form-control icon-calendar">
    <input type="date" name="from" data-range-end="to"
           data-range-presets="today,7d,thismonth" data-range-max-days="92">
  </span>
</form>
  • ช่องวันสิ้นสุดคือช่องชื่อนั้นในฟอร์มเดียวกัน (ใช้ <input type="hidden" name="to"> ถ้าต้องการผูก
    data-attr) ถ้าไม่มี ระบบสร้าง hidden input ไว้ข้างช่องวันเริ่มให้ ทั้งสองช่องส่งค่าในชื่อของตัวเอง
  • แถบ จาก / ถึง เลือกว่าวันที่คลิกถัดไปจะลงฝั่งไหน × ล้างเฉพาะฝั่งนั้น วันแรกคือวันเริ่ม
    วันที่สองคือวันสิ้นสุดและปิดปฏิทิน ถ้าเลือกวันก่อนวันเริ่ม วันนั้นกลายเป็นวันเริ่ม
  • data-range-presets เพิ่มปุ่มลัด data-range-max-days ปิดวันที่ห่างจากวันเริ่มเกินกำหนดขณะเลือกวันสิ้นสุด
  • ส่ง change ครั้งเดียวต่อหนึ่งช่วง ตอนปิดปฏิทิน: change ที่ช่องวันสิ้นสุดเมื่อค่าเปลี่ยน, change
    ที่ช่องวันเริ่ม แล้วตามด้วย gdatetime:rangechange
  • สร้างด้วยโค้ด: ElementManager.create('daterange', {name: 'from', rangeEnd: 'to', rangePresets: '7d,30d', rangeMaxDays: 31, onChange}) ตัวกรอง data-type="daterange"
    ของ TableManager สร้างด้วยวิธีนี้

min / max เทียบเป็นวันตามปฏิทิน วันที่เท่ากับ min จึงเลือกได้ในทุกเขตเวลา และ onChange
ที่ส่งมากับ definition ถูกเรียกทุกครั้งที่ค่าเปลี่ยน เหมือน select

Data attribute ที่ใช้

data-display-format · data-format · data-range-end · data-range-max-days · data-range-presets · data-show-seconds

พึ่งพาคลาสอื่น

DropdownPanel · ElementFactory · ElementManager · EventManager · Utils