Group.cshtml 7.7 KB

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