如何解决使用javascript将网页滚动到给定位置表格内
在一个javascript网络应用程序中,我正在显示一个相当长的表,其中充满了来自firebase的数据,其代码如下所示。
struct NewTask: View {
// MARK: - Variables
@Binding var showSheet: Bool // To close the sheet view and go back to home
@State private var closeAlert: Bool = false // Close alert trigger
@State private var name: String = "" // Task's name
@State private var type: TaskType = TaskType.Homework // Task's type
@State private var subject: Subject? // << cannot create here,because MOC is not available
@State private var description: String = "" // Task's description
@State private var priority: Int = 1 // Task's priority
@State private var date: Date = Date() // Task's date
@State private var notificationEnabled: Bool = UserDefaults.standard.bool(forKey: "notificationsEnabled")
@Environment(\.managedObjectContext) var moc // Managed Object Context for Core Data managing
@FetchRequest(entity: Subject.entity(),sortDescriptors: []) var subjects: FetchedResults<Subject>
// MARK: - View body
var body: some View {
NavigationView {
Form {
// Name input
TextField("app.inputTask.name",text: $name)
// Description input
TextField("app.inputTask.description",text: $description)
// Priority input
HStack {
Stepper("app.inputTask.priority",value: $priority,in: 1...3)
Text(String(priority)).padding(.leading,10)
}
// Type input
Picker(selection: $type,label: Text("app.inputTask.type")) {
ForEach(TaskType.allCases,id: \.self) {
Text(LocalizedStringKey($0.rawValue))
}
}
// Subject selection
Picker(selection: $subject,label: Text("app.inputTask.subject")) {
ForEach(self.subjects,id: \.subjectID) { subject in
Text(subject.name)
}
}
// Date picker for date
DatePicker(LocalizedStringKey("app.inputTask.date"),selection: $date,in: Date()...,displayedComponents: [.date,.hourAndMinute])
Button(action: {
print("Materie:")
//print(subjects)
},label: {
Text("Testing")
})
}
.alert(isPresented: $closeAlert) {
Alert(title: Text("app.warning"),message: Text("app.warning.discardChanges"),primaryButton: .default(Text("app.buttons.confirm"),action: {
self.showSheet.toggle()
}),secondaryButton: .default(Text("app.buttons.cancel")))
}
.navigationBarTitle("app.tasks.newTask")
.navigationBarItems(leading: HStack {
Button(action: {
if !self.name.isEmpty || !self.description.isEmpty {
self.closeAlert.toggle()
} else {
self.showSheet.toggle()
}
}) {
Text("app.buttons.close").foregroundColor(Color.red).bold()
}
},trailing: HStack {
Button(action: {
// Create and save the object
let task = Task(context: self.moc)
task.name = self.name
task.descr = self.description
task.priority = self.priority
task.type = self.type.rawValue
task.date = self.date
task.taskID = UUID()
task.notificationID = UUID()
// Relationship
if let subject = self.subject {
task.subject = self.subject
subject.addToTasks(task)
}
// Schedule the notification for the task
if self.notificationEnabled == true {
scheduleNotification(title: name,body: description,date: date,notifUUID: task.notificationID)
}
// Close the new task view
self.showSheet.toggle()
}) {
Text("app.buttons.save").bold()
}
.disabled(self.name.isEmpty == true)
})
}
.onAppear {
// MOC is available here and results are available here,so
// setup default subject here !!!
self.subject = self.subjects.first ?? Subject(context: moc)
}
}
}
它可以按预期工作,最终我得到了表格中的项目列表。但是我还需要其他东西。
如何使窗口自动滚动到给定位置。例如,要使列表中的第157个项目显示在窗口顶部。我已经在网上搜索过,找到了一些尝试过的滚动功能,但没有成功。
请注意,类似的问题(JavaScript to scroll long page to DIV)不能解决问题。也许是因为它是一张桌子。
我尝试使用各种选项来遵循此处提供的解决方案,但无法使其正常工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。