iView表格列筛选的组件封装 发表于 2019-01-15 | 分类于 Vue | | 阅读次数: 字数统计: 1.8k 字 | 阅读时长 ≈ 9 分钟 当表格列过多时,可设置筛选选择要展示的列 tableColumnFilter.vue 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768<template> <div> <Button type="primary" @click="showFilterPanelFlag = !showFilterPanelFlag"> <span class="icon_filter"></span> </Button> <transition> <div v-show="showFilterPanelFlag" class="filter-panel"> <CheckboxGroup v-model="selected" @on-change="checkAllGroupChange"> <div v-for="item in filterFiled" :key="item"> <Checkbox :label="item" style="margin: 2px 5px"></Checkbox> </div> </CheckboxGroup> </div> </transition> </div></template><script> export default { name: "tableColumnFilter", props: { filterFiled: { type: Array, default: function () { return ['标题', '年度', '版本', '创建人', '创建时间', '使用状态'] } }, defaultCheck: { type: Array } }, data() { return { showFilterPanelFlag: false, //已选中的数据 selected: [...this.defaultCheck], } }, methods: { checkAllGroupChange() { this.$emit('filter-field-change', this.selected); } } }</script><style scoped lang="less"> .filter-panel { width: 166px; min-height: 120px; height: 500px; position: absolute; background-color: white; z-index: 9999; margin-left: -113px; overflow-y: scroll; } .icon_filter { display: inline-block; width: 14px; height: 14px; position: relative; top: 5px; background: url("filter.png") no-repeat center center; }</style> example.vue 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371<template> <div> <Row type="flex" justify="space-between" align="middle"> <Col> <div class="alertTips"> <Alert> 可筛选要展示的列 </Alert> </div> </Col> <Col> <tableColumnFilter :filterFiled="[ '学期', '年级', '学院', '专业', '课程名称', '课程代码', '教学班名称', '实际班级人数', '是否录入成绩', '是否排课', '是否选课', '成绩录入人', '连排节数', '周学时', '学时', '学分', '成绩分制', '老师端展示形式', '学生端展现形式', '学生选课比例', '是否需要校验地点冲突', '是否需要校验教师冲突', '是否需要校验学生冲突', '答疑学时', '授课教师', '上课周次', ]" :defaultCheck="[ '学期', '年级', '学院', '专业', '课程名称', '课程代码', '教学班名称', '实际班级人数' ]" @filter-field-change="refreshTable"> </tableColumnFilter> </Col> </Row> <div> <Table :data="tableData" :columns="tableColumns" border></Table> </div> </div></template><script> import tableColumnFilter from './tableColumnFilter' export default { name: 'taskManager', data() { let index = { title: ' ', type: 'index', width: 60, align: 'center' }; let term = { title: '学期', key: 'term', align: 'center', customOrder: 2 }; let grade = { title: '年级', key: 'grade', align: 'center', customOrder: 3 }; let departmentName = { title: '学院', key: 'departmentName', align: 'center', customOrder: 4 }; let major = { title: '专业', key: 'major', align: 'center', customOrder: 5 }; let courseName = { title: '课程名称', key: 'courseName', align: 'center', customOrder: 6 }; let courseCode = { title: '课程代码', key: 'courseCode', align: 'center', customOrder: 7 }; let className = { title: '教学班名称', key: 'className', align: 'center', customOrder: 8 }; let classCount = { title: '实际班级人数', key: 'classCount', align: 'center', customOrder: 9, render: (h, params) => { return h('a', {}, params.row.classCount) } }; const hasScore = { title: '是否录入成绩', key: 'hasScore', align: 'center', customOrder: 10 }; let hasSchedule = { title: '是否排课', key: 'hasSchedule', align: 'center', customOrder: 11 }; let hasChoose = { title: '是否选课', key: 'hasChoose', align: 'center', customOrder: 12 }; let scorePerson = { title: '成绩录入人', key: 'scorePerson', align: 'center', customOrder: 13 }; let sequentNum = { title: '连排节数', key: 'sequentNum', align: 'center', customOrder: 14 }; let weekTime = { title: '周学时', key: 'weekTime', align: 'center', customOrder: 15 }; let time = { title: '学时', key: 'time', align: 'center', customOrder: 16 }; let credit = { title: '学分', key: 'credit', align: 'center', customOrder: 17 }; let scoreFormat = { title: '成绩分制', key: 'scoreFormat', align: 'center', customOrder: 18 }; let displayTeacher = { title: '老师端展示形式', key: 'displayTeacher', align: 'center', customOrder: 19 }; let displayStudent = { title: '学生端展现形式', key: 'displayStudent', align: 'center', customOrder: 20 }; let percent = { title: '学生选课比例', key: 'percent', align: 'center', customOrder: 21 }; let conflictPlace = { title: '是否需要校验地点冲突', key: 'conflictPlace', align: 'center', customOrder: 22 }; let conflictTeacher = { title: '是否需要校验教师冲突', key: 'conflictTeacher', align: 'center', customOrder: 23 }; let conflictStudent = { title: '是否需要校验学生冲突', key: 'conflictStudent', align: 'center', customOrder: 24 }; let questionTime = { title: '答疑学时', key: 'questionTime', align: 'center', customOrder: 25 }; let teacher = { title: '授课教师', key: 'teacher', align: 'center', customOrder: 26 }; let week = { title: '上课周次', key: 'week', align: 'center', customOrder: 27 }; let action = { title: '操作', key: 'action', width: 160, align: 'center', render: (h, params) => { let readView = h('a', { on: { click: () => { console.log(12); } } }, '管理学生名单'); return h('div', {}, [readView]); }, }; return { tableColumns: [ index, term, grade, departmentName, major, courseName, courseCode, className, classCount, action ], tableData: [ { term: '第一学期', grade: '2018级', departmentName: '食品学院', major: '食品加工技术', courseName: '大学生心里健康', courseCode: '98903782', className: '2018级体育-食品营养与健康1班', classCount: '100', hasScore: '是', hasSchedule: '是', hasChoose: '是', scorePerson: 'zhang', sequentNum: '2', weekTime: '2', time: '2', credit: '2', scoreFormat: '百分', displayTeacher: '百分', displayStudent: '百分', percent: '100%', conflictPlace: '是', conflictTeacher: '是', conflictStudent: '是', questionTime: '2', teacher: '2', week: '2', } ], //固定的两个列 index, action, //用于匹配 labelInfo: { 学期: 'term', 年级: 'grade', 学院: 'departmentName', 专业: 'major', 课程名称: 'courseName', 课程代码: 'courseCode', 教学班名称: 'className', 实际班级人数: 'classCount', 是否录入成绩: 'hasScore', 是否排课: 'hasSchedule', 是否选课: 'hasChoose', 成绩录入人: 'scorePerson', 连排节数: 'sequentNum', 周学时: 'weekTime', 学时: 'time', 学分: 'credit', 成绩分制: 'scoreFormat', 老师端展示形式: 'displayTeacher', 学生端展现形式: 'displayStudent', 学生选课比例: 'percent', 是否需要校验地点冲突: 'conflictPlace', 是否需要校验教师冲突: 'conflictTeacher', 是否需要校验学生冲突: 'conflictStudent', 答疑学时: 'questionTime', 授课教师: 'teacher', 上课周次: 'week', }, //所有列 tableFieldInfo: { term: term, grade: grade, departmentName: departmentName, major: major, courseName: courseName, courseCode: courseCode, className: className, classCount: classCount, hasScore: hasScore, hasSchedule: hasSchedule, hasChoose: hasChoose, scorePerson: scorePerson, sequentNum: sequentNum, weekTime: weekTime, time: time, credit: credit, scoreFormat: scoreFormat, displayTeacher: displayTeacher, displayStudent: displayStudent, percent: percent, conflictPlace: conflictPlace, conflictTeacher: conflictTeacher, conflictStudent: conflictStudent, questionTime: questionTime, teacher: teacher, week: week, } } }, methods: { //过滤字段,刷新表格显示内容 refreshTable(selectedField) { let showField = []; for (let item of selectedField) { let fieldName = this.labelInfo[item]; showField.push(this.tableFieldInfo[fieldName]) } if (showField.length) { showField.sort((a, b) => { return a.customOrder > b.customOrder }) } //序号和操作固定 this.tableColumns = [this.index, ...showField, this.action]; }, }, components: { tableColumnFilter, } }</script><style scoped></style>