|
1 | 1 | $(() => { |
2 | | - let count = 0; |
3 | | - $('#btn').dxButton({ |
4 | | - text: `Click count: ${count}`, |
5 | | - onClick(e) { |
6 | | - count += 1; |
7 | | - e.component.option('text', `Click count: ${count}`); |
| 2 | + const store = DevExpress.data.AspNet.createStore({ |
| 3 | + key: 'ID', |
| 4 | + loadUrl: `${url}/Tasks`, |
| 5 | + updateUrl: `${url}/UpdateTask`, |
| 6 | + onBeforeSend(method, ajaxOptions) { |
| 7 | + ajaxOptions.xhrFields = { |
| 8 | + withCredentials: true, |
| 9 | + }; |
8 | 10 | }, |
9 | 11 | }); |
| 12 | + let updateInProgress = false; |
| 13 | + |
| 14 | + function getDataGridConfiguration(index) { |
| 15 | + return { |
| 16 | + height: 440, |
| 17 | + dataSource: { |
| 18 | + store, |
| 19 | + reshapeOnPush: true, |
| 20 | + }, |
| 21 | + showBorders: true, |
| 22 | + filterValue: ['Status', '=', index], |
| 23 | + selection: { |
| 24 | + mode: 'multiple', |
| 25 | + }, |
| 26 | + rowDragging: { |
| 27 | + data: index, |
| 28 | + group: 'tasksGroup', |
| 29 | + onAdd, |
| 30 | + onDragStart(e) { |
| 31 | + const selectedData = e.component.getSelectedRowsData(); |
| 32 | + e.itemData = getVisibleRowValues(selectedData, e.component); |
| 33 | + e.cancel = !canDrag(e); |
| 34 | + }, |
| 35 | + dragTemplate(dragData) { |
| 36 | + const itemsContainer = $('<table>').addClass('drag-container'); |
| 37 | + dragData.itemData.forEach(((rowData) => { |
| 38 | + const itemContainer = $('<tr>'); |
| 39 | + |
| 40 | + Object.keys(rowData).forEach((field) => { |
| 41 | + itemContainer.append($('<td>').text(rowData[field])); |
| 42 | + }); |
| 43 | + |
| 44 | + itemsContainer.append(itemContainer); |
| 45 | + })); |
| 46 | + return $('<div>').append(itemsContainer); |
| 47 | + }, |
| 48 | + }, |
| 49 | + scrolling: { |
| 50 | + mode: 'virtual', |
| 51 | + }, |
| 52 | + columns: [{ |
| 53 | + dataField: 'Subject', |
| 54 | + dataType: 'string', |
| 55 | + }, { |
| 56 | + dataField: 'Priority', |
| 57 | + dataType: 'number', |
| 58 | + width: 80, |
| 59 | + lookup: { |
| 60 | + dataSource: priorities, |
| 61 | + valueExpr: 'id', |
| 62 | + displayExpr: 'text', |
| 63 | + }, |
| 64 | + }, { |
| 65 | + dataField: 'Status', |
| 66 | + dataType: 'number', |
| 67 | + visible: false, |
| 68 | + }], |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + $('#grid1').dxDataGrid(getDataGridConfiguration(1)); |
| 73 | + |
| 74 | + $('#grid2').dxDataGrid(getDataGridConfiguration(2)); |
| 75 | + |
| 76 | + function onAdd(e) { |
| 77 | + const selectedRowKeys = e.fromComponent.getSelectedRowKeys(); |
| 78 | + const updateProcess = []; |
| 79 | + const changes = []; |
| 80 | + updateInProgress = true; |
| 81 | + e.fromComponent.beginCustomLoading('Loading...'); |
| 82 | + e.toComponent.beginCustomLoading('Loading...'); |
| 83 | + |
| 84 | + selectedRowKeys.forEach((key) => { |
| 85 | + const values = { Status: e.toData }; |
| 86 | + updateProcess.push(store.update(key, values)); |
| 87 | + changes.push({ |
| 88 | + type: 'update', |
| 89 | + key, |
| 90 | + data: values, |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + Promise.all(updateProcess).then(() => { |
| 95 | + store.push(changes); |
| 96 | + e.fromComponent.endCustomLoading(); |
| 97 | + e.toComponent.endCustomLoading(); |
| 98 | + updateInProgress = false; |
| 99 | + |
| 100 | + e.fromComponent.clearSelection(); |
| 101 | + if (!shouldClearSelection()) { |
| 102 | + e.toComponent.selectRows(selectedRowKeys, true); |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + function canDrag(e) { |
| 108 | + if (updateInProgress) return false; |
| 109 | + const visibleRows = e.component.getVisibleRows(); |
| 110 | + return visibleRows.some((r) => r.isSelected && r.rowIndex === e.fromIndex); |
| 111 | + } |
| 112 | + |
| 113 | + function getVisibleRowValues(rowsData, grid) { |
| 114 | + const visibleColumns = grid.getVisibleColumns().filter((c) => c.dataField); |
| 115 | + const selectedData = rowsData.map((s) => { |
| 116 | + const visibleValues = {}; |
| 117 | + visibleColumns.forEach((column) => { |
| 118 | + visibleValues[column.dataField] = column.lookup |
| 119 | + ? column.lookup.calculateCellValue(s[column.dataField]) |
| 120 | + : s[column.dataField]; |
| 121 | + }); |
| 122 | + return visibleValues; |
| 123 | + }); |
| 124 | + return selectedData; |
| 125 | + } |
| 126 | + |
| 127 | + function shouldClearSelection() { |
| 128 | + return $('#clear-after-drop-switch').dxSwitch('option', 'value'); |
| 129 | + } |
| 130 | + |
| 131 | + $('#clear-after-drop-switch').dxSwitch({}); |
10 | 132 | }); |
0 commit comments