Displaying unique value in dropdown

Certified Associate Developer

I have 2 dropdowns type and courses type will be premium, free and courses for premium java, c++, and also for free also I have java , python like this . i need to display all the courses when in default and when type selected as premium it will only show that category courses. but java is in both category it displaying 2 times by default . In db they have mapped with thier unique type ids only. I tried with union but choice values and choice values length varies because of that. Can anyone help me ?

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Senior Developer

    When I read "unique" my first thought is the union() function.  I'm not sure where you're dropdown values come from or how your objects are configured, but you could try something like below...

    a!localVariables(
      
      local!selectedType,
      local!selectedCourse,
      local!dropdownOptionsMap: {
        a!map(course: "Java", type: "Premium"),
        a!map(course: "C++", type: "Premium"),
        a!map(course: "Appian", type: "Premium"),
        a!map(course: "Java", type: "Free"),
        a!map(course: "Python", type: "Free"),
        a!map(course: "Javascript", type: "Free"),
      },
      
      local!allCourseOptions: union(local!dropdownOptionsMap.course,local!dropdownOptionsMap.course),
      
      local!courseOptions: if(
        a!isNotNullOrEmpty(local!selectedType),
        index(local!dropdownOptionsMap, wherecontains(local!selectedType, local!dropdownOptionsMap.type)).course,
        local!allCourseOptions
      ),
      
      
      {
        a!dropdownField(
          label: "Course Type",
          placeholder: "Select Course Type",
          choiceLabels: union(local!dropdownOptionsMap.type,local!dropdownOptionsMap.type),
          choiceValues: union(local!dropdownOptionsMap.type,local!dropdownOptionsMap.type),
          value: local!selectedType,
          saveInto: {
            local!selectedType,
            a!save(local!selectedCourse, null)
          }
        ),
        a!dropdownField(
          label: "Course",
          placeholder: "Select Course",
          choiceLabels: local!courseOptions,
          choiceValues: local!courseOptions,
          value: local!selectedCourse,
          saveInto: {
            local!selectedCourse,
            a!save(
              local!selectedType,
              if(
                and(
                  a!isNotNullOrEmpty(save!value),
                  length(
                    index(
                      local!dropdownOptionsMap,
                      wherecontains(
                        save!value,
                        local!dropdownOptionsMap.course
                      )
                    ).type
                  ) = 1
                ),
                index(
                  local!dropdownOptionsMap,
                  wherecontains(
                    save!value,
                    local!dropdownOptionsMap.course
                  )
                ).type,
                null
              )
            )
          }
        )
      }
    )

    The list of course options begins with all unique values in the local!dropdownOptionsMap.course.  Notice Java is listed twice in local!dropdownOptionsMap.course.  To get the unique list I just used union(local!dropdownOptionsMap.course, local!dropdownOptionsMap.course).

    Added some logic in course selection to set the type if the selected course is unique in the list of options and logic in the type selection to clear away any previously selected course.

    Hope this helps!

Reply
  • 0
    Certified Senior Developer

    When I read "unique" my first thought is the union() function.  I'm not sure where you're dropdown values come from or how your objects are configured, but you could try something like below...

    a!localVariables(
      
      local!selectedType,
      local!selectedCourse,
      local!dropdownOptionsMap: {
        a!map(course: "Java", type: "Premium"),
        a!map(course: "C++", type: "Premium"),
        a!map(course: "Appian", type: "Premium"),
        a!map(course: "Java", type: "Free"),
        a!map(course: "Python", type: "Free"),
        a!map(course: "Javascript", type: "Free"),
      },
      
      local!allCourseOptions: union(local!dropdownOptionsMap.course,local!dropdownOptionsMap.course),
      
      local!courseOptions: if(
        a!isNotNullOrEmpty(local!selectedType),
        index(local!dropdownOptionsMap, wherecontains(local!selectedType, local!dropdownOptionsMap.type)).course,
        local!allCourseOptions
      ),
      
      
      {
        a!dropdownField(
          label: "Course Type",
          placeholder: "Select Course Type",
          choiceLabels: union(local!dropdownOptionsMap.type,local!dropdownOptionsMap.type),
          choiceValues: union(local!dropdownOptionsMap.type,local!dropdownOptionsMap.type),
          value: local!selectedType,
          saveInto: {
            local!selectedType,
            a!save(local!selectedCourse, null)
          }
        ),
        a!dropdownField(
          label: "Course",
          placeholder: "Select Course",
          choiceLabels: local!courseOptions,
          choiceValues: local!courseOptions,
          value: local!selectedCourse,
          saveInto: {
            local!selectedCourse,
            a!save(
              local!selectedType,
              if(
                and(
                  a!isNotNullOrEmpty(save!value),
                  length(
                    index(
                      local!dropdownOptionsMap,
                      wherecontains(
                        save!value,
                        local!dropdownOptionsMap.course
                      )
                    ).type
                  ) = 1
                ),
                index(
                  local!dropdownOptionsMap,
                  wherecontains(
                    save!value,
                    local!dropdownOptionsMap.course
                  )
                ).type,
                null
              )
            )
          }
        )
      }
    )

    The list of course options begins with all unique values in the local!dropdownOptionsMap.course.  Notice Java is listed twice in local!dropdownOptionsMap.course.  To get the unique list I just used union(local!dropdownOptionsMap.course, local!dropdownOptionsMap.course).

    Added some logic in course selection to set the type if the selected course is unique in the list of options and logic in the type selection to clear away any previously selected course.

    Hope this helps!

Children