Reputation: 31
I'm trying to put a search-bar in my first app. I already searched about this topic and all that I found had an array as a constant, but I am using json.
import SwiftUI
let menu = Bundle.main.decode([MenuSection].self, from: "menu.json")
struct ContentView: View {
@State var buscar: String = ""
var body: some View {
NavigationView {
VStack {
List {
ForEach(menu) { section in
Section(header: Busqueda(text: self.$buscar)) {
ForEach(section.items) {item in
detalleLinea(item: item)
}
}
}
}.navigationBarTitle("Menu de opciones")
.listStyle(GroupedListStyle())
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I already tried different things, but none of it helped. Section(header: Busqueda(text: self.$buscar))
had the searchbar and works fine - I can see it in my app, but I want to apply the filter in the ForEach
inside
ForEach(section.items) {item in
detalleLinea(item: item)
}
Upvotes: 0
Views: 1393
Reputation: 31
it solved thank you for help LuLuGaGa and all
struct ContentView: View {
@State private var buscar: String = ""
var body: some View {
NavigationView {
VStack {
Busqueda(text: $buscar)
List(menu.filter {
buscar.isEmpty ?
true :
"\($0)".localizedCaseInsensitiveContains(buscar)
}, id: \.self) { item in
// ForEach(menu) {item1 in
detalleLinea(item: item)
// }
}
}.navigationBarTitle("Menu de opciones")
// .listStyle(GroupedListStyle())
}
}
}
Upvotes: 2