本文针对本人这两天进行界面开发的过程中遇到的一些坑点和要点进行记录,留待日后备忘。

本篇博客将用一个极简的购物清单的例子讲解,例子的完整代码可见最后。

购物清单的数据结构说明

shoppingList: {
supermarket: '', // 超市名称,非空,字符串
status: true, // 状态位,默认为 true
items: [ // 购物清单项,0 到多个
{
name: '', // 商品名,非空,字符串
quantity: 0 // 数量,非空,整数
}
]
}

了解了购物清单的数据结构之后,就可以开始讲解了。

1. <el-radio>label 属性

根据官方文档label 属性可以为 StringNumberBoolean,但官方文档中没有说明的是,如果要给 label 属性设置 NumberBoolean 值,则需要加上冒号变成 :label,不然像 "0""true" 这类的值会被当作 String 处理。

<!--
感兴趣的可以试着去掉 label 前的冒号,
你会发现在页面中没有一个 radio 处于选中状态,
提交表单的时候对应的 status 会变成 String 而非 Boolean
-->
<el-form-item prop="status" label="状态">
<el-radio v-model="shoppingList.status" :label="true">启用</el-radio>
<el-radio v-model="shoppingList.status" :label="false">禁用</el-radio>
</el-form-item>

2. 在 <el-table> 中放入表单组件

从购物清单的数据结构可以看出,清单项部分是可变的,在 element-ui 里不难解决,官方文档中提到过动态增减表单项。但出于项目的需要,我们希望清单项能够像表格那样编辑,于是自然就有了在表格中嵌入表单组件的做法。官方文档中有一个自定义列模板可以用来做这个。在本例中,代码如下:

<el-table :data="shoppingList.items">
<el-table-column type="index"></el-table-column>
<el-table-column label="商品名">
<template slot-scope="scope">
<el-form-item :prop="'items.' + scope.$index + '.name'"
:rules="[{ required: true, message: '请输入商品名称', trigger: 'blur' }]">
<el-input v-model="scope.row.name"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="数量">
<template slot-scope="scope">
<el-form-item :prop="'items.' + scope.$index + '.quantity'"
:rules="[{ required: true, message: '请输入商品数量' }, { type: 'number', message: '只能输入数字' }]">
<el-input v-model="scope.row.quantity"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column>
<template slot-scope="scope">
<el-button type="danger" size="mini" icon="el-icon-delete" @click="removeItem(scope.$index)"></el-button>
</template>
</el-table-column>
</el-table>

至于表单数据的绑定,在 <el-table-column><template> 元素中可以定义一个 slot-scope 属性(这里,该属性被定义为 "scope")。经过这样的设置之后,就可以通过 scope.$index 得到该行的索引,通过 scope.row 获得该行的元素,表单数据绑定就比较容易了。至于添加和删除表单项,就通过对 shoppingList.items 进行 pushsplice 来实现(参见完整代码)。

3. 表单验证时填写正确的 prop 属性

官方文档中,表单验证既可以在 Form 上传递所有的 rules,也可以在单个表单域上传递验证规则。但在动态增减的表单项中进行验证,需要十分注意设置正确的 prop 属性。既然时动态增减的表单项,那么这个 prop 应该也是动态的,于是我们使用 v-bind 指令(简写为 :)进行绑定,以下是一个例子:

<el-table-column label="商品名">
<template slot-scope="scope">
<el-form-item :prop="'items.' + scope.$index + '.name'"
:rules="[{ required: true, message: '请输入商品名称', trigger: 'blur' }]">
<el-input v-model="scope.row.name"></el-input>
</el-form-item>
</template>
</el-table-column>

上一节中我们知道 scope.$index 可以访问到该行数据对应的索引,于是就可以通过 :prop="'items.' + $index + '.name'" 进行绑定。注意这里不能写 :prop="scope.row.name",也不能省略冒号——前者会导致验证规则不起作用,后者会在运行中给出警告,要求填写正确的 prop 值。至于为什么一定要这种写法,个人认为可以类比到 Java 中遍历数组元素的两种方式

// 方式一
for (int i = 0; i < items.length; i++) {
// 遍历操作
}
// 方式二
for (Item item : items) {
// 遍历操作
}

对于方式二来说,item 在遍历过程中是会变化的,无法通过它唯一确定一条记录,而下标可以。对于动态增减的表单项,肯定要把一个验证规则应用到所有表单项,同时又要区分哪个项验证通过,哪个项验证不通过,在这种要求下,自然用下标定位成为了不二之选。

总结

以上为我在这两天的页面开发中遇到的一些坑点和要点,有任何问题或者其它需要商讨的点可以在评论区留言。

附:极简购物清单的完整代码

<template>
<div id="app">
<h2>A simple shopping list.</h2>
<el-form :model="shoppingList" label-width="100px" size="mini">
<el-form-item prop="supermarket" label="超市"
:rules="[{ required: true, message: '请输入超市名称', trigger: 'blur' }]">
<el-input v-model="shoppingList.supermarket"></el-input>
</el-form-item>
<el-form-item prop="status" label="状态">
<el-radio v-model="shoppingList.status" :label="true">启用</el-radio>
<el-radio v-model="shoppingList.status" :label="false">禁用</el-radio>
</el-form-item>
<el-divider></el-divider>
<el-form-item prop="items" label="购物清单">
<el-button type="text" icon="el-icon-plus" @click="addItem">添加</el-button>
<el-table :data="shoppingList.items">
<el-table-column type="index"></el-table-column>
<el-table-column label="商品名">
<template slot-scope="scope">
<el-form-item :prop="'items.' + scope.$index + '.name'"
:rules="[{ required: true, message: '请输入商品名称', trigger: 'blur' }]">
<el-input v-model="scope.row.name"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="数量">
<template slot-scope="scope">
<el-form-item :prop="'items.' + scope.$index + '.quantity'"
:rules="[{ required: true, message: '请输入商品数量' }, { type: 'number', message: '只能输入数字' }]">
<el-input v-model="scope.row.quantity"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column>
<template slot-scope="scope">
<el-button type="danger" size="mini" icon="el-icon-delete" @click="removeItem(scope.$index)"></el-button>
</template>
</el-table-column>
</el-table>
</el-form-item>
<el-button type="primary" @click="saveList">Submit!</el-button>
</el-form>
</div>
</template> <script>
export default {
name: 'app',
data () {
return {
shoppingList: {
supermarket: '',
status: true,
items: [
{
name: '',
quantity: 0
}
]
}
}
},
methods: {
addItem () {
this.shoppingList.items.push({ name: '', quantity: 0 })
},
removeItem (index) {
this.shoppingList.items.splice(index, 1)
},
saveList () {
console.log('购物列表为:' + JSON.stringify(this.shoppingList))
}
}
}
</script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

element-ui 开发备忘的更多相关文章

  1. 基于Prism.Windows的UWP开发备忘

    以前做UWP开发都是使用MvvmLight,主要是简单易上手,同时也写了很多MvvmLight的开发系列文章: UWP开发必备以及常用知识点总结 UWP开发之Mvvmlight实践九:基于MVVM的项 ...

  2. 基于Springboot+Mybatis+Element UI开发的钢贸供应链系统

    小蓝钢贸云供应链系统以销售.采购.库存及财务一体化的设计理念,从供应商到客户的销售流程,实现订单.货物.资金的全面管控,并能对成本进行准确的跟踪与分析,为销售决策提供依据. 基于SpringBoot2 ...

  3. JS开发备忘笔记-- Javascript中document.execCommand()的用法

    document.execCommand()方法处理Html数据时常用语法格式如下:document.execCommand(sCommand[,交互方式, 动态参数]) 其中:sCommand为指令 ...

  4. 移动端web app开发备忘

    近期要做个手机html5的页面,做些知识储备,重要的点记录下来以备兴许. 1.devicePixelRatio:定义设备物理象素和设备独立象素的比例.css中的px能够看作是设备的独立象素.通过dev ...

  5. 【WPF开发备忘】使用MVVM模式开发中列表控件内的按钮事件无法触发解决方法

    实际使用MVVM进行WPF开发的时候,可能会用到列表控件中每行一个编辑或删除按钮,这时直接去绑定,发现无法响应: <DataGridTemplateColumn Header="操作& ...

  6. Laravel 6.X + Vue.js 2.X + Element UI 开发知乎流程

    本流程参照:CODECASTS的Laravel Vuejs 实战:开发知乎 视频教程 1项目环境配置和用户表设计 2Laravel 开发知乎:用户注册 3Laravel 开发知乎:用户登录 4Lara ...

  7. Git开发备忘

    1.在Git中,上传了中文命名的文件,但是后面想删除的时候,发现中文命名被转义了. 利用Git add是无法添加这类文件的,所以这里我们需要用到 git add -u命令,即可实现成功添加. 2.在G ...

  8. 开发备忘:AngularJS Syntax error, unrecognized expression in template file

    在写基于Angular的项目过程中,运行 grunt test的时候,一直给我蹦出这个错误,导致我的test一直跑不过,怎么试都是失败,经过重复排查,发现是因为template file中的html元 ...

  9. IMX515开发备忘

    1.多个PAD可以选择为同样的功能引脚 IMX515处理器一个PAD可以作为多种功能引脚,比如EIM_D25可以作为UART3_RXD,定义如下: 图1 而处理还有一个另一个UART3_RXD的PAD ...

随机推荐

  1. AspNet Core结合Quartz使用定时任务且通过注入缓存或者配置参数

    一.经常在项目会用到定时任务同步数据或更新缓存等操作,在很久以前我们可能经常会用一个多线程或timer来做定时任务,这样能实现比较简单轻量级的任务:对于任务多且都调用频率不一样的任务,我们都会用到Qu ...

  2. 学习CSS Grid布局

    一. 重要术语: CSS Grid(网格) 布局(又称为 "Grid(网格)" ),是一个二维的基于网格的布局系统,它的目标是完全改变我们基于网格的用户界面的布局方式. FlexB ...

  3. java实现二叉树常见操作

    package com.xk.test.struct.newp; import java.util.ArrayList; import java.util.LinkedList; import jav ...

  4. 结合 Vue.observable 写一个简易 Vuex

    作为 Vue 全家桶的一员,Vuex 的重要性不言而喻,不管是用来管理状态,还是封装 Controler 都很好用 不过在一些体量较小的项目中,为了几个简单的状态或者处理函数而引入 Vuex,就像是高 ...

  5. django rest_framework vue 实现用户列表分页

    django rest_framework vue 实现用户列表分页 后端 配置urls # 导入view from api.appview.userListView import userListV ...

  6. 修改GIT已提交的用户名和邮箱

    修改GIT已提交的用户名和邮箱 原文:https://help.github.com/en/github/using-git/changing-author-info 说明 要更改在现有提交中记录的名 ...

  7. rhel7 学习第二天

    参加<Linux就该这么学>在线培训的第二天,学习了虚拟环环境的搭建和红帽7的安装,同时也学习了rhel7的基本命令格式,以及systemctl的使用.

  8. matplotlib---画等高线

    contour - 绘制等高线 mp.contour(x, y, z, 等高线条数,colors=颜色, linewidth=线宽)#等高线绘制 contourf - 填充等高线 mp.contour ...

  9. 极简让ingress-nginx最新版(0.25.0)跑起来

    裸步骤: 一,manadatory.yaml apiVersion: v1 kind: Namespace metadata: name: ingress-nginx labels: app.kube ...

  10. Python 编码错误解决方案

    Python 编码错误解决方案 Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 7: ordin ...