14.ElementUI

ElementUI

笔记目录:(https://www.cnblogs.com/wenjie2000/p/16378441.html)

视频教程(P149~P153)

  • Element:是饿了么公司前端开发团队提供的一套基于Vue的网站组件库,用于快速构建网页

  • 组件:组成网页的部件,例如超链接、按钮、图片、表格等等~

  • 自己完成的按钮

  • Element 提供的按钮

  • Element官网:https://element-plus.org/zh-CN/#/zh-CN(基于 Vue 3)

    https://element.eleme.cn/#/zh-CNListener(基于 Vue 2)

    我后面用的都是基于vue2的。如果想使用基于vue3的版本,可以去看官网,也有官方教程。

Element快速入门

  1. 引入Element的css、js文件和Vue.js

    <!-- 引入vue2 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    
  2. 创建Vue核心对象

    new Vue({
        el: "#app",
    })
    
  3. 官网复制Element组件代码

    例如:https://element.eleme.cn/2.15/#/zh-CN/component/button

    将代码放到div中(<style>部分的代码需要放在div外面),页面就能正常显示了。至于复制的代码是什么意思,不需要看懂,这都是element自己定义的标签,会用会修改就行。有不会的部分就去官网查(各项参数说明在各个组件页面的最下面)

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <!-- 引入样式 -->
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <!-- 引入组件库 -->
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    </head>
    <body>
    <div id="app">
        <el-row>
            ......//这里内容比较多,自己去官网复制,后面也是
        </el-row>
    </div>
    <script>
        new Vue({
            el: "#app",
        })
    </script>
    </body>
    </html>
    

Element布局

  • Element中有两种布局方式:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <!-- 引入vue2 -->
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <!-- 引入样式 -->
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <!-- 引入组件库 -->
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    	<style>
        ......//注意style部分别放在div中
    	</style>
    </head>
    <body>
    <div id="app">
          ......
    </div>
    
    <script>
        new Vue({
            el: "#app",
            ......
        })
    </script>
    </body>
    </html>
    

Element 组件

最终要实现以下页面内容:(要好好利用官网中现成的东西来进行改写)

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 引入vue2 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
</head>
<body>
<div id="app">
    <!--搜索表单-->
    <el-form :inline="true" :model="brand" class="demo-form-inline">
        <el-form-item label="当前状态">
            <el-select v-model="brand.status" placeholder="当前状态">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="企业名称">
            <el-input v-model="brand.companyName" placeholder="企业名称"></el-input>
        </el-form-item>
        <el-form-item label="品牌名称">
            <el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>
    <!--按钮-->
    <el-row>
        <el-button type="primary" plain>批量删除</el-button>
        <el-button type="danger" @click="dialogVisible = true" plain>新增</el-button>
    </el-row>
    <!--添加数据对话框标表单-->
    <el-dialog
            title="编辑品牌"
            :visible.sync="dialogVisible"
            width="30%">
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="brand.status" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addBrand">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
  </span>
    </el-dialog>

    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    label="排序"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="当前状态"
                    align="center">
            </el-table-column>
            <el-table-column
                    label="操作"
                    align="center">
                <el-row>
                    <el-button type="primary">修改</el-button>
                    <el-button type="danger">删除</el-button>
                </el-row>
            </el-table-column>
        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="400">
    </el-pagination>
</div>
<style>

</style>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                //表格数据
                tableData: [{
                    brandName: '华为',
                    companyName: '华为公司',
                    ordered: '100',
                    status: '1',
                }, {
                    brandName: '华为',
                    companyName: '华为公司',
                    ordered: '100',
                    status: '1',
                }, {
                    brandName: '华为',
                    companyName: '华为公司',
                    ordered: '100',
                    status: '1',
                }, {
                    brandName: '华为',
                    companyName: '华为公司',
                    ordered: '100',
                    status: '1',
                }],
                //复选框选中数据集合
                multipleSelection: [],
                //搜索表单数据
                brand: {
                    status: '',
                    companyName: '',
                    brandName: '',
                    id:'',
                    ordered:'',
                    description:'',
                },
                //添加数据对话框是否展示的标记
                dialogVisible: false,
                //当前页码
                currentPage: 4,
            }
        },
        methods: {
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            //复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;
                // console.log(this.multipleSelection);
            },
            //查询方法
            onSubmit() {
                console.log(this.brand);
            },
            //添加数据
            addBrand(){
                console.log(this.brand);
            },
            //分页
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
            },
        },
    })
</script>
</body>
</html>
posted @ 2023-01-09 09:07  文杰2000  阅读(74)  评论(0编辑  收藏  举报