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

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

购物清单的数据结构说明

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. oracle 10 升级补丁

    Ooacle 10g补丁安装方法 Windows 平台 方法: 1.  备份数据库:关闭数据库,拷贝,安装软件目录,数据文件拷到另一个地方 2.  关闭停止所有oracle 服务+Distribute ...

  2. SQlServer 变量定义 赋值

    declare @id int declare @name char(10) ;注意:char(10)为10位,要是位数小了会让数据出错 set @id=1 select @id=1 select @ ...

  3. DjangoDRF总结

    思维导图xmind文件:https://files-cdn.cnblogs.com/files/benjieming/DRF%E6%A8%A1%E5%9D%97.zip

  4. ObjC: Foundation Kit

    转自:http://marshal.easymorse.com/tech/objc-foundation-kit Foundation Kit是什么? 你可以把它看作Java JDK中的java.la ...

  5. vue+element 表格按需合并

    这个功能难度感觉一般般吧,记录一下,以后碰到了直接来复制,懒得再写了 效果如下: 前6列是合并,后面的有几行,动态显示几行 重点是在数据处理上面做文章,合并列大家都会,数据处理呢?这样来处理, 我们拿 ...

  6. linux中find命令的使用详解(转载)

    常用命令 find  (目录)   [-type d | f]  (文件夹 | 文件)   -name   (名称,可使用正则表达式) find  /root  -name "*core&q ...

  7. 使用gunicorn部署python web

    gunicorn 是一款支持wsgi的web服务器, 支持gevent 首先安装setuptools.  wget https://bootstrap.pypa.io/ez_setup.py $pyt ...

  8. TCP链接异常断开后,对端仍然ESTABLISH

    双方建立TCP链接,其中一方拔掉网线,另一端依然是ESTABLISHED,那么要过多长时间才会发觉链接被断开了呢? [root@node1 ~]# sysctl -a |grep keepalive ...

  9. 服务器学习--Linux基本操作指令

    小编后续会持续更新 1.修改服务器的hostname [root@mexihq ~]# hostname [root@mexihq ~]# hostnamectl set-hostname xxx P ...

  10. C#8.0接口默认实现特性

    文章:[译]C#8.0中一个使接口更加灵活的新特性-默认接口实现 原文示例代码: public interface IBook { void AddBook(string bookName, stri ...