index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <script lang="ts" setup>
  2. import { ref, onMounted, watch, reactive } from 'vue';
  3. import { ElMessage } from 'element-plus';
  4. interface Props {
  5. data: number[][];
  6. disabled: boolean;
  7. }
  8. const props = withDefaults(defineProps<Props>(), {
  9. disabled: false,
  10. });
  11. const bidData = ref(props.data);
  12. const mouseDown = ref(false);
  13. const startRowIndex = ref(0);
  14. const startColIndex = ref(0);
  15. const items = ref([]);
  16. const WeekMap = {
  17. 0: '星期一',
  18. 1: '星期二',
  19. 2: '星期三',
  20. 3: '星期四',
  21. 4: '星期五',
  22. 5: '星期六',
  23. 6: '星期日',
  24. };
  25. const dialogVisible = ref(false);
  26. const bid = ref(1.00);
  27. const selectedItems = ref({});
  28. const form = ref({
  29. bid: 1.00, // 将 bid 添加到 form 对象中
  30. });
  31. const rules = ref({
  32. bid: [
  33. { required: true, message: '请输入数值小于100的数值,可精确到小数点后2位', trigger: 'blur' },
  34. {
  35. validator: (rule, value, callback) => {
  36. if (Number(value) > 100) {
  37. return callback(new Error('请输入数值小于100的数值,可精确到小数点后2位'));
  38. }
  39. return callback();
  40. },
  41. trigger: 'blur'
  42. }
  43. ]
  44. });
  45. const formRef = ref(null);
  46. //根据 props.disabled 和 info.selected 来返回一个包含样式的对象,用于设置单元格的样式
  47. function getTdStyle(info: any) {
  48. if (props.disabled) {
  49. return {cursor: 'not-allowed', background: '#fff'};
  50. }
  51. return {background: info.selected ? '#ccdbff' : ''};
  52. }
  53. for (let i = 0; i < 7; i++) {
  54. selectedItems.value[i] = [];
  55. const tmp = [];
  56. for (let j = 0; j < 24; j++) {
  57. tmp.push({value: bidData.value.length === 0 ? 0 : bidData.value[i][j], selected: false});
  58. }
  59. items.value.push(tmp);
  60. }
  61. //在单元格上按下鼠标时触发的函数
  62. function handleMouseDown(rowIndex: number, colIndex: number, event: any) {
  63. if (props.disabled) return;
  64. if (event.button === 0) {
  65. clearCellBackgroundColor();
  66. mouseDown.value = true;
  67. startRowIndex.value = rowIndex;
  68. startColIndex.value = colIndex;
  69. items.value[rowIndex][colIndex].selected = true;
  70. selectedItems.value[rowIndex].push(colIndex);
  71. }
  72. }
  73. function handleMouseUp(event: any) {
  74. if (props.disabled) return;
  75. if (event.button === 0) {
  76. mouseDown.value = false;
  77. startColIndex.value = 0;
  78. startRowIndex.value = 0;
  79. dialogVisible.value = true;
  80. }
  81. }
  82. //在单元格上移动鼠标时触发的函数,用于选择多个单元格。
  83. function handleMouseMove(rowIndex: number, colIndex: number) {
  84. if (props.disabled) return;
  85. //if (info.selected) {
  86. // info.selected = false;
  87. //}
  88. if (mouseDown.value) {
  89. selectCell(rowIndex, colIndex);
  90. }
  91. }
  92. //选择多个单元格
  93. function selectCell(rowIndex: number, colIndex: number) {
  94. if (props.disabled) return;
  95. const rowStart = Math.min(startRowIndex.value, rowIndex);
  96. const rowEnd = Math.max(startRowIndex.value, rowIndex);
  97. const cellStart = Math.min(startColIndex.value, colIndex);
  98. const cellEnd = Math.max(startColIndex.value, colIndex);
  99. for (let i = rowStart; i <= rowEnd; i++) {
  100. for (let j = cellStart; j <= cellEnd; j++) {
  101. items.value[i][j].selected = true;
  102. selectedItems.value[i].push(j);
  103. }
  104. }
  105. }
  106. //提交出价,将选中的单元格的值设置为 bid,并清除选中状态和单元格背景色。
  107. function submitBid() {
  108. for (const row of Object.keys(selectedItems.value)) {
  109. for (const col of selectedItems.value[row]) {
  110. items.value[row][col].value = Number(form.value.bid);
  111. bidData.value[row][col] = Number(form.value.bid);
  112. }
  113. }
  114. clearSelectedItems();
  115. clearCellBackgroundColor();
  116. dialogVisible.value = false;
  117. }
  118. const submitForm = async (formEl: FormInstance | undefined) => {
  119. if (!formEl) return;
  120. await formEl.validate((valid, fields) => {
  121. if (valid) {
  122. submitBid();
  123. }
  124. });
  125. };
  126. const resetForm = (formEl: FormInstance | undefined) => {
  127. if (!formEl) return
  128. cancelBid()
  129. formEl.resetFields()
  130. }
  131. //取消出价
  132. function cancelBid() {
  133. clearSelectedItems();
  134. clearCellBackgroundColor();
  135. dialogVisible.value = false;
  136. }
  137. //清除所有单元格的背景色。
  138. function clearCellBackgroundColor() {
  139. for (const hoursList of items.value) {
  140. for (const info of hoursList) {
  141. info.selected = false;
  142. }
  143. }
  144. }
  145. function clearSelectedItems() {
  146. for (var i = 0; i < 7; i++) {
  147. selectedItems.value[i] = [];
  148. }
  149. }
  150. //重置所有单元格的出价为 1,并清除选中状态。
  151. function resetAllBid() {
  152. for (let i = 0; i < 7; i++) {
  153. for (let j = 0; j < 24; j++) {
  154. items.value[i][j].value = 1;
  155. items.value[i][j].selected = false;
  156. bidData.value[i][j] = 1; // TODO: 不知道是什么东西
  157. }
  158. }
  159. }
  160. function closeDialog(done: Function) {
  161. cancelBid();
  162. done();
  163. }
  164. const applyBid = (timeRangeValue: string, scheduleValue: string, bidValue: number) => {
  165. const timeRangeMap = {
  166. 'Option1': {start: 0, end: 23},
  167. 'Option2': {start: 0, end: 6},
  168. 'Option3': {start: 7, end: 11},
  169. 'Option4': {start: 9, end: 16},
  170. 'Option5': {start: 12, end: 16},
  171. 'Option6': {start: 17, end: 20},
  172. 'Option7': {start: 21, end: 23},
  173. };
  174. const scheduleMap = {
  175. 'Option1': [0, 1, 2, 3, 4, 5, 6],
  176. 'Option2': [0, 1, 2, 3, 4],
  177. 'Option3': [5, 6],
  178. };
  179. const {start, end} = timeRangeMap[timeRangeValue];
  180. const days = scheduleMap[scheduleValue];
  181. days.forEach(day => {
  182. for (let hour = start; hour <= end; hour++) {
  183. items.value[day][hour].selected = true;
  184. items.value[day][hour].value = bidValue;
  185. bidData.value[day][hour] = bidValue;
  186. }
  187. });
  188. };
  189. defineExpose({
  190. applyBid,
  191. });
  192. watch(
  193. () => props.data,
  194. () => {
  195. if (props.data.length === 0) {
  196. for (let i = 0; i < 7; i++) {
  197. const tmp = [];
  198. for (let j = 0; j < 24; j++) {
  199. tmp.push(1);
  200. }
  201. props.data.push(tmp);
  202. }
  203. }
  204. for (let i = 0; i < 7; i++) {
  205. for (let j = 0; j < 24; j++) {
  206. items.value[i][j].value = props.data[i][j];
  207. }
  208. }
  209. bidData.value = props.data;
  210. },
  211. {immediate: true}
  212. );
  213. </script>
  214. <template>
  215. <div class="calendar">
  216. <table class="calendar-table calendar-table-hour">
  217. <thead class="calendar-head">
  218. <tr>
  219. <th class="week-td" rowspan="8">星期 / 时间</th>
  220. <th colspan="12">00:00 - 12:00</th>
  221. <th colspan="12">12:00 - 24:00</th>
  222. <th class="week-td" colspan="4" rowspan="2" style="display: none">小时</th>
  223. </tr>
  224. <tr>
  225. <th v-for="(_, i) in 24" :key="i" colspan="1">{{ i }}</th>
  226. </tr>
  227. </thead>
  228. <tbody>
  229. <template v-for="(hoursList, week) in items">
  230. <tr>
  231. <th>{{ WeekMap[week] }}</th>
  232. <td
  233. v-for="(info, hour) in hoursList"
  234. :key="hour"
  235. :style="getTdStyle(info)"
  236. @mousedown="handleMouseDown(week, hour, $event)"
  237. @mouseover="handleMouseMove(week, hour)"
  238. @mouseup="handleMouseUp">
  239. <span class="cell-text"> {{
  240. Number(info.value).toFixed(2)
  241. }} </span>
  242. </td>
  243. </tr>
  244. </template>
  245. <tr>
  246. <th class="clear-bar" colspan="28">
  247. <span class="middle">可拖动鼠标选择时间段</span>
  248. <el-button :disabled="disabled" class="hover-link fr" link @click="resetAllBid">全部重置</el-button>
  249. </th>
  250. </tr>
  251. </tbody>
  252. </table>
  253. </div>
  254. <el-dialog v-model="dialogVisible" :before-close="closeDialog" :close-on-click-modal="false" title="修改出价系数"
  255. width="30%">
  256. <el-form ref="formRef" :model="form" :rules="rules" style="display: inline-block;">
  257. <el-form-item label="Bid" prop="bid" label-width="80px">
  258. <el-input
  259. v-model="form.bid"
  260. clearable
  261. oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')"
  262. placeholder="1.0"
  263. style="width: 150px;"
  264. ></el-input>
  265. </el-form-item>
  266. </el-form>
  267. <template #footer>
  268. <span class="dialog-footer">
  269. <el-button @click="resetForm(formRef)">取消</el-button>
  270. <el-button type="primary" @click="submitForm(formRef)">确认</el-button>
  271. </span>
  272. </template>
  273. </el-dialog>
  274. </template>
  275. <style lang="scss" scoped>
  276. .calendar {
  277. background-color: #fff;
  278. -webkit-user-select: none;
  279. position: relative;
  280. display: inline-block;
  281. .calendar-table {
  282. border-collapse: collapse;
  283. }
  284. .week-td {
  285. width: 90px;
  286. }
  287. }
  288. table {
  289. display: table;
  290. border-collapse: separate;
  291. box-sizing: border-box;
  292. text-indent: initial;
  293. border-spacing: 2px;
  294. border-color: gray;
  295. thead {
  296. display: table-header-group;
  297. vertical-align: middle;
  298. border-color: inherit;
  299. }
  300. tr {
  301. border: 1px solid #e0e5f4;
  302. font-size: 12px;
  303. text-align: center;
  304. line-height: 32px;
  305. color: rgba(0, 0, 0, 0.5);
  306. th {
  307. min-width: 40px;
  308. border: 1px solid #e0e5f4;
  309. font-size: 12px;
  310. text-align: center;
  311. line-height: 32px;
  312. background: #f7f8fa;
  313. }
  314. td {
  315. border: 1px solid #e0e5f4;
  316. font-size: 12px;
  317. text-align: center;
  318. line-height: 32px;
  319. min-width: 40px;
  320. &:hover {
  321. background: #ccdbff;
  322. cursor: pointer;
  323. }
  324. .cell-text {
  325. color: black;
  326. }
  327. }
  328. }
  329. }
  330. .clear-bar {
  331. line-height: 32px;
  332. padding: 0 12px;
  333. .hover-link {
  334. color: #1c6bde;
  335. font-size: 13px;
  336. margin-top: 7px;
  337. }
  338. .fr {
  339. float: right;
  340. }
  341. .middle {
  342. float: center;
  343. }
  344. }
  345. </style>