Index.cshtml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. @using bitforum.Helpers;
  2. @{
  3. ViewData["Title"] = "배너 위치";
  4. var bannerPositions = ViewBag.BannerPositions as List<bitforum.Models.Page.Banner.BannerPosition>;
  5. var total = ViewBag.Total.ToString("N0");
  6. }
  7. <partial name="~/Views/Page/Banner/_Navbar.cshtml" />
  8. <div class="container-fluid">
  9. <h3>@ViewData["Title"]</h3>
  10. <hr />
  11. <partial name="_StatusMessage" />
  12. <div class="row g-2 align-items-end">
  13. <div class="col">
  14. Total : @total
  15. </div>
  16. <div class="col text-end">
  17. <button type="button" id="btnAdd" class="btn btn-sm btn-primary" form="fAdminWrite">추가</button>
  18. <button type="submit" id="btnSave" class="btn btn-sm btn-success" form="fAdminWrite">저장</button>
  19. </div>
  20. </div>
  21. <div class="table-responsive">
  22. <form id="fAdminWrite" asp-action="Save" method="post" accept-charset="utf-8" autocomplete="off"></form>
  23. <table class="table table-striped table-bordered table-hover mt-3">
  24. <caption>
  25. 배너 위치에 등록된 배너가 있다면 삭제가 불가합니다.<br/>
  26. 배너 위치를 삭제하려면 등록된 배너를 먼저 삭제해주세요.
  27. </caption>
  28. <colgroup>
  29. <col width="5%" />
  30. <col width="*" />
  31. <col width="*" />
  32. <col width="*" />
  33. <col width="*" />
  34. <col width="*" />
  35. <col width="*" />
  36. <col width="*" />
  37. </colgroup>
  38. <thead>
  39. <tr>
  40. <th>ID</th>
  41. <th>Code</th>
  42. <th>위치 명</th>
  43. <th>배너 수</th>
  44. <th>사용</th>
  45. <th>등록일시</th>
  46. <th>수정일시</th>
  47. <th>비고</th>
  48. </tr>
  49. </thead>
  50. <tbody id="bannerPositions">
  51. @if (bannerPositions == null || !bannerPositions.Any())
  52. {
  53. <tr>
  54. <td colspan="8">No Data.</td>
  55. </tr>
  56. }
  57. else
  58. {
  59. @foreach (var row in bannerPositions)
  60. {
  61. var index = bannerPositions.IndexOf(row);
  62. <tr class="align-middle text-center">
  63. <td>
  64. <input type="text" name="request[@index].ID" readonly class="form-control-plaintext text-center" required form="fAdminWrite" value="@row.ID" />
  65. </td>
  66. <td>
  67. <input type="text" name="request[@index].Code" class="form-control" maxlength="30" required form="fAdminWrite" value="@row.Code" />
  68. </td>
  69. <td>
  70. <input type="text" name="request[@index].Subject" class="form-control" maxlength="255" required form="fAdminWrite" value="@row.Subject" />
  71. </td>
  72. <td>@row.BannerItem.Count</td>
  73. <td>
  74. <div class="form-check-inline">
  75. <input class="form-check-input" type="checkbox" id="request_@(index)_IsActive" name="request[@index].IsActive" checked="@bannerPositions[index].IsActive" form="fAdminWrite" value="true" />
  76. <label class="form-check-label" for="request_@(index)_IsActive">
  77. 사용
  78. </label>
  79. </div>
  80. </td>
  81. <td><input type="text" name="request[@index].CreatedAt" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.CreatedAt.GetDateAt()" /></td>
  82. <td><input type="text" name="request[@index].UpdatedAt" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.UpdatedAt.GetDateAt()" /></td>
  83. <td>
  84. <button type="button" class="btn btn-sm btn-danger btn-delete">삭제</button>
  85. </td>
  86. </tr>
  87. }
  88. }
  89. </tbody>
  90. </table>
  91. </div>
  92. </div>
  93. <script type="module">
  94. $(function() {
  95. let $bannerPositions = $("#bannerPositions");
  96. let total = Number(@total);
  97. // 추가
  98. $(document).on("click", "#btnAdd", function() {
  99. if (total <= 0) {
  100. $bannerPositions.empty();
  101. }
  102. let tableRow = `
  103. <tr>
  104. <td>-</td>
  105. <td>
  106. <input type="text" name="request[${total}].Code" class="form-control" maxlength="30" required form="fAdminWrite" />
  107. </td>
  108. <td>
  109. <input type="text" name="request[${total}].Subject" class="form-control" maxlength="255" required form="fAdminWrite" />
  110. </td>
  111. <td>-</td>
  112. <td>
  113. <div class="form-check-inline">
  114. <input class="form-check-input" type="checkbox" id="request_${total}_IsActive" name="request[${total}].IsActive" checked form="fAdminWrite" value="true" />
  115. <label class="form-check-label" for="request_${total}_IsActive">
  116. 사용
  117. </label>
  118. </div>
  119. </td>
  120. <td>-</td>
  121. <td>-</td>
  122. <td>
  123. <button type="button" class="btn btn-danger btn-sm btn-delete">삭제</button>
  124. </td>
  125. </tr>
  126. `;
  127. $bannerPositions.append(tableRow);
  128. total++;
  129. recalculateIndices();
  130. });
  131. // 삭제
  132. $(document).on("click", "button.btn-delete", function(e) {
  133. e.target.closest("tr").remove();
  134. total--;
  135. if (total <= 0) {
  136. $bannerPositions.append(
  137. `<tr><td colspan="8">No Data.</td></tr>`
  138. );
  139. total = 0;
  140. } else {
  141. recalculateIndices();
  142. }
  143. });
  144. // 저장
  145. $(document).on("click", "#btnSave", function() {
  146. if (confirm("저장 하시겠습니까?")) {
  147. let form = document.getElementById("fAdminWrite");
  148. if (form.checkValidity()) { // HTML5 폼 검증 수행
  149. form.submit();
  150. } else {
  151. form.reportValidity();
  152. }
  153. }
  154. return false;
  155. });
  156. // 인덱스 재계산 함수
  157. function recalculateIndices() {
  158. $bannerPositions.find("tr").each(function(index, tr) {
  159. $(tr)
  160. .find("input, label")
  161. .each(function() {
  162. let name = $(this).attr("name");
  163. let id = $(this).attr("id");
  164. if (name) {
  165. $(this).attr("name", name.replace(/\[\d+\]/, `[${index}]`));
  166. }
  167. if (id) {
  168. $(this).attr("id", id.replace(/_\d+_/, `_${index}_`));
  169. }
  170. });
  171. // 인덱스 기반으로 라벨의 `for` 속성도 수정
  172. $(tr)
  173. .find("label")
  174. .each(function() {
  175. let labelFor = $(this).attr("for");
  176. if (labelFor) {
  177. $(this).attr("for", labelFor.replace(/_\d+_/, `_${index}_`));
  178. }
  179. });
  180. });
  181. }
  182. });
  183. </script>