I have come quite far with my first app and have various picker lists working well. However, I am stuck thinking through how to do this next picker.
My data model has Golf Course name and Tee. For each golf course there are multiple Tees and not all the same across the Courses.
Example of Data
CourseA RedTee CourseA GreenTee CourseA BlueTee CouseB RedTee CourseB YellowTee CourseB WhiteTee
I first give the client the ability to Pick a Course from a picker list. That works fine. Now I want to create a Picker list of Tees but only for that selected Course.
So if the client selected CourseB they would be presented with these Tees to select from RedTee YellowTee WhiteTee
How do I limit the second picker to only show the Tees for the selected Course?
Then in an associated question, once i have the Course and the Tee, I want to Auto fill the rest of a form with Slope, Rating and Yardage.
@Model
class ScorecardData {
var scorecardcourseName: String
var scorecardTee: String
var scorecardSlope: Double
var scorecardRating: Double
var scorecardYardage: Int
Here is my code for the Course Picker List
Picker("", selection: $selectedCourse) {
Text("Select a Course").tag(" ")
ForEach(courses, id: \.self) { course in
Text(course.courseName)
.tag(course.courseName)
}
}
.onChange(of: selectedCourse) {
if(selectedCourse != nil) {
roundsdata.roundscourseName = selectedCourse!
}
}
Here is my current Picker list but its pulling all Tees for all Courses
Picker("", selection: $selectedTee) {
Text("Select Tee").tag(" ")
ForEach (tees, id: \.self) { tee in
Text(tee.scorecardTee)
.tag(tee.scorecardTee)
}
}
.onChange(of: selectedTee) {
if(selectedTee != nil) {
roundsdata.roundsTee = selectedTee!
}
}
My @State and @Query statements are as follows in case there is a change there that is needed as well,
@State private var selectedTee: String? = "Select Tee"
@Query(sort: \ScorecardData.scorecardcourseName) private var tees: [ScorecardData]
@State private var selectedCourse: String? = "Select Course"
@Query(sort: \CourseData.courseName) private var courses: [CourseData]