<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>x-data-spreadsheet電子表格</title>
<link rel="stylesheet" href="https://unpkg.com/x-data-spreadsheet@1.1.9/dist/xspreadsheet.css">
<script src="https://unpkg.com/x-data-spreadsheet@1.1.9/dist/xspreadsheet.js"></script>
<script src="https://unpkg.com/x-data-spreadsheet@1.1.9/dist/locale/zh-cn.js"></script>
<style type="text/css">
.uni-flex {
display: flex;
flex-direction: row;
}
.uni-flex-item {
flex: 1;
}
.menu-item {
width: 200px;
}
.border-gray {
border: solid 1px #DEDEDE;
}
</style>
</head>
<body>
<div class="uni-flex">
<div class="menu-item">
<ul>
<li draggable="true" id='name'><span>【姓名】</span></li>
</ul>
</div>
<div class="uni-flex-item border-gray" id="x-spreadsheet-demo"></div>
</div>
<script>
document.addEventListener('DOMContentLoaded', (evtAll) => {
x_spreadsheet.locale('zh-cn');
const xs = new x_spreadsheet("#x-spreadsheet-demo", {
mode: 'edit', // edit | read
showToolbar: true,
showGrid: true,
showContextmenu: true,
view: {
height: () => document.documentElement.clientHeight,
width: () => document.documentElement.clientWidth - 200,
},
row: {
len: 100,
height: 25,
},
col: {
len: 26,
width: 100,
indexWidth: 60,
minWidth: 60,
},
style: {
bgcolor: '#ffffff',
align: 'left',
valign: 'middle',
textwrap: false,
strike: false,
underline: false,
color: '#0a0a0a',
font: {
name: 'Helvetica',
size: 10,
bold: false,
italic: false,
},
},
})
.loadData({}) // load data
.change(data => {
// save data to db
});
// data validation
xs.validate();
// 測試xs的方法
setTimeout(() => {
console.log('xs.data:', xs.sheet.data.getCellRectByXY(550, 310));
}, 2000);
function onSelected(event) {
// event.target.style = 'background-color: blue';
console.log('xxx')
}
document.getElementById('name').addEventListener('click', onSelected, false);
/**
* 拖拽開始
* @param {DragEvent} event事件對象
*
*/
function onDragStart(event) {
if(!event.target || !event.dataTransfer) {
return;
}
let text = event.target.innerText;
let children = event.target.children[0].innerText;
console.log('from text:', text, 'children:', children);
// 傳送的數(shù)據(jù)
event.dataTransfer.setData('key', 'name');
// 傳送的數(shù)據(jù)
event.dataTransfer.setData('value', text);
}
document.getElementById('name').addEventListener('dragstart', onDragStart, false);
// 拖動事件
function onDragOver(event) {
event.stopPropagation();
event.preventDefault();
}
// 松開拖放
function onDrop(event) {
event.stopPropagation();
event.preventDefault();
const target = event.currentTarget;
if(!target || !event.dataTransfer) {
return;
}
let key = event.dataTransfer.getData('key');
let value = event.dataTransfer.getData('value');
// 目標(biāo)位置
var targetPosition = getTargetPosition(event);
/**
* 通過鼠標(biāo)目標(biāo)位置的x、y坐標(biāo),獲取所在的行下表、列下標(biāo)
* ri 行下標(biāo)
* ci 列下標(biāo)
*/
const { ri, ci } = xs.sheet.data.getCellRectByXY(targetPosition.x, targetPosition.y);
xs.cellText(ri, ci, `${key}:${value}`).reRender();
}
// 監(jiān)聽事件
document.getElementById('x-spreadsheet-demo').addEventListener('dragover', onDragOver, false);
document.getElementById('x-spreadsheet-demo').addEventListener('drop', onDrop, false);
// 獲取拖拽松開鼠標(biāo)時,目標(biāo)位置的x、y坐標(biāo)
function getTargetPosition(event) {
const canvas = document.querySelector('.x-spreadsheet-table');
const x = event.pageX - canvas.getBoundingClientRect().left;
const y = event.pageY - canvas.getBoundingClientRect().top;
return { x: x, y: y };
}
});
</script>
</body>
</html>