Как я могу увеличить размер моего модального в таблице - PullRequest
0 голосов
/ 27 сентября 2019

Я сделал таблицу, и я хочу, чтобы различные ячейки были активными, чтобы появился модал.Я понял, что работает, когда модал в одной строке, см. Test3 в фрагменте.

Однако, когда я создаю модал в нескольких строках, он не делает всю ячейку кликабельной, только ее часть,При наведении на него курсора изменяется только цвет кликабельной части, но я хочу, чтобы вся ячейка была кликабельной и, таким образом, меняла цвет при наведении на нее курсора.Прямо как "test3".

Как я могу изменить это, чтобы оно охватывало всю ячейку?

enter image description here

var btn = document.querySelectorAll("button.modal-button");


var modals = document.querySelectorAll('.modal');

// Get the <span> element that closes the modal
var spans = document.getElementsByClassName("close");

// When the user clicks the button, open the modal
for (var i = 0; i < btn.length; i++) {
  btn[i].onclick = function(e) {
    e.preventDefault();
    modal = document.querySelector(e.target.getAttribute("href"));
    modal.style.display = "block";
  }
}

// When the user clicks on <span> (x), close the modal
for (var i = 0; i < spans.length; i++) {
  spans[i].onclick = function() {
    for (var index in modals) {
      if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";
    }
  }
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target.classList.contains('modal')) {
    for (var index in modals) {
      if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";
    }
  }
}
#tableone {
  background-color: transparent;
  border-collapse: collapse;
  width: 100%;
}

#tableone td,
th {
  border: 1px solid #000000;
  padding: 8px;
}

#tableone th {
  padding: 8px;
  text-align: left;
  border: 1px solid #000000;
  background-color: rgba(0, 110, 167, 1);
  color: white;
}

.modalTD {
  padding: 0!important;
  background-color: #00a6d6;
}


/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 10px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content */

.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0px;
  border: 1px solid #888;
  width: 80%;
}


/* The Close Button */

.close {
  color: black;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: white;
  text-decoration: none;
  cursor: pointer;
}

.notebook {
  border-collapse: collapse;
  width: 70%;
}

.notebook td.modalTD:hover .block:hover {
  background-color: yellow;
  color: black;
}

.notebook td,
.notebook th {
  border: 1px solid #ddd;
  padding: 0px;
}

.block {
  display: block;
  width: 100%;
  height: 100%;
  border: none;
  background-color: #00a6d6;
  padding: 14px 28px;
  font-size: 16px;
  cursor: pointer;
  text-align: center;
  box-sizing: border-box;
}
<div class="centerjustify">
  <h2>Table 1</h2>
  <br>
  <table id="tableone" class="notebook">
    <tr id="category">
      <th style="background-color:transparent; color:black;"></th>
      <th style="background-color:transparent; color:black;" colspan=2>One</th>
      <th style="background-color:transparent; color:black;" colspan=2>Two</th>
    </tr>
    <tr>
      <td class="month">April</td>
      <td rowspan=4 class="modalTD">
        <!-- Trigger/Open The Modal -->
        <button class="block modal-button" href="#myModal1">Test</button>

        <!-- The Modal -->
        <div id="myModal1" class="modal">

          <!-- Modal content -->
          <div class="modal-content">
            <div class="modal-body">
              <p>Some text in the Modal Body</p>
              <p>Some other text...</p>
            </div>
          </div>

        </div>
      </td>
      <td rowspan=3 class="modalTD">
        <!-- Trigger/Open The Modal -->
        <button class="block modal-button" href="#myModal2">Test2</button>

        <!-- The Modal -->
        <div id="myModal2" class="modal">

          <!-- Modal content -->
          <div class="modal-content">
            <div class="modal-body">
              <p>Some text in the Modal Body</p>
              <p>Some other text...</p>
            </div>

          </div>

        </div>
      </td>
      <td colspan=2></td>
    </tr>
    <tr>
      <td class="month">May</td>


      <td colspan=2></td>
    </tr>
    <tr>
      <td class="month">June</td>
      <td colspan=2></td>

    </tr>
    <tr>
      <td class="month">July</td>


      <td></td>
      <td colspan=2 class="modalTD">
        <!-- Trigger/Open The Modal -->
        <button class="block modal-button" href="#myModal3">Test 3</button>

        <!-- The Modal -->
        <div id="myModal3" class="modal">

          <!-- Modal content -->
          <div class="modal-content">
            <div class="modal-body">
              <p>Some text in the Modal Body</p>
              <p>Some other text...</p>
            </div>
          </div>

        </div>
      </td>
    </tr>

  </table>

</div>

1 Ответ

0 голосов
/ 27 сентября 2019

Один из способов достижения этой цели - установить высоту кнопок, равную высоте внешних ячеек td.

Вот вам пример работы:

var btn = document.querySelectorAll("button.modal-button");
var modalTD = document.querySelectorAll(".modalTD") // get modalTD


var modals = document.querySelectorAll('.modal');

// Get the <span> element that closes the modal
var spans = document.getElementsByClassName("close");

// When the user clicks the button, open the modal
for (var i = 0; i < btn.length; i++) {
  btn[i].style.height = modalTD[i].clientHeight + 'px' //set height of outer td to buttons
  btn[i].onclick = function(e) {
    e.preventDefault();    
    modal = document.querySelector(e.target.getAttribute("href"));
    modal.style.display = "block";
  }
}

// When the user clicks on <span> (x), close the modal
for (var i = 0; i < spans.length; i++) {
  spans[i].onclick = function() {
    for (var index in modals) {
      if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";
    }
  }
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target.classList.contains('modal')) {
    for (var index in modals) {
      if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";
    }
  }
}
#tableone {
  background-color: transparent;
  border-collapse: collapse;
  width: 100%;
}

#tableone td,
th {
  border: 1px solid #000000;
  padding: 8px;
}

#tableone th {
  padding: 8px;
  text-align: left;
  border: 1px solid #000000;
  background-color: rgba(0, 110, 167, 1);
  color: white;
}

.modalTD {
  padding: 0!important;
  background-color: #00a6d6;
}


/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 10px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content */

.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0px;
  border: 1px solid #888;
  width: 80%;
}


/* The Close Button */

.close {
  color: black;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: white;
  text-decoration: none;
  cursor: pointer;
}

.notebook {
  border-collapse: collapse;
  width: 70%;
}

.notebook td.modalTD:hover .block:hover {
  background-color: yellow;
  color: black;
}

.notebook td,
.notebook th {
  border: 1px solid #ddd;
  padding: 0px;
}

.block {
  display: block;
  width: 100%;
  height: 100%;
  border: none;
  background-color: #00a6d6;
  padding: 14px 28px;
  font-size: 16px;
  cursor: pointer;
  text-align: center;
  box-sizing: border-box;
}
<div class="centerjustify">
  <h2>Table 1</h2>
  <br>
  <table id="tableone" class="notebook">
    <tr id="category">
      <th style="background-color:transparent; color:black;"></th>
      <th style="background-color:transparent; color:black;" colspan=2>One</th>
      <th style="background-color:transparent; color:black;" colspan=2>Two</th>
    </tr>
    <tr>
      <td class="month">April</td>
      <td rowspan=4 class="modalTD">
        <!-- Trigger/Open The Modal -->
        <button class="block modal-button" href="#myModal1">Test</button>

        <!-- The Modal -->
        <div id="myModal1" class="modal">

          <!-- Modal content -->
          <div class="modal-content">
            <div class="modal-body">
              <p>Some text in the Modal Body</p>
              <p>Some other text...</p>
            </div>
          </div>

        </div>
      </td>
      <td rowspan=3 class="modalTD">
        <!-- Trigger/Open The Modal -->
        <button class="block modal-button" href="#myModal2">Test2</button>

        <!-- The Modal -->
        <div id="myModal2" class="modal">

          <!-- Modal content -->
          <div class="modal-content">
            <div class="modal-body">
              <p>Some text in the Modal Body</p>
              <p>Some other text...</p>
            </div>

          </div>

        </div>
      </td>
      <td colspan=2></td>
    </tr>
    <tr>
      <td class="month">May</td>


      <td colspan=2></td>
    </tr>
    <tr>
      <td class="month">June</td>
      <td colspan=2></td>

    </tr>
    <tr>
      <td class="month">July</td>


      <td></td>
      <td colspan=2 class="modalTD">
        <!-- Trigger/Open The Modal -->
        <button class="block modal-button" href="#myModal3">Test 3</button>

        <!-- The Modal -->
        <div id="myModal3" class="modal">

          <!-- Modal content -->
          <div class="modal-content">
            <div class="modal-body">
              <p>Some text in the Modal Body</p>
              <p>Some other text...</p>
            </div>
          </div>

        </div>
      </td>
    </tr>

  </table>

</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...