Group.cshtml 7.6 KB

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