Index.cshtml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. @using bitforum.Helpers;
  2. @{
  3. ViewData["Title"] = "FAQ 분류";
  4. var faqCategories = ViewBag.FaqCategories as List<bitforum.Models.Page.Faq.FaqCategory>;
  5. var total = (faqCategories?.Count ?? 0).ToString("N0");
  6. }
  7. <partial name="~/Views/Page/Faq/_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. 분류에 등록된 FAQ 가 있다면 삭제가 불가합니다.<br/>
  26. 분류를 삭제하려면 해당 FAQ 를 먼저 삭제해주세요.
  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. <col width="*" />
  38. </colgroup>
  39. <thead>
  40. <tr class="text-center">
  41. <th>ID</th>
  42. <th>Code</th>
  43. <th>분류 명</th>
  44. <th>FAQ 수</th>
  45. <th>순서</th>
  46. <th>사용</th>
  47. <th>등록일시</th>
  48. <th>수정일시</th>
  49. <th>비고</th>
  50. </tr>
  51. </thead>
  52. <tbody id="categories">
  53. @if (faqCategories == null || !faqCategories.Any())
  54. {
  55. <tr>
  56. <td colspan="9" class="text-center align-middle">No Data.</td>
  57. </tr>
  58. }
  59. else
  60. {
  61. @foreach (var row in faqCategories)
  62. {
  63. var index = faqCategories.IndexOf(row);
  64. <tr class="align-middle text-center">
  65. <td>
  66. <input type="text" name="request[@index].ID" readonly class="form-control-plaintext text-center" required form="fAdminWrite" value="@row.ID" />
  67. </td>
  68. <td>
  69. <input type="text" name="request[@index].Code" class="form-control" maxlength="30" required form="fAdminWrite" value="@row.Code" />
  70. </td>
  71. <td>
  72. <input type="text" name="request[@index].Subject" class="form-control" maxlength="255" required form="fAdminWrite" value="@row.Subject" />
  73. </td>
  74. <td>@row.FaqItem.Count</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>
  79. <div class="form-check-inline">
  80. <input class="form-check-input" type="checkbox" id="request_@(index)_IsActive" name="request[@index].IsActive" @(faqCategories[index].IsActive ? "checked" : "") form="fAdminWrite" value="true" />
  81. <label class="form-check-label" for="request_@(index)_IsActive">
  82. 사용
  83. </label>
  84. </div>
  85. </td>
  86. <td><input type="text" name="request[@index].CreatedAt" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.CreatedAt.GetDateAt()" /></td>
  87. <td><input type="text" name="request[@index].UpdatedAt" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.UpdatedAt.GetDateAt()" /></td>
  88. <td>
  89. <button type="button" class="btn btn-sm btn-danger btn-delete">삭제</button>
  90. </td>
  91. </tr>
  92. }
  93. }
  94. </tbody>
  95. </table>
  96. </div>
  97. </div>
  98. <script type="module">
  99. $(function() {
  100. let $categories = $("#categories");
  101. let total = Number(@total);
  102. // 추가
  103. $(document).on("click", "#btnAdd", function() {
  104. if (total <= 0) {
  105. $categories.empty();
  106. }
  107. let tableRow = `
  108. <tr class="align-middle text-center">
  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}].Subject" class="form-control" maxlength="255" required form="fAdminWrite" />
  115. </td>
  116. <td>-</td>
  117. <td>
  118. <input type="number" name="request[${total}].Order" class="form-control" min="-999" max="999" required form="fAdminWrite" />
  119. </td>
  120. <td>
  121. <div class="form-check-inline">
  122. <input class="form-check-input" type="checkbox" id="request_${total}_IsActive" name="request[${total}].IsActive" checked form="fAdminWrite" value="true" />
  123. <label class="form-check-label" for="request_${total}_IsActive">
  124. 사용
  125. </label>
  126. </div>
  127. </td>
  128. <td>-</td>
  129. <td>-</td>
  130. <td>
  131. <button type="button" class="btn btn-danger btn-sm btn-delete">삭제</button>
  132. </td>
  133. </tr>
  134. `;
  135. $categories.append(tableRow);
  136. total++;
  137. recalculateIndices();
  138. });
  139. // 삭제
  140. $(document).on("click", "button.btn-delete", function(e) {
  141. e.target.closest("tr").remove();
  142. total--;
  143. if (total <= 0) {
  144. $categories.append(
  145. `<tr><td colspan="9" class="text-center align-middle">No Data.</td></tr>`
  146. );
  147. total = 0;
  148. } else {
  149. recalculateIndices();
  150. }
  151. });
  152. // 저장
  153. $(document).on("click", "#btnSave", function() {
  154. if (confirm("저장 하시겠습니까?")) {
  155. document.getElementById("fAdminWrite").submit();
  156. }
  157. return false;
  158. });
  159. // 인덱스 재계산 함수
  160. function recalculateIndices() {
  161. $categories.find("tr").each(function(index, tr) {
  162. $(tr)
  163. .find("input, label")
  164. .each(function() {
  165. let name = $(this).attr("name");
  166. let id = $(this).attr("id");
  167. if (name) {
  168. $(this).attr("name", name.replace(/\[\d+\]/, `[${index}]`));
  169. }
  170. if (id) {
  171. $(this).attr("id", id.replace(/_\d+_/, `_${index}_`));
  172. }
  173. });
  174. // 인덱스 기반으로 라벨의 `for` 속성도 수정
  175. $(tr)
  176. .find("label")
  177. .each(function() {
  178. let labelFor = $(this).attr("for");
  179. if (labelFor) {
  180. $(this).attr("for", labelFor.replace(/_\d+_/, `_${index}_`));
  181. }
  182. });
  183. });
  184. }
  185. });
  186. </script>