๊ด€๋ฆฌ ๋ฉ”๋‰ด

bom's happy life

[JS๋™์ ์บ˜๋ฆฐ๋”] ํ† ,์ผ ๊ธฐ๋ณธ๊ฐ’ disabled, ์˜ค๋Š˜๋‚ ์งœ ํฌํ•จํ•œ ์ด์ „๋‚ ์งœ disabled ๋ณธ๋ฌธ

Deveolpment Study๐Ÿ—‚๏ธ/Javascript

[JS๋™์ ์บ˜๋ฆฐ๋”] ํ† ,์ผ ๊ธฐ๋ณธ๊ฐ’ disabled, ์˜ค๋Š˜๋‚ ์งœ ํฌํ•จํ•œ ์ด์ „๋‚ ์งœ disabled

bompeach 2024. 5. 13. 16:34
<div id="calendar-controls">
  <button id="prevMonth">โ—</button>
  <span id="currentYearMonth"></span>
  <button id="nextMonth">โ–ท</button>
</div>
<div id="calendar"></div>
<div id="time-slots" style="display:none;"> 
</div>
<div id="selectedDateTime"></div>
table {
  width: 400px;
  height: 343px;
  border-collapse: collapse;
  text-align: center;
}

th, td {
  border: 1px solid #ccc;
  padding: 8px;
  text-align: center;
}

th.day-name {  /* ์š”์ผ ํ‘œ์‹œ ๋ถ€๋ถ„์˜ ์Šคํƒ€์ผ */
  background-color: transparent;  /* ๋ฐฐ๊ฒฝ ํˆฌ๋ช… */
  border: none;  /* ํ…Œ๋‘๋ฆฌ ์ œ๊ฑฐ */
  color: #4a506b;  /* ๊ธ€์ž์ƒ‰ ํšŒ์ƒ‰ */
}

td.date-cell {  /* ๋‚ ์งœ ์…€ ์Šคํƒ€์ผ */
  cursor: pointer;  /* ์ปค์„œ๋ฅผ ํฌ์ธํ„ฐ๋กœ ์„ค์ • */
}

td.date-cell:hover {
  background-color:#586e9a;
  color:white;
}

button {
  padding: 10px;
  margin: 5px;
  background-color: #f9f9f9;
  border: none;
  cursor: pointer;
}

#calendar-controls {
  width: 400px;
  text-align: center;
  margin-bottom: 20px;
}

.selected {
  background-color: #e63c64;  // ์„ ํƒ๋œ ์…€์˜ ๋ฐฐ๊ฒฝ์ƒ‰
}
label {
  margin-right: 20px;
}
input[type="radio"] {
  margin-right: 5px;
}
.disabled {
  color: #d3d3d3; 
  background-color: #f9f9f9; 
  pointer-events: none; /* ํด๋ฆญ ๋น„ํ™œ์„ฑํ™” */
}
var today = new Date();
var currentYear = today.getFullYear();
var currentMonth = today.getMonth() + 1;

window.onload = function() {
  createCalendar(currentYear, currentMonth);

  document.getElementById('prevMonth').addEventListener('click', function() {
    changeMonth(-1);
  });

  document.getElementById('nextMonth').addEventListener('click', function() {
    changeMonth(1);
  });
};

function createCalendar(year, month) {
  const container = document.getElementById('calendar');
  container.innerHTML = '';
  const daysInMonth = new Date(year, month, 0).getDate();
  const firstDayOfMonth = new Date(year, month - 1, 1).getDay();

  const table = document.createElement('table');
  const header = table.createTHead();
  const headerRow = header.insertRow(0);
  const dayNames = ['์ผ', '์›”', 'ํ™”', '์ˆ˜', '๋ชฉ', '๊ธˆ', 'ํ† '];

  dayNames.forEach(day => {
    const cell = document.createElement('th');
    cell.textContent = day;
    headerRow.appendChild(cell);
  });

  const calendarBody = table.createTBody();
  let date = 1;

  for (let i = 0; i < 6; i++) {
    const row = calendarBody.insertRow();
    let rowHasDates = false;

    for (let j = 0; j < 7; j++) {
      const cell = row.insertCell();
      const currentDate = new Date(year, month - 1, date);
      const dayOfWeek = currentDate.getDay(); // ์ผ์š”์ผ์€ 0, ํ† ์š”์ผ์€ 6

      if (i === 0 && j < firstDayOfMonth) {
        cell.classList.add('empty');
      } else if (date > daysInMonth) {
        cell.classList.add('empty');
      } else {
        cell.textContent = date;
        cell.classList.add('date-cell');
        
        if (currentDate < today || dayOfWeek === 0 || dayOfWeek === 6) {
          cell.classList.add('disabled');
          cell.style.color = '#d3d3d3'; // ๋น„ํ™œ์„ฑํ™”๋œ ๋‚ ์งœ ์ƒ‰์ƒ
        } else {
          (function(currentYear, currentMonth, currentDate) {
            cell.addEventListener('click', function() {
              document.querySelectorAll('.date-cell').forEach(c => c.classList.remove('selected'));
              this.classList.add('selected');
              displaySelectedDate(currentYear, currentMonth, currentDate);
            });
          })(year, month, date);
        }

        if (today.getFullYear() === year && today.getMonth() + 1 === month && today.getDate() === date) {
          cell.style.border = "3px solid #586e9a"; // ์˜ค๋Š˜๋‚ ์งœ ํ…Œ๋‘๋ฆฌ ์Šคํƒ€์ผ
        }
        
        date++;
        rowHasDates = true;
      }
    }

    if (!rowHasDates) {
      calendarBody.removeChild(row);
    }
  }

  container.appendChild(table);
  updateHeader(year, month);
}

function displaySelectedDate(year, month, date) {
  const displayArea = document.getElementById('selectedDateTime');
  const monthNames = ["1์›”", "2์›”", "3์›”", "4์›”", "5์›”", "6์›”", "7์›”", "8์›”", "9์›”", "10์›”", "11์›”", "12์›”"];
  const formattedDate = `${year}๋…„ ${monthNames[month - 1]} ${date}์ผ`;
  displayArea.textContent = formattedDate;

  // ์‹œ๊ฐ„๋Œ€ ๋ผ๋””์˜ค ๋ฒ„ํŠผ ์ƒ์„ฑ
  const times = ["09:30", "10:00", "10:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00"];
  const timeSelector = document.createElement('div');

  times.forEach(time => {
    const label = document.createElement('label');
    const radioInput = document.createElement('input');
    radioInput.type = 'radio';
    radioInput.name = 'appointmentTime';
    radioInput.value = time;
    
    label.appendChild(radioInput);
    label.appendChild(document.createTextNode(time));
    timeSelector.appendChild(label);

    radioInput.addEventListener('click', function() {
      console.log(`์‹œ๊ฐ„์„ ํƒ :  ${time} `);
    });
  });

  displayArea.appendChild(timeSelector);
}

function updateHeader(year, month) {
  const monthNames = ["1์›”", "2์›”", "3์›”", "4์›”", "5์›”", "6์›”", "7์›”", "8์›”", "9์›”", "10์›”", "11์›”", "12์›”"];
  document.getElementById('currentYearMonth').textContent = `${year}๋…„ ${monthNames[month - 1]}`;
}

function changeMonth(delta) {
  currentMonth += delta;
  if (currentMonth < 1) {
    currentYear -= 1;
    currentMonth = 12;
  } else if (currentMonth > 12) {
    currentYear += 1;
    currentMonth = 1;
  }
  createCalendar(currentYear, currentMonth);
}