77 <el-tooltip class =" item" effect =" dark" content =" 刷新" placement =" top" >
88 <el-button circle icon =" Refresh" @click =" refresh()" />
99 </el-tooltip >
10- <el-tooltip class =" item" effect =" dark" content =" 显隐列" placement =" top" v-if =" columns" >
10+ <el-tooltip class =" item" effect =" dark" content =" 显隐列" placement =" top" v-if =" Object.keys( columns).length > 0 " >
1111 <el-button circle icon =" Menu" @click =" showColumn()" v-if =" showColumnsType == 'transfer'" />
1212 <el-dropdown trigger =" click" :hide-on-click =" false" style =" padding-left : 12px " v-if =" showColumnsType == 'checkbox'" >
1313 <el-button circle icon =" Menu" />
1818 <el-checkbox :indeterminate =" isIndeterminate" v-model =" isChecked" @change =" toggleCheckAll" > 列展示 </el-checkbox >
1919 </el-dropdown-item >
2020 <div class =" check-line" ></div >
21- <template v-for =" item in columns " :key =" item .key " >
21+ <template v-for =" ( item , key ) in columns " :key =" item .key " >
2222 <el-dropdown-item >
23- <el-checkbox v-model =" item.visible" @change =" checkboxChange($event, item.label )" :label =" item.label" />
23+ <el-checkbox v-model =" item.visible" @change =" checkboxChange($event, key )" :label =" item.label" />
2424 </el-dropdown-item >
2525 </template >
2626 </el-dropdown-menu >
3232 <el-transfer
3333 :titles =" ['显示', '隐藏']"
3434 v-model =" value"
35- :data =" columns "
35+ :data =" transferData "
3636 @change =" dataChange"
3737 ></el-transfer >
3838 </el-dialog >
@@ -46,9 +46,10 @@ const props = defineProps({
4646 type: Boolean ,
4747 default: true
4848 },
49- /* 显隐列信息 */
49+ /* 显隐列信息(数组格式、对象格式) */
5050 columns: {
51- type: Array
51+ type: [Array , Object ],
52+ default : () => ({})
5253 },
5354 /* 是否显示检索图标 */
5455 search: {
@@ -82,14 +83,15 @@ const style = computed(() => {
8283 ret .marginRight = ` ${ props .gutter / 2 } px`
8384 }
8485 return ret
85- });
86+ })
8687
8788// 是否全选/半选 状态
8889const isChecked = computed ({
89- get : () => props .columns . every (col => col .visible ),
90+ get : () => Array . isArray ( props .columns ) ? props . columns . every (col => col . visible ) : Object . values ( props . columns ). every (( col ) => col .visible ),
9091 set : () => {}
9192})
92- const isIndeterminate = computed (() => props .columns .some ((col ) => col .visible ) && ! isChecked .value )
93+ const isIndeterminate = computed (() => Array .isArray (props .columns ) ? props .columns .some ((col ) => col .visible ) && ! isChecked .value : Object .values (props .columns ).some ((col ) => col .visible ) && ! isChecked .value )
94+ const transferData = computed (() => Array .isArray (props .columns ) ? props .columns .map ((item , index ) => ({ key: index, label: item .label })) : Object .keys (props .columns ).map ((key , index ) => ({ key: index, label: props .columns [key].label })))
9395
9496// 搜索
9597function toggleSearch () {
@@ -98,14 +100,20 @@ function toggleSearch() {
98100
99101// 刷新
100102function refresh () {
101- emits (" queryTable" );
103+ emits (" queryTable" )
102104}
103105
104106// 右侧列表元素变化
105107function dataChange (data ) {
106- for (let item in props .columns ) {
107- const key = props .columns [item].key
108- props .columns [item].visible = ! data .includes (key)
108+ if (Array .isArray (props .columns )) {
109+ for (let item in props .columns ) {
110+ const key = props .columns [item].key
111+ props .columns [item].visible = ! data .includes (key)
112+ }
113+ } else {
114+ Object .keys (props .columns ).forEach ((key , index ) => {
115+ props .columns [key].visible = ! data .includes (index)
116+ })
109117 }
110118}
111119
@@ -114,24 +122,40 @@ function showColumn() {
114122 open .value = true
115123}
116124
117- if (props .showColumnsType == ' transfer' ) {
118- // 显隐列初始默认隐藏列
119- for (let item in props .columns ) {
120- if (props .columns [item].visible === false ) {
121- value .value .push (parseInt (item))
125+ if (props .showColumnsType == " transfer" ) {
126+ // transfer穿梭显隐列初始默认隐藏列
127+ if (Array .isArray (props .columns )) {
128+ for (let item in props .columns ) {
129+ if (props .columns [item].visible === false ) {
130+ value .value .push (parseInt (item))
131+ }
122132 }
133+ } else {
134+ Object .keys (props .columns ).forEach ((key , index ) => {
135+ if (props .columns [key].visible === false ) {
136+ value .value .push (index)
137+ }
138+ })
123139 }
124140}
125141
126142// 单勾选
127- function checkboxChange (event , label ) {
128- props .columns .filter (item => item .label == label)[0 ].visible = event
143+ function checkboxChange (event , key ) {
144+ if (Array .isArray (props .columns )) {
145+ props .columns .filter (item => item .key == key)[0 ].visible = event
146+ } else {
147+ props .columns [key].visible = event
148+ }
129149}
130150
131151// 切换全选/反选
132152function toggleCheckAll () {
133153 const newValue = ! isChecked .value
134- props .columns .forEach ((col ) => (col .visible = newValue))
154+ if (Array .isArray (props .columns )) {
155+ props .columns .forEach ((col ) => (col .visible = newValue))
156+ } else {
157+ Object .values (props .columns ).forEach ((col ) => (col .visible = newValue))
158+ }
135159}
136160 </script >
137161
0 commit comments