CRUD的UD / ICON / confirmDialog - day06

前情提要

前几编文章里,大家已经知道如何利用 Vaadin-on-Kotlin 简单快速的新增、查询资料库并使用Grid显示,也知道了如何验证输入资料的正确性。先祝大家中秋节愉快,今天轻松写,来完成单一资料表的CRUD吧!

修改

  • 写完C、R,接着进入U,开新档 EditStudents.kt
package com.example.vok

import com.github.mvysny.karibudsl.v10.*
import com.vaadin.flow.router.BeforeEvent
import com.vaadin.flow.router.HasUrlParameter
import com.vaadin.flow.router.Route

@Route("edit-student", layout = MainLayout::class)
class EditStudent : KComposite(), HasUrlParameter<Long> {
    private val binder = beanValidationBinder<Student>()
    private var student: Student? = null
    private val root = ui {
        verticalLayout {
            h1("学生资料修改")
            textField("姓名 : "){
                bind(binder).bind(Student::name)
            }
            comboBox<Gender>("性别 : "){
                setItems(*Gender.values())
                bind(binder).bind(Student::gender)
            }
            datePicker("生日 : "){
                bind(binder).bind(Student::birthday)
            }
            numberField("身高"){
                bind(binder).bind(Student::height)
            }
            numberField("体重"){
                bind(binder).bind(Student::weight)
            }
            button("储存"){
                onLeftClick {
                    val student = student!!
                    if (binder.validate().isOk && binder.writeBeanIfValid(student)){
                        student.save()
                        StudentView.navigateTo(student.id!!)
                    }
                }
            }
            routerLink(null, "返回", AllStudentsView::class)
        }
    }

    override fun setParameter(event: BeforeEvent?, studentId: Long?) {
        binder.readBean(Student.getById(studentId!!))
    }
    
    companion object{
        fun navigateTo(studentId: Long) = navigateToView(EditStudent::class, studentId)
    }
}

HasUrlParameter说明请参考d04
binder和栏位绑定後,只要使用readBean() 方法读取bean,画面立即自动更新。
而在companion object navigateTo()方法则是提供外部快速跳转本页的"入口"。

JavaBeans是Java中一种特殊的类,可以将多个物件封装到一个物件(bean)中。特点是可序列化,提供无参建构元,提供getter方法和setter方法存取物件的属性。名称中的「Bean」是用於Java的可重用软件组件的惯用叫法。 -- 维基百科

  • 现在要在Grid里加上两栏,分别导到单笔显示页(student)和编辑页(edit-student)。请打开AllStudentView.kt
        addColumn(NativeButtonRenderer("Show", {StudentView.navigateTo(it.id!!)}))
        addColumn(NativeButtonRenderer("Edit", {EditStudent.navigateTo(it.id!!)}))

NativeButtonRenderer()方法有2个参数,第一个是button显示文字,第二个参数则是 clickListener

  • 在单笔学生资料显示页加入修改连结。请打开StudentView.kt

    1. 定义连结
    private lateinit var editLink: RouterLink
  1. 在setParameter()方法设定连结内容导致编辑页 EditStudent
    editLink.setRoute(EditStudent::class, student.id!!)
  1. 在画面上加入连结
    editLink = routerLink(VaadinIcon.EDIT,null)

在此使用图形连结,Vaadin提供了许多Icon,可直接使用 enum VaadinIcon取得图示

  1. 再加上返回连结
    routerLink(VaadinIcon.ARROW_LEFT, null, viewType = AllStudentsView::class)

执行结果如下 :
https://ithelp.ithome.com.tw/upload/images/20210921/20138680sPf4vjPIjD.png

删除

  • 在Grid里加一栏删除键。请打开AllStudentView.kt
            addColumn(NativeButtonRenderer("Delete") {
                confirmDialog {
                    it.delete()
                    this.refresh()
                }
            })

第2行,按下Delete後,跳出确认框,预设讯息为 "Are you sure?",若欲自订讯息,请改为 :

                confirmDialog(text = "是否确定删除${it.name}的资料?") {
                    it.delete()
                    this.refresh()
                }

执行结果如下:
https://ithelp.ithome.com.tw/upload/images/20210921/20138680vLYCwR1vLb.png

单一资料表的 CRUD 到本日告一个段落,是不是很简单呢?
明天要开始进入自订可重复使用的 Component了,明天见~

本日程序已上传 GitHub


<<:  JavaScript 伪装命名空间(Facking NameSpace)

>>:  Day9-D3绘图:绘制形状的Helper Functions

Day 11 Compose Click Event

今年的疫情蛮严重的,希望大家都过得安好,希望疫情快点过去,能回到一些线下技术聚会的时光~ 今天目标:...

EP 12 - [TDD] 设定环境变数

Youtube 频道:https://www.youtube.com/c/kaochenlong ...

Day1 学习目标与动机

这学期我加入了学校的实验室,而学长给我们的第一个作业呢,就是要我们去了解到基本网页架构是怎麽形成的,...

DAY 18 制作 Nav Bar - dropdown

针对 dropdown 的部分,我们要来细节微调他的 style ,让他符合 vogue 上的设计,...

来举例一下 Neo4j 的实务应用

前情提要 最後一篇正篇,稍稍回顾了一下之前的每一篇 发现对於现实上的使用案例,好像没有太多的描述 所...