123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- import _ from 'lodash'
- import {grid} from '../Defaults'
- import {baseConfig} from "./base";
- import {invokeMethod} from "../utils";
- import {lookupFn, lookupScope} from "../lib/lib";
- import {serverInvokeUrlTransform} from "../lib/config";
- export default function () {
- Ext.define('Yvan.Grid', {
- extend: 'Ext.grid.Panel',
- xtype: 'yvgrid',
- constructor(config) {
- const me = this
- const {dataSource} = config
- const newConfig = _.defaultsDeep({
- // 强制性属性
- }, baseConfig(config, 'row-item'), config, grid)
- if (_.isPlainObject(dataSource) && dataSource.method === 'invoke') {
- newConfig.store = {
- autoLoad: newConfig.autoLoad,
- remoteSort: newConfig.remoteSort,
- remoteFilter: newConfig.remoteFilter,
- proxy: {
- type: 'jsonAjax',
- $owner: me,
- url: serverInvokeUrlTransform(dataSource.url),
- extraParams: dataSource.params,
- reader: {
- type: 'json',
- rootProperty: 'data',
- totalProperty: 'pagination.total',
- successProperty: 'success',
- messageProperty: 'msg'
- }
- },
- listeners: {
- load: function (store, records, successful, operation) {
- me.fireEvent('dataLoadComplete', me, successful, records);
- }
- }
- }
- }
- const buttons = [
- {
- xtype: 'button',
- tooltip: '导出Excel',
- iconCls: 'x-fa fa-file-excel-o'
- },
- {
- xtype: 'button',
- iconCls: 'x-fa fa-columns',
- tooltip: '自适应宽度',
- listeners: {
- click: this.autoSizeColumns
- }
- },
- {
- xtype: 'button',
- tooltip: '清空筛选',
- iconCls: 'x-fa fa-filter',
- handler: this.clearFilter
- }
- ]
- if (newConfig.pagination) {
- newConfig.bbar = new Ext.PagingToolbar({
- pageSize: 50,
- displayInfo: true,
- store: this.store,
- emptyMsg: '没有记录',
- items: [
- {
- xtype: 'combobox',
- tooltip: '分页',
- queryMode: 'local',
- editable: false,
- allowBlank: true,
- labelAlign: 'right',
- width: 90,
- // labelWidth: 30,
- listConfig: {
- minWidth: null
- },
- value: 20,
- valueField: undefined,
- displayField: undefined,
- store: ['20', '50', '100', '200', '300'],
- listeners: {
- change: (sender, nv, ov) => {
- this.store.pageSize = nv;
- this.store.loadPage(1);
- }
- }
- },
- ...buttons
- ]
- })
- } else {
- newConfig.bbar = {
- xtype: 'toolbar', overflowHandler: 'menu',
- items: [
- ...buttons
- ]
- }
- }
- this.superclass.constructor.call(this, newConfig)
- },
- setData(value) {
- if (!this.store) {
- const {config} = this
- this.store = new Ext.data.Store({
- fields: getFileds(config),
- data: value
- })
- } else {
- this.store.getProxy().setData(value)
- this.store.load()
- }
- },
- reload() {
- const me = this
- const {config} = me
- if (config.dataSourceCallbackFn) {
- const scope = lookupScope(this)
- me.setLoading(true)
- config.dataSourceCallbackFn.call(scope, me, {
- successCallback(value) {
- me.setData(value)
- me.setLoading(false)
- me.fireEvent('dataLoadComplete', me, true, value);
- },
- failCallback(error) {
- me.setLoading(false)
- me.fireEvent('dataLoadComplete', me, false, error);
- }
- })
- }
- },
- initComponent() {
- const me = this
- const {config} = me
- const scope = lookupScope(this)
- // 转换 dataSource 属性
- convertDataSource(me, scope, config)
- this.on({
- afterrender(sender) {
- const {config} = this
- if (config.dataSourceCallbackFn && config.autoLoad) {
- me.reload()
- }
- },
- destory() {
- },
- })
- if (this.store?.proxy) {
- // 为 stores.proxy.buildRequest 做准备
- this.store.proxy.$owner = this
- }
- this.superclass.initComponent.call(this)
- },
- autoSizeColumns(sender) {
- const grid = sender.up('grid')
- // const columns = grid.columns;
- // for (let i = 0; i < columns.length; i++) {
- // const column = columns[i];
- // grid.getView().autoSizeColumn(column);
- // column.setWidth(column.getWidth() + 5);
- // }
- for (let i = 1; i < grid.headerCt.getColumnCount(); i++) {
- grid.headerCt.getGridColumns()[i].autoSize(i);
- grid.headerCt.getGridColumns()[i].setWidth(grid.headerCt.getGridColumns()[i].getWidth() + 15);
- }
- },
- clearFilter(sender) {
- sender.up('grid').filters.clearFilters();
- },
- setLoading(value) {
- if (value) {
- this.mask('读取中')
- } else {
- this.unmask()
- }
- },
- // reload() {
- // dataSourceReload(this)
- // },
- })
- }
- /**
- * 获取 columns 中所有的 dataIndex
- */
- function getFileds(newConfig) {
- const fields = []
- _.forEach(newConfig.columns, c => {
- if (c.dataIndex) {
- fields.push(c.dataIndex)
- }
- })
- return fields
- }
- function convertDataSource(sender, scope, newConfig) {
- if (typeof newConfig.store !== 'undefined') {
- // 有 store 属性的情况下,不做任何事
- return
- }
- if (typeof newConfig.dataSource === 'undefined') {
- // 没有定义 dataSource 的情况下,不做任何事
- return
- }
- if (_.isArray(newConfig.data)) {
- // 有 data 属性赋值的情况下
- newConfig.store = {
- fields: getFileds(newConfig),
- data: newConfig.data
- }
- delete newConfig.data
- return
- }
- let {dataSource} = newConfig
- if (typeof dataSource === 'string') {
- // dataSource 是字符串的情况下,找到成员函数
- dataSource = lookupFn(scope, dataSource)
- }
- if (typeof dataSource === 'function') {
- // dataSource 是函数的情况下,在 afterrender 之后进行回调
- newConfig.store = {
- fields: getFileds(newConfig),
- data: []
- }
- newConfig.dataSourceCallbackFn = dataSource
- return
- }
- throw new TypeError('无法识别的调用方法')
- }
|