组件.Form表单和控件

12003

使用Vuetify.js的表单组件, 了解更多Vuetify.js V2

表单组件

v-text-field:单行文本输入框

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <span class="jh-input-label">学生名</span>
  4. <v-text-field v-model="studentName" class="jh-v-input" dense single-line filled></v-text-field>
  5. </v-col>
  6. </v-app>
  7. <script type="module">
  8. new Vue({
  9. el: '#app',
  10. template: '#app-template',
  11. vuetify: new Vuetify(),
  12. data: () => ({
  13. studentName: '小明',
  14. }),
  15. })
  16. </script>

v-form:表单校验

  1. <jh-toast />
  2. <jh-mask />
  3. <jh-confirm-dialog />
  4. <v-app>
  5. <v-form ref="createForm" lazy-validation>
  6. <v-row no-gutters>
  7. <span class="text-h7 font-weight-bold pa-4 pl-0">表单组件</span>
  8. </v-row>
  9. <v-divider class="jh-divider"></v-divider>
  10. <v-row>
  11. <v-col cols="12" sm="12" md="4">
  12. <span class="jh-input-label">学生名</span>
  13. <v-text-field v-model="studentName" :rules="validationRules.studentName" class="jh-v-input" dense single-line filled></v-text-field>
  14. </v-col>
  15. </v-row>
  16. <v-row class="justify-end mx-0 my-8 px-4">
  17. <v-btn color="success" small @click="doUiAction('createStudent')">保存</v-btn>
  18. </v-row>
  19. </v-form>
  20. </v-app>
  21. <script type="module">
  22. new Vue({
  23. el: '#app',
  24. template: '#app-template',
  25. vuetify: new Vuetify(),
  26. data: () => ({
  27. validationRules: {
  28. studentName: [
  29. v => !!v || '学生名字是必填项',
  30. v => (v && v.length >= 2) || '学生名字至少需要2个字符',
  31. ],
  32. },
  33. studentName: '张',
  34. }),
  35. async created() {
  36. },
  37. mounted() {
  38. },
  39. methods: {
  40. async doUiAction(uiActionId, uiActionData) {
  41. switch (uiActionId) {
  42. case 'createStudent':
  43. await this.prepareCreateValidate();
  44. await this.confirmCreateItemDialog();
  45. await this.doCreateStudent();
  46. break;
  47. default:
  48. console.error("[doUiAction] uiActionId not find", { uiActionId });
  49. break;
  50. }
  51. },
  52. async prepareCreateValidate() {
  53. if (await this.$refs.createForm.validate()) {
  54. return true;
  55. }
  56. throw new Error("请完善表单信息")
  57. },
  58. async confirmCreateItemDialog() {
  59. if (await window.confirmDialog({title: "新增", content: "确定新增吗?"}) === false) {
  60. throw new Error("[confirmCreateFormDialog] 否");
  61. }
  62. },
  63. async doCreateStudent() {
  64. window.vtoast.success("保存")
  65. },
  66. }
  67. })
  68. </script>

v-textarea:多行文本输入框

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <span class="jh-input-label">评论</span>
  4. <v-textarea v-model="comment" rows="3" class="jh-v-input" dense single-line filled></v-textarea>
  5. </v-col>
  6. </v-app>
  7. <script type="module">
  8. new Vue({
  9. el: '#app',
  10. template: '#app-template',
  11. vuetify: new Vuetify(),
  12. data: () => ({
  13. comment: '评论内容...',
  14. }),
  15. })
  16. </script>

v-select:下拉选择框

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <span class="jh-input-label">选择省</span>
  4. <v-select v-model="selectedState" :items="provinceList" class="jh-v-input" dense single-line filled></v-select>
  5. </v-col>
  6. </v-app>
  7. <script type="module">
  8. new Vue({
  9. el: '#app',
  10. template: '#app-template',
  11. vuetify: new Vuetify(),
  12. data: () => ({
  13. selectedState: '四川省',
  14. provinceList: ['四川省', '湖北省', '湖南省', '云南省', '山东省', '浙江省', '江苏省']
  15. }),
  16. })
  17. </script>

v-autocomplete:自动完成输入框

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <span class="jh-input-label">水果搜索</span>
  4. <v-autocomplete v-model="selectedItem" :items="items" hint="开始打字..." persistent-hint class="jh-v-input" dense single-line filled></v-autocomplete>
  5. </v-col>
  6. </v-app>
  7. <script type="module">
  8. new Vue({
  9. el: '#app',
  10. template: '#app-template',
  11. vuetify: new Vuetify(),
  12. data: () => ({
  13. selectedItem: null,
  14. items: ['苹果', '香蕉', '橙子', '草莓', '葡萄'],
  15. }),
  16. })
  17. </script>

v-checkbox:复选框

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <v-checkbox v-model="termsAccepted" label="同意条款" class="jh-v-input" dense single-line filled></v-checkbox>
  4. </v-col>
  5. </v-app>
  6. <script type="module">
  7. new Vue({
  8. el: '#app',
  9. template: '#app-template',
  10. vuetify: new Vuetify(),
  11. data: () => ({
  12. termsAccepted: false,
  13. }),
  14. })
  15. </script>

v-radio-group:单选按钮组

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <span class="jh-input-label">选择季节</span>
  4. <v-radio-group v-model="favSeason" class="jh-v-input" dense single-line filled>
  5. <v-radio value="春季" label="春季" class="jh-v-input"/>
  6. <v-radio value="夏季" label="夏季" class="jh-v-input"/>
  7. <v-radio value="秋季" label="秋季" class="jh-v-input"/>
  8. <v-radio value="冬季" label="冬季" class="jh-v-input"/>
  9. </v-radio-group>
  10. </v-col>
  11. </v-app>
  12. <script type="module">
  13. new Vue({
  14. el: '#app',
  15. template: '#app-template',
  16. vuetify: new Vuetify(),
  17. data: () => ({
  18. favSeason: '夏季',
  19. }),
  20. })
  21. </script>

v-switch:开关组件

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <span class="jh-input-label">开关</span>
  4. <v-switch v-model="switchEnable" class="jh-v-input" dense single-line filled></v-switch>
  5. </v-col>
  6. </v-app>
  7. <script type="module">
  8. new Vue({
  9. el: '#app',
  10. template: '#app-template',
  11. vuetify: new Vuetify(),
  12. data: () => ({
  13. switchEnable: true,
  14. }),
  15. })
  16. </script>

v-slider:滑块

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <span class="jh-input-label">音量</span>
  4. <v-slider v-model="volume" max="100" thumb-label class="jh-v-input" dense single-line filled></v-slider>
  5. </v-col>
  6. </v-app>
  7. <script type="module">
  8. new Vue({
  9. el: '#app',
  10. template: '#app-template',
  11. vuetify: new Vuetify(),
  12. data: () => ({
  13. volume: 50,
  14. }),
  15. })
  16. </script>

v-range-slider:范围滑块

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <span class="jh-input-label">价格范围</span>
  4. <v-range-slider v-model="rangeValues" min="0" max="100" class="jh-v-input" dense single-line filled></v-range-slider>
  5. </v-col>
  6. </v-app>
  7. <script type="module">
  8. new Vue({
  9. el: '#app',
  10. template: '#app-template',
  11. vuetify: new Vuetify(),
  12. data: () => ({
  13. rangeValues: [0, 100],
  14. }),
  15. })
  16. </script>

v-rating:评分组件

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <span class="jh-input-label">评分</span>
  4. <v-rating v-model="rating" :readonly="false" color="yellow darken-3" background-color="grey darken-1" class="jh-v-input" dense single-line filled></v-rating>
  5. </v-col>
  6. </v-app>
  7. <script type="module">
  8. new Vue({
  9. el: '#app',
  10. template: '#app-template',
  11. vuetify: new Vuetify(),
  12. data: () => ({
  13. rating: 3,
  14. }),
  15. })
  16. </script>

v-file-input:文件选择

  1. <v-app>
  2. <v-col cols="12" sm="12" md="4">
  3. <span class="jh-input-label">上传文件</span>
  4. <v-file-input v-model="file" class="jh-v-input" dense filled single-line chips label="请选择文件" ></v-file-input>
  5. </v-col>
  6. </v-app>
  7. <script type="module">
  8. new Vue({
  9. el: '#app',
  10. template: '#app-template',
  11. vuetify: new Vuetify(),
  12. data: () => ({
  13. file: null,
  14. }),
  15. })
  16. </script>