defaultValueFn 사용 가이드

defaultValueFn 사용 가이드

defaultValueFn을 사용해 입력 필드의 기본값을 JavaScript로 설정할 수 있습니다.

  • 특정 필드의 기본값을 동적으로 설정
  • row 값을 직접 활용 가능
  • 기본값이 없을 경우 대체 값 지정 가능

예제 코드

아래는 postcode의 기본값을 설정하는 예제입니다.
값이 없을 경우 '00000'을 기본값으로 지정합니다.

menus:
- path: defaultvaluefn

pages:
- path: defaultvaluefn
  blocks:
  - type: query # 모달 안에서 적용
    resource: mysql.qa
    sqlType: select
    sql: >
      SELECT * FROM wine_order
    columns:
      memo:
        openModal: edit-:id
    
    modals: 
      - path: edit-:id
        blocks:
          - type: query
            sqlType: update
            resource: mysql.qa
            sql: |
              UPDATE wine_order
              SET postcode = :postcode
              WHERE id = :id
            params:
              - key: id
                valueFromRow: true
              - key: postcode
                # defaultValueFromRow: postcode
                defaultValueFn: |
                  if (!row.postcode) return '00000'
                  return row.postcode
            confirm: update?
            reloadAfterSubmit: true  

  - type: query # 모달 없이 적용
    resource: mysql.qa
    sqlType: select
    sql: >
      SELECT * FROM wine_order WHERE id = 6
    display: shadow
    blocks:
      - type: query
        title: update
        sqlType: update
        resource: mysql.qa
        sql: |
          UPDATE wine_order
          SET postcode = :postcode
          WHERE id = :id
        params:
          - key: id
            valueFromRow: true
          - key: postcode
            defaultValueFn: |
              console.log('>R', row)
              if (!row.postcode) return '00000'
              return row.postcode

modals에서 defaultValueFn 사용법을 더 확인하고 싶으신 경우 아래 예제를 참고해주세요.

menus:
- path: pages/ad70uf
  name: modals defaultValueFn 예시
pages:
- path: pages/ad70uf
  blocks:
  - type: query
    resource: mysql.sample
    sqlType: select
    sql: >
      SELECT 1 as id
    columns:
      id:
        buttons:
          - label: button            
            openModal: modal1
    modals:
      - path: modal1
        blocks:
          - type: query
            resource: mysql.sample
            sqlType: insert
            sql: >
              select 2
            params:
              - key: id
                valueFromRow: id
                hidden: false
              - key: id2
                defaultValueFn: |
                  console.log(row.id);
                  return row.id;
              - key: id3
                defaultValueFn: |
                  return DateTime.now().toFormat('yyyy-MM-dd');
              - key: id4
                defaultValueFn: |
                  const addMonths = row.id;
                  console.log(row.id)
                  const done_at = DateTime.now();
                  const result = done_at.plus({ months: addMonths }).toFormat('yyyy-MM-dd');
                  return result;