• 10-Element UI


    1) ElementUI

    安装

    npm install element-ui -S
    
    • 1

    引入组件

    import Element from 'element-ui'
    import 'element-ui/lib/theme-chalk/index.css'
    
    Vue.use(Element)
    
    • 1
    • 2
    • 3
    • 4

    测试,在自己的组件中使用 ElementUI 的组件

    <el-button>按钮el-button>
    
    • 1
    表格组件
    <template>
        <div>
            <el-table :data="students">
                <el-table-column label="编号" prop="id">el-table-column>
                <el-table-column label="姓名" prop="name">el-table-column>
                <el-table-column label="性别" prop="sex">el-table-column>
                <el-table-column label="年龄" prop="age">el-table-column>
            el-table>
        div>
    template>
    <script>
    import axios from '../util/myaxios'
    const options = {
        async mounted() {
            const resp = await axios.get('/api/students');
            this.students = resp.data.data
        },
        data() {
            return {
                students: []
            }
        }
    }
    export default options;
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    分页组件
    <template>
        <div>
            <el-table v-bind:data="students">
                <el-table-column label="编号" prop="id">el-table-column>
                <el-table-column label="姓名" prop="name">el-table-column>
                <el-table-column label="性别" prop="sex">el-table-column>
                <el-table-column label="年龄" prop="age">el-table-column>
            el-table>
            <el-pagination 
                :total="total"
                :page-size="queryDto.size"
                :current-page="queryDto.page"
                layout="prev,pager,next,sizes,->,total"
                :page-sizes="[5,10,15,20]"
                @current-change="currentChange"
                @size-change="sizeChange"
            >el-pagination>
        div>
    template>
    <script>
    import axios from '../util/myaxios'
    const options = {
        mounted() {
            this.query();
        },
        methods: {
            currentChange(page) {
                this.queryDto.page = page;
                this.query();
            },
            sizeChange(size){
                this.queryDto.size = size;
                this.query();
            },
            async query() {
                const resp = await axios.get('/api/students/q', {
                    params: this.queryDto
                });
                this.students = resp.data.data.list;
                this.total = resp.data.data.total;
            }
        },
        data() {
            return {
                students: [],
                total: 0,
                queryDto: {
                    page: 1,
                    size: 5
                }
            }
        }
    }
    export default options;
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 三种情况都应该触发查询
      • mounted 组件挂载完成后
      • 页号变化时
      • 页大小变化时
    • 查询传参应该根据后台需求,灵活采用不同方式
      • 本例中因为是 get 请求,无法采用请求体,只能用 params 方式传参
    • 返回响应的格式也许会很复杂,需要掌握【根据返回的响应结构,获取数据】的能力
    分页搜索
    <template>
        <div>
            <el-input placeholder="请输入姓名" size="mini" v-model="queryDto.name">el-input>
            <el-select placeholder="请选择性别" size="mini" v-model="queryDto.sex" clearable>
                <el-option value="">el-option>
                <el-option value="">el-option>
            el-select>
            <el-select placeholder="请选择年龄" size="mini" v-model="queryDto.age" clearable>
                <el-option value="0,20" label="0到20岁">el-option>
                <el-option value="21,30" label="21到30岁">el-option>
                <el-option value="31,40" label="31到40岁">el-option>
                <el-option value="41,120" label="41到120岁">el-option>
            el-select>
            <el-button type="primary" size="mini" @click="search()">搜索el-button>
            <el-divider>el-divider>
            <el-table v-bind:data="students">
                <el-table-column label="编号" prop="id">el-table-column>
                <el-table-column label="姓名" prop="name">el-table-column>
                <el-table-column label="性别" prop="sex">el-table-column>
                <el-table-column label="年龄" prop="age">el-table-column>
            el-table>
            <el-pagination :total="total" :page-size="queryDto.size" :current-page="queryDto.page"
                layout="prev,pager,next,sizes,->,total" :page-sizes="[5, 10, 15, 20]" @current-change="currentChange"
                @size-change="sizeChange">el-pagination>
        div>
    template>
    <script>
    import axios from '../util/myaxios'
    const options = {
        mounted() {
            this.query();
        },
        methods: {
            currentChange(page) {
                this.queryDto.page = page;
                this.query();
            },
            sizeChange(size) {
                this.queryDto.size = size;
                this.query();
            },
            async query() {
                const resp = await axios.get('/api/students/q', {
                    params: this.queryDto
                });
                this.students = resp.data.data.list;
                this.total = resp.data.data.total;
            },
            search() {
                this.query();
            }
        },
        data() {
            return {
                students: [],
                total: 0,
                queryDto: {
                    name: '',
                    sex: '',
                    age: '',  
                    page: 1,
                    size: 5
                }
            }
        }
    }
    export default options;
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • sex 与 age 均用 '' 表示用户没有选择的情况
    • age 取值 0,20 会被 spring 转换为 new int[]{0, 20}
    • age 取值 '' 会被 spring 转换为 new int[0]
    级联选择

    级联选择器中选项的数据结构为

    [
        {value:100, label:'主页',children:[
            {value:101, label:'菜单1', children:[
                {value:105, label:'子项1'},
                {value:106, label:'子项2'}
            ]},
            {value:102, label:'菜单2', children:[
                {value:107, label:'子项3'},
                {value:108, label:'子项4'},
                {value:109, label:'子项5'}
            ]},
            {value:103, label:'菜单3', children:[
                {value:110, label:'子项6'},
                {value:111, label:'子项7'}
            ]},
            {value:104, label:'菜单4'}
        ]}
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    下面的例子是将后端返回的一维数组【树化】

    <template>
        <el-cascader :options="ops">el-cascader>
    template>
    <script>
    import axios from '../util/myaxios'
    const options = {
        async mounted() {
            const resp = await axios.get('/api/menu')
            console.log(resp.data.data)
            const array = resp.data.data;
    
            const map = new Map(); 
    
            // 1. 将所有数据存入 map 集合(为了接下来查找效率)
            for(const {id,name,pid} of array) {
                map.set(id, {value:id, label:name, pid:pid})
            }
            // 2. 建立父子关系
            // 3. 找到顶层对象
            const top = [];
            for(const obj of map.values()) {
                const parent = map.get(obj.pid);
                if(parent !== undefined) {
                    parent.children ??= [];
                    parent.children.push(obj);
                } else {
                    top.push(obj)
                }
            }
            this.ops = top;
        },
        data(){
            return {
                ops: []
            }
        }
    };
    export default options;
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    css文字竖向且英文字母不倒向
    ORA-01940 无法删除当前已连接的用户之解决方案(脚本)
    AI变现之数字人工具库账号引流
    ASP.NET Core WEB API 使用element-ui文件上传组件el-upload执行手动文件文件,并在文件上传后清空文件
    基于当量因子法、InVEST、SolVES模型等多技术融合在生态系统服务功能社会价值评估中的应用及论文写作、拓展分析
    动手学深度学习_目标检测算法 R-CNN 系列
    <Xcode> Xcode IOS无开发者账号打包和分发
    【云原生之Docker实战】使用Docker部署FireShare轻量视频分享平台
    Python CT图像预处理——nii格式读取、重采样、窗宽窗位设置
    kafka集群配置
  • 原文地址:https://blog.csdn.net/qwy715229258163/article/details/128009280