123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import BaseCurd from './BaseCurd'
- import MapBuilder from './MapBuilder'
- import bindPrototype from './bindPrototype'
- export default (Target, hasPagination = true) => ({
- name: 'BaseCurdList',
- extends: BaseCurd(Target),
- data: () => ({
- optionGroup: Object.freeze({}),
- query: Target.$$getQuery() || {},
- oQuery: {},
- pagination: {
- pageNo: 0,
- pageSize: 20
- },
- oPagination: {
- pageNo: 0,
- pageSize: 20
- },
- list: Object.freeze([]),
- total: 0,
- selectedData: null,
- selectedList: Object.freeze([]),
- editorName: 'editor'
- }),
- methods: {
- afterLoadList () {
- this.selectedList = Object.freeze([])
- this.selectedData = null
- setTimeout(() => {
- this.$refs.table && this.$refs.table.doLayout()
- }, 0)
- },
- loadList () {
- let query = Object.assign({}, this.query)
- if (hasPagination) {
- Object.assign(query, this.pagination)
- this.$$request(Target.$$api.getPagination, this.query, this.pagination)
- .then(data => {
- const { count, list } = data
- const { pagination } = this
- const { pageSize } = pagination
- if (list.length > 0 || pagination.pageNo === 1) {
- this.total = count
- this.list = Object.freeze(bindPrototype(data.list, Target))
- this.afterLoadList()
- return
- }
- pagination.pageNo = Math.floor(count / pageSize)
- this.loadList()
- }).catch(console.error).finally(() => {})
- } else {
- this.$$request(Target.$$api.getList, this.query).then(list => {
- this.total = list.length
- this.list = Object.freeze(bindPrototype(list, Target))
- this.afterLoadList()
- }).catch(console.error).finally(() => {})
- }
- },
- currentPageNoChange () {
- this.loadList()
- },
- onResetBtnClick () {
- this.query = JSON.parse(JSON.stringify(this.oQuery))
- this.pagination = { ...this.oPagination }
- this.loadList()
- },
- onSearchBtnClick () {
- this.pagination.pageNo = 1
- this.loadList()
- },
- onSelectionChange (selection) {
- this.selectedList = Object.freeze(selection)
- },
- onRowClick (data) {
- this.selectedData = data
- },
- onOpenEditorBtnClick (data, isView) {
- const editor = this.getEditor()
- data instanceof Event ? editor.open() : editor.open(data, isView)
- },
- deleteSuccess () {
- this.loadList()
- this.$notify({
- title: '成功',
- message: '删除成功',
- type: 'success',
- position: 'bottom-right'
- })
- },
- deleteFailed () {
- this.$notify.error({
- title: '失败',
- message: '删除失败',
- position: 'bottom-right'
- })
- },
- deleteData (data) {
- this.$$request(Target.$$api.delete, data, Target)
- .then(this.deleteSuccess)
- .catch(this.deleteFailed)
- .finally(() => {})
- },
- onDeleteBtnClick (data) {
- const name = this[this.$$nameProp]
- this.$confirm(`此操作将删除${name ? `'${name}'` : '该记录'}, 是否继续?`, '确认', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- confirmButtonClass: 'confirm-dialog-confirm-btn',
- cancelButtonClass: 'confirm-dialog-cancel-btn',
- type: 'warning'
- }).then(() => {
- this.deleteData(data)
- }).catch(() => {})
- },
- onSaved () {
- this.loadList()
- },
- getEditor () {
- return this.$refs[this.editorName]
- },
- getOptions (optionTargetConfigs, optionGroup) {
- const { query } = this
- return Promise.all(
- optionTargetConfigs.map(
- optionTargetConfig => {
- const optionTarget = optionTargetConfig.Target
- return this.$$request(optionTarget.$$api.getList, typeof optionTargetConfig.getQuery === 'function' ? optionTargetConfig.getQuery(optionGroup) : undefined).then(options => {
- options = bindPrototype(options, optionTarget)
- optionGroup[optionTarget.name] = {
- list: options,
- map: MapBuilder.getObjectMap(options, optionTarget.$$idProp),
- Target: optionTarget
- }
- const { setQuery } = optionTargetConfig
- typeof setQuery === 'function' && setQuery(options, query, optionTarget)
- })
- })
- )
- },
- async getOptionGroup () {
- const optionGroup = {}
- const { $$optionTargetConfigGroup } = Target
- if (Array.isArray($$optionTargetConfigGroup)) {
- const $$optionTargetConfigGroupLen = $$optionTargetConfigGroup.length
- for (let i = 0; i < $$optionTargetConfigGroupLen; i++) {
- const optionTargetConfigs = $$optionTargetConfigGroup[i]
- const optionTargetConfigsLen = optionTargetConfigs.length
- if (optionTargetConfigsLen > 0) {
- await this.getOptions(optionTargetConfigs, optionGroup)
- }
- }
- }
- return Object.freeze(optionGroup)
- },
- setOptionGroup () {
- this.getOptionGroup(Target).then(optionGroup => {
- this.optionGroup = optionGroup
- Target.$$optionData = optionGroup
- this.oQuery = Object.freeze(JSON.parse(JSON.stringify(this.query)))
- this.loadList()
- })
- }
- },
- created () {
- this.setOptionGroup()
- }
- })
|