|
3037 | 3037 | const allHeaderRowCellsForSort = document.querySelectorAll( |
3038 | 3038 | '.crm__output-table-head-cell' |
3039 | 3039 | ); |
3040 | | - let sortDirectionUpDown = true; |
| 3040 | + let sortDirectionUpDown = true; // "флаг" для работы с направлением сортировки |
3041 | 3041 |
|
3042 | 3042 | function sortClientsByTableCells(event) { |
3043 | | - const clickedTableCell = event.target.textContent; // определение заглавного поля/ячейки, по которой происходит "click" - событие, фиксация контента |
| 3043 | + const clickedTableCell = event.target.closest('th'); // определение заглавного поля/ячейки, по которой происходит "click" - событие, фиксация |
3044 | 3044 |
|
3045 | | - console.log(clickedTableCell); |
| 3045 | + if (!clickedTableCell) return; // если клик не по "th", отмена действий |
3046 | 3046 |
|
3047 | 3047 | updateClientsDataArr.sort((a, b) => { |
3048 | | - if (clickedTableCell === 'ID') { |
3049 | | - return a.shortId - b.shortId; |
3050 | | - } else if (clickedTableCell === 'Фамилия Имя Отчество') { |
| 3048 | + if (clickedTableCell.id === 'table-th-id') { |
| 3049 | + return sortDirectionUpDown |
| 3050 | + ? b.shortId - a.shortId |
| 3051 | + : a.shortId - b.shortId; |
| 3052 | + } else if (clickedTableCell.id === 'table-th-fio') { |
3051 | 3053 | return sortDirectionUpDown |
3052 | 3054 | ? a.fullName.localeCompare(b.fullName) |
3053 | 3055 | : b.fullName.localeCompare(a.fullName); |
3054 | | - } else if (clickedTableCell === 'Дата и время создания') { |
| 3056 | + } else if (clickedTableCell.id === 'table-th-dt') { |
3055 | 3057 | return sortDirectionUpDown |
3056 | | - ? a.faculty.localeCompare(b.faculty) |
3057 | | - : b.faculty.localeCompare(a.faculty); |
3058 | | - } else if (clickedTableCell === 'Последние изменения') { |
3059 | | - const birthDateComparison = |
3060 | | - new Date(a.birthDate).setHours(0, 0, 0, 0) - |
3061 | | - new Date(b.birthDate).setHours(0, 0, 0, 0); // корректировка часов рождения (всем одно) |
3062 | | - if (birthDateComparison !== 0) { |
3063 | | - return sortDirectionUpDown |
3064 | | - ? birthDateComparison |
3065 | | - : -birthDateComparison; |
3066 | | - } |
| 3058 | + ? new Date(b.createdAt) - new Date(a.createdAt) |
| 3059 | + : new Date(a.createdAt) - new Date(b.createdAt); |
| 3060 | + } else if (clickedTableCell.id === 'table-th-change') { |
3067 | 3061 | return sortDirectionUpDown |
3068 | | - ? a.fullName.localeCompare(b.fullName) |
3069 | | - : b.fullName.localeCompare(a.fullName); // если даты рождения равны, по ФИО будет сортировка |
3070 | | - } else if (clickedTableCell === 'Годы обучения') { |
3071 | | - const startYearComparison = a.startYear - b.startYear; |
3072 | | - if (startYearComparison !== 0) { |
3073 | | - return sortDirectionUpDown |
3074 | | - ? startYearComparison |
3075 | | - : -startYearComparison; |
3076 | | - } |
3077 | | - return sortDirectionUpDown |
3078 | | - ? a.fullName.localeCompare(b.fullName) |
3079 | | - : b.fullName.localeCompare(a.fullName); // если годы начала/окончания равны, по ФИО будет сортировка |
| 3062 | + ? new Date(a.updatedAt) - new Date(b.updatedAt) |
| 3063 | + : new Date(b.updatedAt) - new Date(a.updatedAt); |
3080 | 3064 | } |
| 3065 | + |
3081 | 3066 | return 0; |
3082 | 3067 | }); |
3083 | 3068 |
|
3084 | | - addClientsToTable(updateClientsDataArr); // пере-рисовка (пере-компоновка) после сортировки (прожатия ячеек) |
| 3069 | + addClientsToTable(updateClientsDataArr); // пере-рисовка (пере-компоновка) таблицы после сортировки (прожатия ячеек) |
3085 | 3070 | } |
3086 | 3071 |
|
3087 | 3072 | allHeaderRowCellsForSort.forEach((cell) => { |
|
0 commit comments