table宽度自适应

This commit is contained in:
wanghaoo 2018-09-13 10:28:28 +08:00
parent 91363b55b0
commit e5368300cf
15 changed files with 83 additions and 133 deletions

View File

@ -45,7 +45,6 @@ var Column = function (_Component) {
}(_react.Component); }(_react.Component);
Column.defaultProps = { Column.defaultProps = {
width: '200',
ifshow: true ifshow: true
}; };

View File

@ -27,12 +27,13 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
//行控制管理 //行控制管理
var ColumnManager = function () { var ColumnManager = function () {
function ColumnManager(columns, elements) { function ColumnManager(columns, elements, originWidth) {
_classCallCheck(this, ColumnManager); _classCallCheck(this, ColumnManager);
this._cached = {}; this._cached = {};
this.columns = columns || this.normalize(elements); this.columns = columns || this.normalize(elements);
this.originWidth = originWidth;
} }
ColumnManager.prototype.isAnyColumnsFixed = function isAnyColumnsFixed() { ColumnManager.prototype.isAnyColumnsFixed = function isAnyColumnsFixed() {
@ -151,10 +152,13 @@ var ColumnManager = function () {
}; };
columns.forEach(function (column, index) { columns.forEach(function (column, index) {
var defaultOpt = { var defaultOpt = {
ifshow: true, ifshow: true
width: 200 };
//获取非固定列 if (!_this11.originWidth) {
};if (type == 'nofixed' && column.fixed) { defaultOpt.width = 200;
}
//获取非固定列
if (type == 'nofixed' && column.fixed) {
return false; return false;
} }
var newColumn = _extends({}, defaultOpt, column); var newColumn = _extends({}, defaultOpt, column);
@ -210,15 +214,18 @@ var ColumnManager = function () {
}; };
ColumnManager.prototype.getColumnWidth = function getColumnWidth() { ColumnManager.prototype.getColumnWidth = function getColumnWidth() {
var computeWidth = 0;
var columns = this.groupedColumns(); var columns = this.groupedColumns();
columns.forEach(function (col) { var res = { computeWidth: 0, lastShowIndex: 0 };
//如果列显示,固定列也要去掉 columns.forEach(function (col, index) {
if (col.ifshow && !col.fixed) { //如果列显示
computeWidth += parseInt(col.width); if (col.ifshow) {
res.computeWidth += parseInt(col.width);
if (!col.fixed) {
res.lastShowIndex = index;
}
} }
}); });
return computeWidth; return res;
}; };
ColumnManager.prototype.getLeftColumnsWidth = function getLeftColumnsWidth() { ColumnManager.prototype.getLeftColumnsWidth = function getLeftColumnsWidth() {
@ -257,20 +264,24 @@ var ColumnManager = function () {
return this._cached[name]; return this._cached[name];
}; };
//todo 含有children的宽度计算
ColumnManager.prototype._leafColumns = function _leafColumns(columns) { ColumnManager.prototype._leafColumns = function _leafColumns(columns) {
var _this15 = this; var _this15 = this;
var leafColumns = []; var leafColumns = [];
var parWidth = 0;
columns.forEach(function (column) { columns.forEach(function (column) {
if (!column.children) { if (!column.children) {
var defaultOpt = { var defaultOpt = {
ifshow: true, ifshow: true
width: 200
}; };
if (!_this15.originWidth) {
defaultOpt.width = 200;
}
var newColumn = _extends({}, defaultOpt, column); var newColumn = _extends({}, defaultOpt, column);
parWidth += newColumn.width;
leafColumns.push(newColumn); leafColumns.push(newColumn);
} else { } else {
leafColumns.push.apply(leafColumns, _toConsumableArray(_this15._leafColumns(column.children))); leafColumns.push.apply(leafColumns, _toConsumableArray(_this15._leafColumns(column.children)));

View File

@ -157,7 +157,7 @@ var Table = function (_Component) {
var expandedRowKeys = []; var expandedRowKeys = [];
var rows = [].concat(_toConsumableArray(props.data)); var rows = [].concat(_toConsumableArray(props.data));
_this.columnManager = new _ColumnManager2["default"](props.columns, props.children); _this.columnManager = new _ColumnManager2["default"](props.columns, props.children, props.originWidth);
_this.store = (0, _createStore2["default"])({ currentHoverKey: null }); _this.store = (0, _createStore2["default"])({ currentHoverKey: null });
if (props.defaultExpandAllRows) { if (props.defaultExpandAllRows) {
@ -258,10 +258,21 @@ var Table = function (_Component) {
Table.prototype.computeTableWidth = function computeTableWidth() { Table.prototype.computeTableWidth = function computeTableWidth() {
//计算总表格宽度、根据表格宽度和各列的宽度和比较,重置最后一列 //计算总表格宽度、根据表格宽度和各列的宽度和比较,重置最后一列
this.contentWidth = this.contentTable.clientWidth; //表格宽度 this.contentWidth = this.contentTable.clientWidth; //表格宽度
this.computeWidth = this.columnManager.getColumnWidth(); //如果用户传了scroll.x按用户传的为主
var setWidthParam = this.props.scroll.x;
if (setWidthParam) {
if (typeof setWidthParam == 'string' && setWidthParam.indexOf('%')) {
this.contentWidth = this.contentWidth * parseInt(setWidthParam) / 100;
} else {
this.contentWidth = parseInt(setWidthParam);
}
}
var computeObj = this.columnManager.getColumnWidth();
var lastShowIndex = computeObj.lastShowIndex;
this.computeWidth = computeObj.computeWidth;
if (this.computeWidth < this.contentWidth) { if (this.computeWidth < this.contentWidth) {
var contentWidthDiff = this.contentWidth - this.computeWidth; var contentWidthDiff = this.contentWidth - this.computeWidth;
this.setState({ contentWidthDiff: contentWidthDiff }); this.setState({ contentWidthDiff: contentWidthDiff, lastShowIndex: lastShowIndex });
} }
}; };
@ -353,6 +364,7 @@ var Table = function (_Component) {
return showHeader ? _react2["default"].createElement(_TableHeader2["default"], _extends({}, drop, dragBorder, { return showHeader ? _react2["default"].createElement(_TableHeader2["default"], _extends({}, drop, dragBorder, {
minColumnWidth: minColumnWidth, minColumnWidth: minColumnWidth,
contentWidthDiff: contentWidthDiff, contentWidthDiff: contentWidthDiff,
lastShowIndex: this.state.lastShowIndex,
clsPrefix: clsPrefix, clsPrefix: clsPrefix,
rows: rows, rows: rows,
contentTable: this.contentTable, contentTable: this.contentTable,
@ -382,7 +394,7 @@ var Table = function (_Component) {
children: column.title, children: column.title,
drgHover: column.drgHover, drgHover: column.drgHover,
fixed: column.fixed, fixed: column.fixed,
width: column.width ? column.width : 200 width: column.width
}; };
if (column.onHeadCellClick) { if (column.onHeadCellClick) {
cell.onClick = column.onHeadCellClick; cell.onClick = column.onHeadCellClick;
@ -566,9 +578,11 @@ var Table = function (_Component) {
Table.prototype.getColGroup = function getColGroup(columns, fixed) { Table.prototype.getColGroup = function getColGroup(columns, fixed) {
var cols = []; var cols = [];
var _state$contentWidthDi = this.state.contentWidthDiff, var _state = this.state,
contentWidthDiff = _state$contentWidthDi === undefined ? 0 : _state$contentWidthDi; _state$contentWidthDi = _state.contentWidthDiff,
contentWidthDiff = _state$contentWidthDi === undefined ? 0 : _state$contentWidthDi,
_state$lastShowIndex = _state.lastShowIndex,
lastShowIndex = _state$lastShowIndex === undefined ? 0 : _state$lastShowIndex;
if (this.props.expandIconAsCell && fixed !== 'right') { if (this.props.expandIconAsCell && fixed !== 'right') {
cols.push(_react2["default"].createElement('col', { cols.push(_react2["default"].createElement('col', {
@ -588,7 +602,7 @@ var Table = function (_Component) {
} }
cols = cols.concat(leafColumns.map(function (c, i, arr) { cols = cols.concat(leafColumns.map(function (c, i, arr) {
var width = c.width; var width = c.width;
if (arr.length == i + 1) { if (lastShowIndex == i) {
width = parseInt(width) + contentWidthDiff; width = parseInt(width) + contentWidthDiff;
} }
return _react2["default"].createElement('col', { key: c.key, style: { width: width, minWidth: c.width } }); return _react2["default"].createElement('col', { key: c.key, style: { width: width, minWidth: c.width } });

View File

@ -216,7 +216,8 @@ var TableHeader = function (_Component) {
dragborder = _props.dragborder, dragborder = _props.dragborder,
onMouseOut = _props.onMouseOut, onMouseOut = _props.onMouseOut,
contentWidthDiff = _props.contentWidthDiff, contentWidthDiff = _props.contentWidthDiff,
fixed = _props.fixed; fixed = _props.fixed,
lastShowIndex = _props.lastShowIndex;
var attr = dragborder ? { id: 'u-table-drag-thead-' + this.theadKey } : {}; var attr = dragborder ? { id: 'u-table-drag-thead-' + this.theadKey } : {};
return _react2["default"].createElement( return _react2["default"].createElement(
@ -233,7 +234,7 @@ var TableHeader = function (_Component) {
if (!fixed && da.fixed) { if (!fixed && da.fixed) {
fixedStyle = clsPrefix + '-row-fixed-columns-in-body'; fixedStyle = clsPrefix + '-row-fixed-columns-in-body';
} }
if (arr.length == i + 1) { if (lastShowIndex == i) {
da.width = parseInt(da.width) + contentWidthDiff; da.width = parseInt(da.width) + contentWidthDiff;
} }
if (draggable) { if (draggable) {

View File

@ -74,7 +74,7 @@ function sum(Table) {
} }
return item; return item;
}); });
return _react2["default"].createElement(Table, _extends({}, _this.props, { loading: false, footerScroll: true, showHeader: false, columns: columns_sum, data: obj })); return _react2["default"].createElement(Table, _extends({}, _this.props, { loading: false, footerScroll: true, showHeader: false, columns: columns_sum, data: obj, originWidth: true }));
}; };
_this.currentTreeFooter = function () { _this.currentTreeFooter = function () {
@ -126,7 +126,7 @@ function sum(Table) {
var _sumArray = [_extends({ key: "sumData", showSum: "合计" }, _countObj)]; var _sumArray = [_extends({ key: "sumData", showSum: "合计" }, _countObj)];
columns[0] = _extends({}, columns[0], columns2); columns[0] = _extends({}, columns[0], columns2);
return _react2["default"].createElement(Table, _extends({}, _this.props, { bordered: false, loading: false, footerScroll: true, showHeader: false, columns: columns, data: _sumArray })); return _react2["default"].createElement(Table, _extends({}, _this.props, { bordered: false, loading: false, footerScroll: true, showHeader: false, columns: columns, data: _sumArray, originWidth: true }));
}; };
_this.getNodeItem = function (array, newArray) { _this.getNodeItem = function (array, newArray) {

View File

@ -57,7 +57,7 @@ class Demo23 extends Component {
render() { render() {
return <DragColumnTable columns={columns23} data={data23} bordered return <DragColumnTable columns={columns23} data={data23} bordered
draggable={true} dragborder={true}
/>; />;
} }
} }

File diff suppressed because one or more lines are too long

5
dist/demo.css vendored
View File

@ -8424,7 +8424,10 @@ ul {
background: #fff; background: #fff;
position: relative; } position: relative; }
.u-table-fixed-left .u-table-body-inner { .u-table-fixed-left .u-table-body-inner {
margin-right: -20px; } margin-right: -20px;
padding-right: 20px; }
.u-table-fixed-header .u-table-fixed-left .u-table-body-inner {
padding-right: 0px; }
.u-table-fixed-header .u-table-body-inner { .u-table-fixed-header .u-table-body-inner {
height: 100%; height: 100%;
overflow: scroll; } overflow: scroll; }

2
dist/demo.css.map vendored

File diff suppressed because one or more lines are too long

13
dist/demo.js vendored

File diff suppressed because one or more lines are too long

2
dist/demo.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -186,7 +186,11 @@ class Table extends Component{
if(typeof(setWidthParam)=='string' && setWidthParam.indexOf('%')){ if(typeof(setWidthParam)=='string' && setWidthParam.indexOf('%')){
this.contentWidth = this.contentWidth * parseInt(setWidthParam) /100 this.contentWidth = this.contentWidth * parseInt(setWidthParam) /100
}else{ }else{
this.contentWidth = parseInt(setWidthParam); let numSetWidthParam = parseInt(setWidthParam);
//若传入的宽度小于当前宽度以当前宽度为主。
if(numSetWidthParam > this.contentWidth){
this.contentWidth = numSetWidthParam;
}
} }
} }
const computeObj = this.columnManager.getColumnWidth(); const computeObj = this.columnManager.getColumnWidth();

View File

@ -104,6 +104,11 @@ $table-move-in-color: $bg-color-base;
} }
&-fixed-left &-body-inner { &-fixed-left &-body-inner {
margin-right: -20px; margin-right: -20px;
padding-right: 20px;
}
&-fixed-header &-fixed-left &-body-inner {
padding-right: 0px;
} }
&-fixed-header &-body-inner { &-fixed-header &-body-inner {
@ -111,6 +116,7 @@ $table-move-in-color: $bg-color-base;
overflow: scroll; overflow: scroll;
} }
&-fixed-header &-scroll &-header { &-fixed-header &-scroll &-header {
overflow-x: scroll; overflow-x: scroll;
padding-bottom: 20px; padding-bottom: 20px;

View File

@ -153,7 +153,8 @@ class TableHeader extends Component{
let currentDom = document.getElementById("u-table-drag-thead-"+this.theadKey).getElementsByTagName("th")[this.drag.currIndex]; let currentDom = document.getElementById("u-table-drag-thead-"+this.theadKey).getElementsByTagName("th")[this.drag.currIndex];
currentDom.style.width = newWidth+"px"; currentDom.style.width = newWidth+"px";
// this.contentTableWidth = newTableWidth; // this.contentTableWidth = newTableWidth;
contentTable.style.width = newTableWidth+'px'; let contentTableDom = document.getElementById("u-table-drag-thead-"+this.theadKey).parentNode;
contentTableDom.style.width = newTableWidth+'px';
this.drag.x = x; this.drag.x = x;
} }

View File

@ -1,94 +0,0 @@
import React, { Component } from "react";
import {compare} from './util';
import {ObjectAssign} from './util';
/**
* 参数: 列拖拽
* @param {*} Table
*/
// 0、定义一个拖拽dom
// 1、当移动到表头可以显示当前操作列的move图标。
// 2、添加start、move事件、drop事件
export default function dragWidthColumn(Table) {
return class DragWidthColumn extends Component {
constructor(props) {
super(props);
this.state = {
columns:this.setColumOrderByIndex(props.columns)
};
}
componentWillReceiveProps(nextProps){
if(nextProps.columns != this.props.columns){
this.setState({
columns:this.setColumOrderByIndex(nextProps.columns)
})
}
}
setColumOrderByIndex = (_column)=>{
_column.forEach((da,i) => {
da.dragIndex = i;
da.drgHover = false;
});
return _column;
}
onDragStart=(event,data)=>{
if(this.props.onDragStart){
this.props.onDragStart(event,data)
}
}
onDragOver=(event,data)=>{
if(this.props.onDragOver){
this.props.onDragOver(event,data)
}
}
onDragEnter=(event,data)=>{
}
onDrop=(event,data)=>{
}
getTarget=(evt)=>{
return evt.target || evt.srcElement;
}
render() {
const {
data,
dragborder,
draggable,
className,
columns,
onDragStart,
onDragEnter,
onDragOver,
onDrop,
...others
} = this.props;
let key = new Date().getTime();
return (
<Table
{...others}
columns={this.state.columns}
data={data}
className={`${className} u-table-drag-border`}
onDragStart={this.onDragWidthStart}
onDragOver={this.onDragWidthOver}
onDrop={this.onDropWidth}
onDragEnter={this.onDragWidthEnter}
draggable={draggable}
// dragborder={dragborder}
dragborder={false}
dragborderKey={key}
/>)
}
};
}