需求实现如下
重点使用到 @select
以及 @select-all
两个方法
返回数据格式
代码实现
< el-table ref = " tableRef" :cell-style = " { color: '#FFF', background: '#333' }"
:header-cell-style = " { color: '#FFF', background: '#333' }"
row-key = " id" :data = " tableData" style = " width : 100%"
:default-sort = " { prop: 'barcode.putInDate', order: 'descending' }"
@select = " select"
@select-all = " selectAll"
@sort-change = " sortChange" :tree-props = " {
children: 'children',
hasChildren: 'hasChildren',
}
" >
< template slot = " empty" >
< span style = " color : #969799" > {{ $t("NeoLight.empty") }}</ span>
</ template>
< el-table-column type = " selection" width = " 50" />
< el-table-column prop = " barcode" :sortable = " true" :label = " $t('NeoLight.barcodeNo')" width = " 180" />
< el-table-column prop = " partNumber" :sortable = " true" :label = " $t('NeoLight.stockNo')" width = " 150" />
< el-table-column prop = " produceDate" :sortable = " true" :label = " $t('NeoLight.produceDate')" />
< el-table-column prop = " expireDate" :sortable = " true" :label = " $t('NeoLight.expireDate')" />
< el-table-column prop = " batch" :sortable = " true" :label = " $t('barcode.batch')" />
< el-table-column prop = " provider" :sortable = " true" :label = " $t('storagePos.providerNumber')" />
< el-table-column prop = " posName" :sortable = " true" :label = " $t('NeoLight.storageNo')" />
</ el-table>
js方法
methods : {
getList ( ) {
getStoFind ( this . crud. query)
. then ( ( res ) => {
if ( res. totalElements) {
this . tableData = [ ] ;
this . ids = [ ]
this . totalPageNum = res. totalElements
res. content. forEach ( ( item ) => {
if ( item. barcode && item. barcode. children && item. barcode. children. length > 0 ) {
item. barcode. children. map ( ch => {
ch. posId = item. id
ch. parentId = item. barcode. id
} )
}
item. barcode. posId = item. id;
this . tableData. push ( item. barcode) ;
} ) ;
}
} )
} ,
select ( selection, row ) {
let vm = this
if ( selection. some ( ( el ) => row. id
=== el. id
) ) {
row. isChecked = true
this . addId ( row)
if ( row. children) {
row. children. map ( c => {
this . $refs. tableRef. toggleRowSelection ( c, true )
c. isChecked = true
this . addId ( c)
} )
}
if ( row. parentId) {
let pNode = vm. tableData. find ( x => x. id === row. parentId)
this . $refs. tableRef. toggleRowSelection ( pNode, true )
pNode. isChecked = true
this . addId ( pNode)
}
} else {
row. isChecked = false
this . deleteId ( row)
if ( row. children) {
row. children. map ( ( i ) => {
this . $refs. tableRef. toggleRowSelection ( i, false )
i. isChecked = false
this . deleteId ( i)
} )
}
if ( row. parentId) {
let pNode = vm. tableData. find ( x => x. id === row. parentId)
if ( ! pNode. children. some ( el => el. isChecked == true ) ) {
this . $refs. tableRef. toggleRowSelection ( pNode, false )
pNode. isChecked = false
this . deleteId ( pNode)
}
}
}
} ,
selectAll ( selection ) {
let rA = this . tableData. some ( el => {
let r = false
if ( el. children) {
r = el. children. some ( e => e. isChecked)
}
if ( r) return r
return el. isChecked
} )
if ( rA) {
this . tableData. forEach ( el => {
el. isChecked = false
this . $refs. tableRef. toggleRowSelection ( el, false )
this . deleteId ( el)
if ( el. children) {
el. children. forEach ( e => {
e. isChecked = false
this . $refs. tableRef. toggleRowSelection ( e, false )
this . deleteId ( e)
} )
}
} )
} else {
this . tableData. forEach ( el => {
el. isChecked = true
this . $refs. tableRef. toggleRowSelection ( el, true )
this . addId ( el)
if ( el. children) {
el. children. forEach ( e => {
e. isChecked = true
this . $refs. tableRef. toggleRowSelection ( e, true )
this . addId ( e)
} )
}
} )
}
} ,
addId ( o ) {
let id = o. posId
if ( this . ids. indexOf ( id) < 0 ) {
this . ids. push ( id)
}
} ,
deleteId ( o ) {
let id = o. posId
this . ids = this . ids. filter ( item => item !== id) ;
} ,
}