9.5 C
London
Sunday, September 15, 2024

json – Find out how to create knowledge tables with all columns displayed in Swift iOS?


I only recently began studying Swift 5 and located that my desk had just one column and never the remaining columns from my json knowledge. Please may anybody recommend how you can show all of the columns for a knowledge desk? Additionally, how can I search and kind a desk. I’ve used jQuery DataTables and I do not know how you can construct these DataTables in Swift 5.

Here is my code:
DataProvider.swift

import Basis
import SwiftUI

class DataProvider: ObservableObject {
    @Revealed var companies:[Company] = []
    let url: URL
    
    func getBusinessList(){
        if let url = Bundle.foremost.url(forResource: "corporations", withExtension: "json") {
               do {
                   let knowledge = attempt Information(contentsOf: url)
                   let decoder = JSONDecoder()
                   let jsonData = attempt decoder.decode([Company].self, from: knowledge)
                   self.companies = jsonData
               } catch {
                   print("error:(error)")
               }

           }
    }
    
    init(){
        self.url = URL(string: "http://localhost/output_json.php")!
        self.getBusinessList()
    }

}
struct Firm: Codable, Hashable, Identifiable {
    var id: Int { s_no }
    var firm, tackle, fax, telephone, web site: String
    var s_no: Int
}

ContentView.swift

import SwiftUI
import SwiftData

struct ContentView: View {
    @Surroundings(.modelContext) personal var modelContext
    @EnvironmentObject var supplier: DataProvider
    
    var physique: some View {
        Desk( supplier.companies) {
            TableColumn("Identify", worth:.firm)
                TableColumn("Web site", worth:.web site)
            }
            }
    }


#Preview {
    ContentView()
        .modelContainer(for: Merchandise.self, inMemory: true)
        .environmentObject(DataProvider())
}

ListViewerApp.swift

import SwiftUI
import SwiftData

@foremost
struct List_ViewerApp: App {
    var sharedModelContainer: ModelContainer = {
        let schema = Schema([
            Item.self,
        ])
        let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)

        do {
            return attempt ModelContainer(for: schema, configurations: [modelConfiguration])
        } catch {
            fatalError("Couldn't create ModelContainer: (error)")
        }
    }()

    var physique: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(sharedModelContainer)
        .environmentObject(DataProvider())
    }
}

Latest news

A Slice of AI

Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here