拖拽bug fix 修复

This commit is contained in:
jonyshi 2018-05-13 14:58:07 +08:00
parent 0e37683d27
commit 0b9d9209dc
10 changed files with 280 additions and 185 deletions

67
demo/demolist/Demo23.js Normal file
View File

@ -0,0 +1,67 @@
/**
*
* @title 动态调整列的宽度
* @description 点击列的表头进行左右拖拽
*/
import React, { Component } from 'react';
import Table from '../../src';
import dragColumn from '../../src/lib/dragColumn';
import Icon from "bee-icon";
const columns23 = [
{
title: "名字",
dataIndex: "a",
key: "a",
width: 100
},
{
title: "性别",
dataIndex: "b",
key: "b",
width: 200
},
{
title: "年龄",
dataIndex: "c",
key: "c",
width: 200,
sumCol: true,
sorter: (a, b) => a.c - b.c
},
{
title: "武功级别",
dataIndex: "d",
key: "d",
width: 200,
}
];
const data23 = [
{ a: "杨过", b: "男", c: 30,d:'内行', key: "2" },
{ a: "令狐冲", b: "男", c: 41,d:'大侠', key: "1" },
{ a: "郭靖", b: "男", c: 25,d:'大侠', key: "3" }
];
const DragColumnTable = dragColumn(Table);
const defaultProps23 = {
prefixCls: "bee-table"
};
class Demo23 extends Component {
constructor(props) {
super(props);
}
render() {
return <DragColumnTable columns={columns23} data={data23} bordered
dragborder={true}
/>;
}
}
Demo23.defaultProps = defaultProps23;
export default Demo23;

File diff suppressed because one or more lines are too long

5
dist/demo.css vendored
View File

@ -7895,7 +7895,8 @@ ul {
.u-table-bordered table { .u-table-bordered table {
border: 1px solid #e9e9e9; border: 1px solid #e9e9e9;
box-sizing: border-box; box-sizing: border-box;
table-layout: fixed; } table-layout: fixed;
width: auto; }
.u-table-bordered th { .u-table-bordered th {
border-bottom: 1px solid #e9e9e9; border-bottom: 1px solid #e9e9e9;
box-sizing: border-box; } box-sizing: border-box; }
@ -8033,7 +8034,7 @@ ul {
.u-table-thead-th .th-drag-gap { .u-table-thead-th .th-drag-gap {
background: transparent; } background: transparent; }
.u-table-thead-th .th-drag-gap-hover { .u-table-thead-th .th-drag-gap-hover {
background: red; } background: #ccc; }
.u-table-thead-th:last-child-drag-gap { .u-table-thead-th:last-child-drag-gap {
border: none; } border: none; }

2
dist/demo.css.map vendored

File diff suppressed because one or more lines are too long

249
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

@ -221,7 +221,7 @@ class Table extends Component{
getHeader(columns, fixed) { getHeader(columns, fixed) {
const { showHeader, expandIconAsCell, clsPrefix ,onDragStart,onDragEnter,onDragOver,onDrop,draggable, const { showHeader, expandIconAsCell, clsPrefix ,onDragStart,onDragEnter,onDragOver,onDrop,draggable,
onMouseDown,onMouseMove,onMouseUp,dragborder} = this.props; onMouseDown,onMouseMove,onMouseUp,dragborder,onThMouseMove} = this.props;
const rows = this.getHeaderRows(columns); const rows = this.getHeaderRows(columns);
if (expandIconAsCell && fixed !== 'right') { if (expandIconAsCell && fixed !== 'right') {
rows[0].unshift({ rows[0].unshift({
@ -234,7 +234,7 @@ class Table extends Component{
const trStyle = fixed ? this.getHeaderRowStyle(columns, rows) : null; const trStyle = fixed ? this.getHeaderRowStyle(columns, rows) : null;
let drop = draggable?{onDragStart,onDragOver,onDrop,onDragEnter,draggable}:{}; let drop = draggable?{onDragStart,onDragOver,onDrop,onDragEnter,draggable}:{};
let dragBorder = dragborder?{onMouseDown,onMouseMove,onMouseUp,dragborder}:{}; let dragBorder = dragborder?{onMouseDown,onMouseMove,onMouseUp,dragborder,onThMouseMove}:{};
return showHeader ? ( return showHeader ? (
<TableHeader <TableHeader
{...drop} {...drop}
@ -462,7 +462,7 @@ class Table extends Component{
renderDragHideTable=()=>{ renderDragHideTable=()=>{
const {columns,} = this.props; const {columns,} = this.props;
let sum = 0; let sum = 0;
return(<div className={`${this.props.clsPrefix}-hiden-drag`} > return(<div id="u-table-drag-hide-table" className={`${this.props.clsPrefix}-hiden-drag`} >
{ {
columns.map((da,i)=>{ columns.map((da,i)=>{
sum += da.width?da.width:0; sum += da.width?da.width:0;
@ -537,7 +537,9 @@ class Table extends Component{
) : null; ) : null;
return ( return (
<table className={` ${tableClassName} table table-bordered `} style={tableStyle}> <table className={` ${tableClassName} table table-bordered `} style={tableStyle}>
{this.getColGroup(columns, fixed)} {/* {
this.props.dragborder?"":this.getColGroup(columns, fixed)
} */}
{hasHead ? this.getHeader(columns, fixed) : null} {hasHead ? this.getHeader(columns, fixed) : null}
{tableBody} {tableBody}
</table> </table>

View File

@ -27,13 +27,11 @@ $table-move-in-color: $bg-color-base;
} }
&-hiden-drag{ &-hiden-drag{
position: relative; position: relative;
&-li{ &-li{
position: absolute; position: absolute;
top: 0px; top: 0px;
left: 0px; left: 0px;
height: 10px; height: 10px;
// border: 1px solid;
} }
} }
table { table {
@ -83,6 +81,7 @@ $table-move-in-color: $bg-color-base;
border: 1px solid #e9e9e9; border: 1px solid #e9e9e9;
box-sizing: border-box; box-sizing: border-box;
table-layout: fixed; table-layout: fixed;
width:auto;
} }
th { th {
border-bottom: 1px solid #e9e9e9; border-bottom: 1px solid #e9e9e9;
@ -293,7 +292,7 @@ $table-move-in-color: $bg-color-base;
background:transparent; background:transparent;
} }
.th-drag-gap-hover{ .th-drag-gap-hover{
background: red; background: #ccc;
} }
} }
&-th:last-child { &-th:last-child {

View File

@ -2,7 +2,6 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import shallowequal from 'shallowequal'; import shallowequal from 'shallowequal';
import {tryParseInt} from './utils'; import {tryParseInt} from './utils';
// import ResizableTh from './ResizableTh';
const propTypes = { const propTypes = {
clsPrefix: PropTypes.string, clsPrefix: PropTypes.string,
@ -15,15 +14,22 @@ class TableHeader extends Component{
constructor(props){ constructor(props){
super(props); super(props);
this.currentObj = null; this.currentObj = null;
this.state = {
border:false
}
//拖拽宽度处理
if(!props.dragborder)return;
this.border = false; this.border = false;
this.drag = { this.drag = {
initPageLeftX:0, initPageLeftX:0,
initLeft:0, initLeft:0,
x:0 x:0,
} width:0
this.state = {
border:false
} }
let _da = {};
Object.assign(_da,this.props.rows[0]);
this.drag.data = JSON.parse(JSON.stringify(this.props.rows[0]));
} }
shouldComponentUpdate(nextProps) { shouldComponentUpdate(nextProps) {
@ -58,35 +64,43 @@ class TableHeader extends Component{
onMouseMove=(event,data)=>{ onMouseMove=(event,data)=>{
if(this.border)return; if(this.border)return;
const {clsPrefix} = this.props; const {clsPrefix} = this.props;
if(this.border){
let x = (event.pageX - this.drag.initPageLeftX) + this.drag.initLeft;
}else{
event.target.className = `${clsPrefix}-thead-th-drag-gap th-drag-gap-hover`; event.target.className = `${clsPrefix}-thead-th-drag-gap th-drag-gap-hover`;
} }
}
onMouseOut=(event,data)=>{ onMouseOut=(event,data)=>{
if(this.border)return; if(this.border)return;
const {clsPrefix} = this.props; const {clsPrefix} = this.props;
event.target.className = `${clsPrefix}-thead-th-drag-gap th-drag-gap`; event.target.className = `${clsPrefix}-thead-th-drag-gap th-drag-gap`;
} }
onMouseDown=(event,data)=>{ onMouseDown=(event,data)=>{
this.border = true; this.border = true;
const {clsPrefix} = this.props; const {clsPrefix} = this.props;
event.target.className = `${clsPrefix}-thead-th-drag-gap th-drag-gap-hover`;
this.drag.initPageLeftX = event.pageX; this.drag.initPageLeftX = event.pageX;
this.drag.initLeft = tryParseInt(event.target.style.left); this.drag.initLeft = tryParseInt(event.target.style.left);
this.drag.x = this.drag.initLeft; this.drag.x = this.drag.initLeft;
this.drag.currIndex = this.props.rows[0].findIndex(da=>da.key==data.key);
this.drag.width = this.drag.data[this.drag.currIndex].width;
} }
onMouseUp=(event,data)=>{ onMouseUp=(event,data)=>{
this.border = false; this.border = false;
const {clsPrefix} = this.props; const {clsPrefix} = this.props;
event.target.className = `${clsPrefix}-thead-th-drag-gap th-drag-gap`; event.target.className = `${clsPrefix}-thead-th-drag-gap th-drag-gap`;
let endx = (event.pageX-this.dragBorderObj.initPageLeftX); }
// event.target.offsetWidth onThMouseUp=(event,data)=>{
this.border = false;
}
onThMouseMove=(event,data)=>{
if(!this.border)return;
let x = (event.pageX - this.drag.initPageLeftX) + this.drag.initLeft-0;
//设置hiden的left
let currentHideDom = document.getElementById("u-table-drag-hide-table").getElementsByTagName("div")[this.drag.currIndex];
currentHideDom.style.left = (this.drag.initPageLeftX+x-16)+"px";
//设置当前的宽度
let currentData = this.drag.data[this.drag.currIndex];
currentData.width = this.drag.width + x;
let currentDom = document.getElementById("u-table-drag-thead").getElementsByTagName("th")[this.drag.currIndex];
currentDom.style.width = (currentData.width)+"px";
this.drag.x = x;
} }
render() { render() {
@ -94,7 +108,7 @@ class TableHeader extends Component{
onMouseDown,onMouseMove,onMouseUp,dragborder,onMouseOut onMouseDown,onMouseMove,onMouseUp,dragborder,onMouseOut
} = this.props; } = this.props;
return ( return (
<thead className={`${clsPrefix}-thead`}> <thead className={`${clsPrefix}-thead`} id="u-table-drag-thead">
{ {
rows.map((row, index) => ( rows.map((row, index) => (
<tr key={index} style={rowStyle}> <tr key={index} style={rowStyle}>
@ -112,15 +126,9 @@ class TableHeader extends Component{
key={da.key} />) key={da.key} />)
}else if(dragborder){ }else if(dragborder){
return(<th return(<th
// onDragStart={(event)=>{this.onDragGapStart(event,da)}} style={{width:da.width}}
// onDragOver={(event)=>{this.onDragGapOver(event,da)}} onMouseMove={(event)=>{this.onThMouseMove(event,da)}}
// onDrop={(event)=>{this.onDropGap(event,da)}} onMouseUp={(event)=>{this.onThMouseUp(event,da)}}
// onDragEnter={(event)=>{this.onDragGapEnter(event,da)}}
// onMouseDown={(event)=>{onMouseDown(event,da)}}
// onMouseUp={(event)=>{onMouseUp(event,da)}}
// {...da}
className={`${da.className} ${clsPrefix}-thead-th `} className={`${da.className} ${clsPrefix}-thead-th `}
key={i} > key={i} >
{da.children} {da.children}

View File

@ -4,8 +4,6 @@ import Checkbox from "bee-checkbox";
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import Popover from 'bee-popover'; import Popover from 'bee-popover';
import {sortBy} from './util'; import {sortBy} from './util';
// import createColResizable from '../resiztable'
// import Table from './Table';
/** /**
* 参数: 列拖拽 * 参数: 列拖拽
* @param {*} Table * @param {*} Table
@ -18,8 +16,6 @@ export default function dragColumn(Table) {
constructor(props) { constructor(props) {
super(props); super(props);
const {columns} = props; const {columns} = props;
this.dragBorderObj = {startScreenX:0,endScreenX:0};
this.mouse = false;
this.setColumOrderByIndex(columns); this.setColumOrderByIndex(columns);
} }
@ -29,17 +25,6 @@ export default function dragColumn(Table) {
} }
} }
// componentDidMount() {
// const domElemTableList = document.querySelectorAll('table');
// createColResizable(domElemTableList[0], {
// liveDrag: true
// });
// createColResizable(domElemTableList[1], {
// liveDrag: false,
// headerOnly: false
// });
// }
setColumOrderByIndex = (columns)=>{ setColumOrderByIndex = (columns)=>{
let _column = []; let _column = [];
Object.assign(_column,columns); Object.assign(_column,columns);
@ -93,51 +78,19 @@ export default function dragColumn(Table) {
}); });
} }
onMouseDown=(event,data)=>{
this.mouse = true;
this.dragBorderObj.startScreenX = event.screenX;
}
onMouseMove=(event,data)=>{
if(!this.mouse)return;
let endx = (event.screenX-this.dragBorderObj.startScreenX);
let {columns:_columns} = this.state;
let columns = [];
Object.assign(columns,_columns);
// let currentIndex = columns.findIndex((_da,i)=>_da.key == data.key);
// currentIndex = currentIndex==0?currentIndex:(currentIndex-1);
let currObj = columns.find((_da,i)=>_da.key == data.key);
if(!currObj)return;
currObj.width = currObj.width?(currObj.width+endx):endx;
this.setState({
columns
});
}
getTarget=(evt)=>{ getTarget=(evt)=>{
return evt.target || evt.srcElement; return evt.target || evt.srcElement;
} }
onMouseUp=(event,data)=>{
let endx = (event.screenX-this.dragBorderObj.startScreenX);
this.mouse = false;
}
render() { render() {
const {data,dragborder,draggable,className} = this.props; const {data,dragborder,draggable,className} = this.props;
const {columns} = this.state; const {columns} = this.state;
return (<Table {...this.props} columns={columns} data={data} className={`${className} u-table-drag-border`} return (<Table {...this.props} columns={columns} data={data} className={`${className} u-table-drag-border`}
onDragStart={this.onDragStart} onDragOver={this.onDragOver} onDrop={this.onDrop} onDragStart={this.onDragStart} onDragOver={this.onDragOver} onDrop={this.onDrop}
onDragEnter={this.onDragEnter} onDragEnter={this.onDragEnter}
draggable={draggable} draggable={draggable}
dragborder={true} dragborder={true}
onMouseDown={this.onMouseDown}
onMouseMove={this.onMouseMove}
onMouseUp={this.onMouseUp}
/>) />)
} }
}; };