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

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

购物清单的数据结构说明

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. Linux系统SSH免密登录

    第一章 生成密钥 1.1 生成用户默认文件名的密钥 [root@localhost ~] ssh-keygen -t rsa # root用户下生成root用户的默认密钥 1.2 生成用户指定文件名的 ...

  2. MySQL 分库分表及其平滑扩容方案

    转自:https://kefeng.wang/2018/07/22/mysql-sharding/ 众所周知,数据库很容易成为应用系统的瓶颈.单机数据库的资源和处理能力有限,在高并发的分布式系统中,可 ...

  3. np.broadcast_to()的函数使用及维度增加的表达

    import numpy as npanchors=np.ones((2,3))anchor = np.broadcast_to(anchors, (5,)+anchors.shape) # 标红字体 ...

  4. 关于final

    最近见的一道选择题 刚学习一直认为final修饰,为常量,必须声明时被初始化,现在又明白第二种情况可以通过创建对象之后由构造方法立即初始化. 1.final修饰类不能被继承 2.final修饰方法不能 ...

  5. 4 Linux文件与目录管理

    Linux的目录文件结构为树状结构,最顶级目录为根目录 / 其他目录通过挂载可以将他们添加到树中,通过解除挂载可以移除他们 绝对路径: 根目录开始 eg: /usr/share/doc 相对路径: 不 ...

  6. 关于ORACLE图形化安装过程中出现的竖线的处理办法

    这种情况上传个jre 并指定下就好了 ~/database/runInstaller -jreLoc /usr/local/jre1.8.0_191/

  7. linux查看磁盘类型(是否SSD盘)

    介绍两种方法: 第一种: cat /sys/block/sda/queue/rotational 注意: 命令中的sba是你的磁盘名称,可以通过df命令查看磁盘,然后修改成你要的 结果: 返回0:SS ...

  8. node中glob模块总结

    参考文章:   githup_glob    node-glob学习 前言: 最近在学习webpack配置, 其中有一项glob配置入口文件, 来获取对应的文件名, 达到入口entry和output文 ...

  9. HDU3507:Print Article(斜率优化dp)

    传送门 题意: 现有\(n\)个数,每个数的值为\(a_i\),现在可以把数划分为多段,每一段的代价为\((\sum_{k=i}^{j}c_i)^2+M\). 问怎么划分,代价最小. 思路: 考虑dp ...

  10. 从架构开始谈dubbo(一)

    架构发展史 一.单体应用架构      当网站流量很小时,所有的功能写在一个项目中,打包部署在tomcat中.          例如:公司管理系统,超市的收银系统         也可以将单体应用部 ...