Submit button being disabled if no changes are made to the editable grid

Under the grid, display a SAVE button. The button should be disabled by default.
If any changes are made to the grid (i.e. if a row is added, a row is removed, or data entered for a row is changed), the button should be enabled. After the button is clicked, it should become disabled again. Any help will be appreciated.

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Set a LOAD variable (or if on 19.2, a localVariable with no autorefresh) equal to the original value of the data used to populate the grid.  Then set a with / autorefreshing variable that compares the original data with the current data, which evaluates to (for example) true() if changes have been made.  Use that variable to help determine the state of the SAVE button.  And finally, when SAVE is clicked, you would by some means cause the "original value" variable to be updated with the current data.

  • Hello ,

    Please check if below code snippet works for you.Look for this local variable  local!submitDisabled. 

    load(
      local!submitDisabled: true,
    
      
      local!departments: {
        "Finance",
        "Human Resources",
        "Marketing",
        "Sales",
        "IT"
      },
      local!categories: {
        "Hardware",
        "Software",
        "Office Supplies",
        "Miscellaneous"
      },
      local!addresses: {
        {
          id: 1,
          shippingAddress: "123 Main St",
          unit: "",
          city: "Springfield",
          stateOrProvince: "AL",
          zipCode: "36784"
        },
        {
          id: 2,
          shippingAddress: "62201 Oak Blvd",
          unit: "",
          city: "Phoenix",
          stateOrProvince: "AZ",
          zipCode: "85032"
        },
        {
          id: 3,
          shippingAddress: "872 Park Ave",
          unit: "179",
          city: "Milwaukee",
          stateOrProvince: "WI",
          zipCode: "53201"
        },
        {
          id: 4,
          shippingAddress: "11955 Democracy Dr",
          unit: "1700",
          city: "Reston",
          stateOrProvince: "VA",
          zipCode: "20190"
        }
      },
      a!formLayout(
        label: "New Purchase Request",
        contents: {
          a!gridLayout(
            label: "Items",
            headerCells: {
              a!gridLayoutHeaderCell(
                label: "Description"
              ),
              a!gridLayoutHeaderCell(
                label: "Category"
              ),
              a!gridLayoutHeaderCell(
                label: "Qty",
                align: "RIGHT"
              ),
              a!gridLayoutHeaderCell(
                label: "Unit Price",
                align: "RIGHT"
              )
            },
            columnConfigs: {
              a!gridLayoutColumnConfig(
                width: "DISTRIBUTE",
                weight: 3
              ),
              a!gridLayoutColumnConfig(
                width: "DISTRIBUTE",
                weight: 3
              ),
              a!gridLayoutColumnConfig(
                width: "DISTRIBUTE"
              ),
              a!gridLayoutColumnConfig(
                width: "DISTRIBUTE",
                weight: 2
              )
            },
            rows: {
              a!forEach(
                items: ri!purchaseRequest.items,
                expression: a!gridRowLayout(
                  contents: {
                    a!textField(
                      value: fv!item.desc,
                      saveInto: {
                        fv!item.desc,
                        a!save(
                          local!submitDisabled,
                          false
                        )
                      },
                      required: true,
                      validations: {
                        if(
                          and(
                            not(
                              isnull(
                                fv!item.desc
                              )
                            ),
                            len(
                              fv!item.desc
                            ) => 100
                          ),
                          "Description must be less than 100 characters",
                          ""
                        )
                      },
                      validationGroup: "main"
                    ),
                    a!dropdownField(
                      placeholderLabel: "--- Select a Category ---",
                      choiceLabels: local!categories,
                      choiceValues: local!categories,
                      value: fv!item.category,
                      saveInto: {
                        fv!item.category,
                        a!save(
                          local!submitDisabled,
                          false
                        )
                      },
                      required: true,
                      validationGroup: "main"
                    ),
                    a!integerField(
                      value: fv!item.qty,
                      saveInto: {
                        fv!item.qty,
                        a!save(
                          local!submitDisabled,
                          false
                        )
                      },
                      required: true,
                      validations: {
                        if(
                          tointeger(
                            fv!item.qty
                          ) < 1,
                          "Quantity must be greater than zero",
                          ""
                        )
                      },
                      validationGroup: "main",
                      align: "RIGHT"
                    ),
                    a!floatingPointField(
                      value: fv!item.unitPrice,
                      saveInto: {
                        fv!item.unitPrice,
                        a!save(
                          local!submitDisabled,
                          false
                        )
                      },
                      required: true,
                      validations: {
                        if(
                          todecimal(
                            fv!item.unitPrice
                          ) < 0,
                          "Price must be greater than zero",
                          ""
                        )
                      },
                      validationGroup: "main",
                      align: "RIGHT"
                    )
                  }
                )
              ),
              a!gridRowLayout(
                contents: {
                  a!textField(
                    readOnly: true
                  ),
                  a!textField(
                    readOnly: true
                  ),
                  a!textField(
                    readOnly: true
                  ),
                  a!textField(
                    value: "Total",
                    readOnly: true,
                    align: "RIGHT"
                  )
                }
              )
            },
            addRowLink: a!dynamicLink(
              label: "Add New Item",
              saveInto: {
                a!save(
                  local!submitDisabled,
                  false
                ),
                a!save(
                  ri!purchaseRequest.items,
                  append(
                    ri!purchaseRequest.items,
                    {
                      desc: "",
                      category: "",
                      qty: null,
                      unitPrice: null
                    }
                  )
                )
              }
            ),
            rowHeader: 1
          )
        },
        buttons: a!buttonLayout(
          primaryButtons: {
            a!buttonWidget(
              label: "Submit",
              saveInto: {},
              submit: true,
              style: "PRIMARY",
              /* Prevent form submission while a new address is being configured */
              disabled: local!submitDisabled,
              validationGroup: "main"
            )
          },
          secondaryButtons: {
            a!buttonWidget(
              label: "Cancel",
              value: true,
              saveInto: ri!cancel,
              submit: true,
              validate: false
            )
          }
        )
      )
    )

  • I actually have a column as editable--text which iam saving if any editing to the description column also...How can I recognise those changes made to each row and enable the save button based on that

  • 0
    Certified Lead Developer
    in reply to RAHUL GANJI

    The method I described should still be sufficient I think.  Or are you trying to recognize changes on a row-by-row basis?  If you only have one SAVE button for the whole form (which was my assumption), then you shouldn't need to worry about this.

  • +1
    Certified Lead Developer
    in reply to RAHUL GANJI

    Here's my quick version of what I was thinking of.  I In my example the main data is in local!itemList, though in a real implementation that could be replaced with a rule input value feeding in external (and editable) data.  The with() variable takes care of keeping track of whether any changes have been made, with the added bonus that it'll turn back off if changes are reverted.

    load(
      local!itemList: {
        {
          item: "Sofa",
          status: "unoccupied"
        },
        {
          item: "TV",
          status: "off"
        }
      },
      local!itemListOriginalValue: local!itemList,
      with(
        local!changesMade: not(
          exact(
            tostring(local!itemList),
            tostring(local!itemListOriginalValue)
          )
        ),
        
        a!formLayout(
          contents: {
            a!gridLayout(
              headerCells: {
                a!gridLayoutHeaderCell(
                  label: "Item"
                ),
                a!gridLayoutHeaderCell(
                  label: "Status"
                )
              },
              rows: a!forEach(
                local!itemList,
                a!gridRowLayout(
                  contents: {
                    a!textField(
                      value: fv!item.item,
                      readonly: true()
                    ),
                    a!textField(
                      value: fv!item.status,
                      saveInto: fv!item.status,
                      readOnly: false()
                    )
                  }
                )
              )
            )
          },
          buttons: a!buttonLayout(
            primaryButtons: a!buttonWidget(
              label: "SAVE",
              style: "PRIMARY",
              disabled: not(local!changesMade),
              saveInto: {
                a!save(
                  local!itemListOriginalValue,
                  local!itemList
                )
              }
            )
          )
        )
      )
    )

  • thats working for the attaching of a new document to the grid but it is not working for the editable column in the grid when the description in the column changes and we need to enable the submit button for that

  • If I edit the description only the save button should be enabled and I should be able to save the changes 

    I think I couldnot do it this way

  • You are taking it as a CDT right?but I have 2 different lists for documents and their description...I couldnot understand how to handle this

  • 0
    Certified Lead Developer
    in reply to RAHUL GANJI

    Sorry, I'm not sure I follow -- you didn't mention documents at all before.  Perhaps you can share a bit more detail as to what you're actually trying to do with your grid?

  • I have a grid which displays the document(can download the doc through documentdownload link),document description,uploaded on,remove columns..

    Attaching a new file link is in the main interface..We can attach the documents row by row...

    I have a seperate expression rule for the rows displaying (all the code for deleting included there)

    Iam able to disable the save first and enable it for the attachment link only...iam unable to do it for the document description,remove columns...Can you plz help me out