tableCom1.js
29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
setTimeout(function () {
require(['layui/layui'], function (b) {
// 定义一个模块,在这个模块中使用layui的一些功能
// define(['layui'], function(layui) {
// console.log(123455555);
// // 使用layui的某个组件,例如layer
// layui.use('layer', function(){
// var layer = layui.layer;
// layer.msg('Hello World!');
// });
// // 其他的模块逻辑...
// });
setTimeout(function () {
// debugger
test();
}, 1);
var layTable, soulTable, _thisTable;
var qw = window
var tabledData = {};
// console.log('initOK1')
window._renderTable = function () {
// console.log('render talbe')
if (_thisTable){
_thisTable.reload();
}else{ test()};
}
function test() {
// console.log('do test')
// 自定义模块
layui.config({
dir: '/assets/libs/layui/',
base: '/assets/libs/ext/', // 模块目录
version: 'v1.6.4'
}).extend({ // 模块别名
soulTable: 'soulTable'
});
layui.use(['form', 'table', 'soulTable'], function () {
var table = layui.table;
var form = layui.form;
console.log('form', form);
layTable = layui.table;
soulTable = layui.soulTable;
var _thisTable = table.init('parse-table-demo', { //转化静态表格
height: 'full-500',
toolbar: '#toolbarDemo', //操作1:启用自定义模板表格头部工具栏
defaultToolbar: [],//操作2:隐藏头部工具栏右侧的图标
height: 500,
width:"100%",
rowDrag: {
done: function (obj) {
console.log(obj.row) // 当前行数据
console.log(obj.cache) // 改动后全表数据
console.log(obj.oldIndex) // 原来的数据索引
console.log(obj.newIndex) // 改动后数据索引
}
}
, drag: {//同步移动回调 obj 当前 头部顺序 json
done: function (obj) {
console.log('drag', obj.row) // 当前行数据
console.log('drag', obj.cache) // 改动后全表数据
console.log('drag', obj.oldIndex) // 原来的数据索引
console.log(obj.newIndex) // 改动后数据索引
}
}
, totalRow: false
, done: function (res) {
console.log('渲染了', res)// 当前行数据
tabledData = res.data;
soulTable.render(this)
}
});
form.on('switch(rowDragSwitch)', function (data) {
// console.log('kai渲染了', data)
soulTable.suspend('myTable', 'rowDrag', true)
soulTable.suspend('myTable', 'drag', true)
});
table.on('tool(parse-table-demo)', function (obj) {
var data = obj.data;
if (obj.event === 'detail') {
layer.msg('ID:' + data.id + ' 的查看操作');
} else if (obj.event === 'del') {
// console.log(obj, 'data', data)
layer.confirm(data.username ? '真的删除行么' : '删除当前模板数据?', function (index) {
obj.del();
console.log(data.username,'delete data.usernameid',data.usernameid)
$('#list-box2 tr').eq(obj.index).remove();
console.log('22www',$('#list-box2').html())
$('#list-box').html($('#list-box2').html());
if(!data.username){
$('[data-pid="'+data.usernameid+'"]').remove()
$('[data-ppid="'+data.usernameid+'"]').remove()
$('#list-box').html($('#list-box2').html());
}
window._renderTable()
layer.close(index);
});
} else if (obj.event === 'edit') {
layer.alert('编辑行:<br>' + JSON.stringify(data))
}
});
table.on('row(parse-table-demo)', function(obj) {
// var tr = obj.tr; // 获取当前行的DOM对象
// var data = obj.data; // 获取当前行的数据
// console.log('-----111 ----')
});
table.on('edit(parse-table-demo)', function (obj) {
// console.log('obj',obj)
var value = obj.value //得到修改后的值
, data = obj.data //得到所在行所有键值
, field = obj.field; //得到字段
// console.log('修改的数量', data)
// console.log('tr',obj.tr)
const tds7 = $(obj.tr).find('td[data-field="username7"]');
const tds9div = $(obj.tr).find('td[data-field="username9"] .layui-table-cell');
// alert(tds9div.text());
// 遍历这些 td 元素,获取 div.layui-table-cell 的值
var cellValue7 = 0
tds7.each(function() {
cellValue7 = $(this).find('div.layui-table-cell').text();
});
const tds6 = $(obj.tr).find('td[data-field="username6"]');
// 遍历这些 td 元素,获取 div.layui-table-cell 的值
var cellValue6 = 0
tds6.each(function() {
cellValue6 = $(this).find('div.layui-table-cell').text();
});
const tds8 = $(obj.tr).find('td[data-field="username8"]');
cnum = cellValue7*cellValue6;
// 遍历这些 td 元素,获取 div.layui-table-cell 的值
tds8.each(function() {
$(this).find('div.layui-table-cell').html(cnum);
});
const usernameid = $(obj.tr).find('td[data-field="usernameid"] .layui-table-cell').text();
//获取名称
const name = $(obj.tr).find('td[data-field="username2"] .layui-table-cell').text();
const tds9divtext = $(obj.tr).find('td[data-field="username9"] .layui-table-cell').text();
// console.log(name,'当前的usernameid',usernameid)
//更改临时表
$('#list-box2 tr[data-ppid="'+usernameid+'"],#list-box2 tr[data-pid="'+usernameid+'"]').each(function() {
const tr = $(this);
// 在当前 tr 中查找第三个 td 元素的文本值
const thirdTdText = tr.find('td:nth-child(3)').text().trim();
// alert(tds9divtext)
// 检查第三个 td 的文本值是否为 "IC超薄读卡器"
if (thirdTdText === name) {
tr.find('td.dwxh').html(tds9divtext);
// 在当前 tr 中查找具有 class="nums" 的 td 元素
const numsTd = tr.find('td.nums');
const market_sell_price = tr.find('td.market_sell_price');
const subtotal = tr.find('td.subtotal');
// 如果找到了 numsTd,则进行相应的操作
if (numsTd.length > 0) {
// 在这里处理找到的 td 元素
// console.log('Found nums td:', numsTd.text());
// 可以在这里进行其他操作
numsTd.html(cellValue7)
market_sell_price.html(cellValue6)
subtotal.html(cnum)
// ccnum = cellValue7*cellValue6;
}
}
// console.log('thirdTdTextthirdTdText',thirdTdText)
if (thirdTdText === '小计') {
// 在当前 tr 中查找具有 class="nums" 的 td 元素
const tj = tr.find('td.tj');
// console.log('tj--s',tr.html())
// tj.(99)
// 如果找到了 numsTd,则进行相应的操作
if (tj.length > 0) {
// alert(cnum)
var ccnum = 0;
$('#list-box2 tr[data-ppid="'+usernameid+'"]').each(function() {
const n = $(this).find('td.subtotal').text();
ccnum+=parseFloat(n);
})
tj.html(ccnum)
}
}
});
const usernameIdMap = {};
// 遍历所有 tr 元素,填充 usernameIdMap
$('tr').each(function() {
const usernameIdCell = $(this).find('td[data-field="usernameid"] .layui-table-cell');
const usernameId = usernameIdCell.text().trim();
if (usernameId == usernameid) {
if (!usernameIdMap[usernameId]) {
usernameIdMap[usernameId] = [];
}
usernameIdMap[usernameId].push($(this));
}
});
// console.log('usernameIdMap',usernameIdMap);
var countNum = 0;
$.each(usernameIdMap, function(usernameId, rows) {
// console.log('Checking rows for usernameid:', usernameId);
rows.forEach(function(row) {
const username8Cell = row.find('td[data-field="username8"] .layui-table-cell');
// const username5Cell = row.find('td[data-field="username5"] .layui-table-cell');
if (username8Cell.text().trim() !== '') {
// console.log('Valid row found for usernameid', usernameId, ':', row);
// 在此处执行需要的操作,例如高亮显示行
// row.css('background-color', 'yellow');
countNum += parseFloat(username8Cell.text())
}
});
});
// console.log('当前更改的usernameId',usernameid)
// return;
const specifiedTr = $(obj.tr); // 替换为实际的指定 tr 元素选择器
// 遍历指定 tr 元素之后的兄弟 tr 元素
specifiedTr.nextAll('tr').each(function() {
const tr = $(this);
// 找到 td[data-field="usernameid"] 里面 div 的值为 1 的 tr
const divValue = tr.find('td[data-field="usernameid"] div').text().trim();
const divValue2 = tr.find('td[data-field="username2"] div').text().trim();
if (divValue === usernameid && divValue2=='小计') {
// 设置 td[data-field="username2"] 里面的 .layui-table-cell 的字符串为指定的值
tr.find('td[data-field="username5"] .layui-table-cell').text(countNum);
}
});
// console.log('tabledData----',tabledData);
var count = 0;
$('#list-box2 tr').each(function() {
var subtotalValue = $(this).find('.subtotal').text().trim();
// console.log('subtotalValue',subtotalValue)
if (subtotalValue) {
count += parseFloat(subtotalValue);
}
});
var lastTr = $('#table').next().find('.layui-table-body tbody tr:last');
if(count){
if (lastTr.attr('id') === 'tj-dom') {
// 存在则更改值
lastTr.find('#count-dom div').each(function() {
$(this).text(count)
});
} else {
// 不存在则添加这个 tr
var totalRow = '<tr id="tj-dom"><td colspan="8" style="text-align: right;">总统计</td><td id="count-dom" colspan="2"><div class="layui-table-cell laytable-cell-3-0-2">' + count + '</div></td></tr>';
$('#table').next().find('.layui-table-body tbody').append(totalRow);
}
}
// var totalRow = '<tr id="tj-dom"><td colspan="8" style="text-align: right;">统计:</td><td colspan="2">' + count + '</td></tr>';
// if(count){
// $('#table').next().find('.layui-table-body tbody').append(totalRow);
// }
// 获取所有<tr>元素
var rows = document.querySelectorAll('#list-box2 tr');
var dataArray = [];
// 遍历所有行
rows.forEach(function(row) {
var columns = row.querySelectorAll('td');
var rowData = [];
columns.forEach(function(column) {
rowData.push(column.textContent.trim());
});
dataArray.push(rowData);
});
let arra =[]
dataArray.forEach(item=>{
if(item.length > 5){
arra.push({
'mb_id':item[1],
'name': item[2],
'type': item[3],
'dwxh': item[4],
'brand': item[5],
'unit': item[7],
'nums': item[8],
'price': item[9],
'total': item[10],
'comment': item[11],
})
}
})
console.log('dataArray',dataArray);
console.log('arra',arra);
Fast.api.ajax({
url: 'fastflow/bill/Czgprz/setGoods',
data: {
arra
}
}, function (data, ret) {
//成功的回调
// console.log('list', data);
}, function (data, ret) {
//失败的回调
});
window._renderTable();
return;
if (field == 'username7' || field == 'username6') {
for (var i = 0; i < tabledData.length; i++) {
if (tabledData[i].username2 == data.username2) {
// console.log('000001', [tabledData[i].username6,tabledData[i].username7])
tabledData[i].username8 = tabledData[i].username6 * tabledData[i].username7
break;
}
}
// console.log('tabledData', tabledData)
qw.newcent = tabledData
let arra =[]
tabledData.forEach(item=>{
console.table('1231231232',item)
if(item.username5!==undefined && item.username4!==undefined && item.username7!==undefined && item.username6!==undefined){
arra.push({
'mb_id':item.usernameid,
'name': item.username2,
'type': item.username5,
'dwxh': item.username9,
'brand': item.username1,
'unit': item.username4,
'nums':item.username7,
'price': item.username6,
'total': item.username8,
'comment': item.username3,
})
}
})
Fast.api.ajax({
url: 'fastflow/bill/Czgprz/setGoods',
data: {
arra
}
}, function (data, ret) {
//成功的回调
// console.log('list', data);
}, function (data, ret) {
//失败的回调
});
window._renderTable();
// table.reload('table', {
// data: tabledData
// })
}
});
$("input[name='row[status][]']").click(function () {
// console.log('执行此时');
// 检查当前点击的 radio 是否被选中
var selectedValue = $(this).val();
var isSelected = $(this).prop('checked');
// var txt = $(this).siblings('span').text();
if (!isSelected) {
// $('#list-box2')
// alert(111);
// console.log("$('#list-box2')",$('#list-box2').html())
$('#list-box').html($('#list-box2').html());
window._renderTable()
// return;
// var htmlTable = convertToHtmlTable(table.cache["table"]);
// console.log(htmlTable);
}
})
})
setTimeout(function() {
// console.log('-------------执行刷新---------------')
$('td[data-field="username5"]').each(function() {
// console.log('12132312312')
// 获取当前 <td> 元素的内部 <div> 元素
var divElement = $(this).find('div.layui-table-cell');
// 获取 <div> 元素的文本内容
var divText = divElement.text().trim();
// console.log('divText',divText)
var trElement = $(this).closest('tr');
var counttext2 = trElement.find('td[data-field="username2"]').text();
// 判断文本内容是否为空
var divText9 = trElement.find('td[data-field="username9"] .layui-table-cell').text();
var divText1 = trElement.find('td[data-field="username1"] .layui-table-cell').text();
var divText4 = trElement.find('td[data-field="username4"] .layui-table-cell').text();
var divText7 = trElement.find('td[data-field="username7"] .layui-table-cell').text();
var divText6 = trElement.find('td[data-field="username6"] .layui-table-cell').text();
var divText8 = trElement.find('td[data-field="username8"] .layui-table-cell').text();
var divText3 = trElement.find('td[data-field="username3"] .layui-table-cell').text();
if (divText9 == '' && divText1 == '' && divText4 == '' &&
divText7 == '' && divText6 == '' && divText8 == '' && divText3 == '') {
// 获取当前 <td> 元素所在的行 <tr> 元素
var usernameTd = trElement.find('td[data-field="username2"]');
// console.log('trElement',trElement)
// 获取 "username" 列所在的 <td> 元素
// console.log('usernameTd',usernameTd)
// 设置 "username" 列所在 <td> 元素的 colspan 属性为 10
usernameTd.attr('colspan', 10);
usernameTd.find('div').css('width', '100%');
var counttext = trElement.find('td[data-field="username2"]').text();
if(counttext=='小计'){
var countDom = trElement.find('td[data-field="username2"]');
countDom.find('div.layui-table-cell').css('width', '100%');
// $(countDom).nextAll().addBack().remove();
//alert(12121)
countDom.attr('colspan', 7).css('text-align', 'right');
trElement.find('td[data-field="username5"]').attr('colspan', 4).css('text-align', 'left');
$(trElement.find('td[data-field="username5"]')).nextAll().remove();
}else{
$(this).nextAll().addBack().remove();
}
}
var usernameid = trElement.find('td[data-field="usernameid"]').text();
var name = divText;
// console.log('渲染',usernameid,name,counttext2)
//渲染统计值
$('#list-box2 tr[data-ppid="'+usernameid+'"],#list-box2 tr[data-pid="'+usernameid+'"]').each(function() {
const tr = $(this);
// 在当前 tr 中查找第三个 td 元素的文本值
const thirdTdText = tr.find('td:nth-child(4)').text().trim();
// 检查第三个 td 的文本值是否为 "IC超薄读卡器"
if (thirdTdText === name) {
// 在当前 tr 中查找具有 class="nums" 的 td 元素
const subtotal = tr.find('td.subtotal');
const dwxh = tr.find('td.dwxh');
// 如果找到了 numsTd,则进行相应的操作
if (subtotal.length > 0) {
subtotals = subtotal.text()
const market_sell_price = tr.find('td.market_sell_price').text();
const nums = tr.find('td.nums').text();
// 在这里处理找到的 td 元素
trElement.find('td[data-field="username9"]').find('div.layui-table-cell').html(dwxh.text())
trElement.find('td[data-field="username8"]').find('div.layui-table-cell').html(subtotals)
trElement.find('td[data-field="username7"]').find('div.layui-table-cell').html(nums)
trElement.find('td[data-field="username6"]').find('div.layui-table-cell').html(market_sell_price)
// ccnum = cellValue7*cellValue6;
}
}
if (counttext2 === '小计') {
// 在当前 tr 中查找具有 class="nums" 的 td 元素
const tj = tr.find('td.tj');
// console.log('tjtj',tj)
// 如果找到了 numsTd,则进行相应的操作
if (tj.length > 0) {
// alert(cnum)
trElement.find('td[data-field="username5"]').find('div.layui-table-cell').html(tj.text())
}
}
});
var count = 0;
$('#list-box2 tr').each(function() {
var subtotalValue = $(this).find('.subtotal').text().trim();
// console.log('subtotalValue',subtotalValue)
if (subtotalValue) {
count += parseFloat(subtotalValue);
}
});
var lastTr = $('#table').next().find('.layui-table-body tbody tr:last');
if(count){
if (lastTr.attr('id') === 'tj-dom') {
// 存在则更改值
lastTr.find('#count-dom div').each(function() {
$(this).text(count)
});
} else {
// 不存在则添加这个 tr
var totalRow = '<tr id="tj-dom"><td colspan="8" style="text-align: right;">总统计</td><td id="count-dom" colspan="2"><div class="layui-table-cell laytable-cell-3-0-2">' + count + '</div></td></tr>';
$('#table').next().find('.layui-table-body tbody').append(totalRow);
}
}
// 删除当前 <td> 元素后面的所有 <td> 元素
// 获取所有<tr>元素
var rows = document.querySelectorAll('#list-box2 tr');
var dataArray = [];
// 遍历所有行
rows.forEach(function(row) {
var columns = row.querySelectorAll('td');
var rowData = [];
columns.forEach(function(column) {
rowData.push(column.textContent.trim());
});
dataArray.push(rowData);
});
let arra =[]
dataArray.forEach(item=>{
console.log('item',item)
if(item.length > 5){
arra.push({
'mb_id':item[1],
'name': item[2],
'type': item[3],
'dwxh': item[4],
'brand': item[5],
'unit': item[7],
'nums': item[8],
'price': item[9],
'total': item[10],
'comment': item[11],
})
}
})
console.log('dataArray',dataArray);
console.log('arra',arra);
Fast.api.ajax({
url: 'fastflow/bill/Czgprz/setGoods',
data: {
arra
}
}, function (data, ret) {
//成功的回调
// console.log('list', data);
return false
}, function (data, ret) {
//失败的回调
});
});
}, 1000);
}
})
}, 1000);
function convertToHtmlTable(data) {
var html = '<table border="1"><thead><tr>';
// 添加表头
for (var key in data[0]) {
html += '<th>' + key + '</th>';
}
html += '</tr></thead><tbody>';
// 添加表格内容
for (var i = 0; i < data.length; i++) {
html += '<tr>';
for (var key in data[i]) {
html += '<td>' + data[i][key] + '</td>';
}
html += '</tr>';
}
html += '</tbody></table>';
return html;
}