Category.cshtml 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. @page
  2. @model Admin.Pages.Faq.CategoryModel
  3. @{
  4. ViewData["Title"] = "FAQ 분류";
  5. }
  6. <div class="container-fluid">
  7. <h3>@ViewData["Title"]</h3>
  8. <hr />
  9. <partial name="_StatusMessage" />
  10. <partial name="_NavTabs" />
  11. <div class="row g-2 align-items-end mt-2">
  12. <div class="col">
  13. Total : @Model.Total
  14. </div>
  15. <div class="col text-end">
  16. <button type="button" id="btnAdd" class="btn btn-primary" form="fAdminWrite">추가</button>
  17. <button type="submit" id="btnSave" class="btn btn-success" form="fAdminWrite">저장</button>
  18. </div>
  19. </div>
  20. <div class="table-responsive">
  21. <form id="fAdminWrite" method="post" accept-charset="utf-8" autocomplete="off"></form>
  22. <table class="table table-striped table-bordered table-hover mt-3">
  23. <caption>
  24. FAQ 분류에 등록된 FAQ 가 있다면 삭제가 불가합니다.<br />
  25. FAQ 분류를 삭제하려면 해당 FAQ 를 먼저 삭제해주세요.
  26. </caption>
  27. <colgroup>
  28. <col style="width: 5%;" />
  29. <col />
  30. <col />
  31. <col />
  32. <col />
  33. <col />
  34. <col />
  35. <col />
  36. <col />
  37. </colgroup>
  38. <thead>
  39. <tr class="text-center">
  40. <th>ID</th>
  41. <th>Code</th>
  42. <th>분류 명</th>
  43. <th>FAQ 수</th>
  44. <th>순서</th>
  45. <th>사용</th>
  46. <th>등록일시</th>
  47. <th>수정일시</th>
  48. <th>비고</th>
  49. </tr>
  50. </thead>
  51. <tbody id="categories">
  52. @if (Model.List == null || Model.List.Count <= 0)
  53. {
  54. <tr>
  55. <td colspan="9">No Data.</td>
  56. </tr>
  57. }
  58. else
  59. {
  60. @foreach (var row in Model.List)
  61. {
  62. var index = row.Index;
  63. <tr>
  64. <td>
  65. <input type="text" readonly class="form-control-plaintext text-center @(row.FaqItemRows > 0 ? "text-white bg-danger" : "")" value="@row.Num" />
  66. <input type="hidden" 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.FaqItemRows</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" checked="@Model.Data[index].IsActive" 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" /></td>
  87. <td><input type="text" name="request[@index].UpdatedAt" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.UpdatedAt" /></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. @section Scripts {
  99. <script>
  100. $(function() {
  101. let $categories = $("#categories");
  102. let total = Number(@Model.Total);
  103. // 추가
  104. $(document).on("click", "#btnAdd", function() {
  105. if (total <= 0) {
  106. $categories.empty();
  107. }
  108. let tableRow = `
  109. <tr>
  110. <td>-</td>
  111. <td>
  112. <input type="text" name="request[${total}].Code" class="form-control" maxlength="30" required form="fAdminWrite" />
  113. </td>
  114. <td>
  115. <input type="text" name="request[${total}].Subject" class="form-control" maxlength="255" required form="fAdminWrite" />
  116. </td>
  117. <td>-</td>
  118. <td>
  119. <input type="number" name="request[${total}].Order" class="form-control" min="-999" max="999" required form="fAdminWrite" />
  120. </td>
  121. <td>
  122. <div class="form-check-inline">
  123. <input class="form-check-input" type="checkbox" id="request_${total}_IsActive" name="request[${total}].IsActive" checked form="fAdminWrite" value="true" />
  124. <label class="form-check-label" for="request_${total}_IsActive">
  125. 사용
  126. </label>
  127. </div>
  128. </td>
  129. <td>-</td>
  130. <td>-</td>
  131. <td>
  132. <button type="button" class="btn btn-danger btn-sm btn-delete">삭제</button>
  133. </td>
  134. </tr>
  135. `;
  136. $categories.append(tableRow);
  137. total++;
  138. recalculateIndices();
  139. });
  140. // 삭제
  141. $(document).on("click", "button.btn-delete", function(e) {
  142. e.target.closest("tr").remove();
  143. total--;
  144. if (total <= 0) {
  145. $categories.append(
  146. `<tr><td colspan="9">No Data.</td></tr>`
  147. );
  148. total = 0;
  149. } else {
  150. recalculateIndices();
  151. }
  152. });
  153. // 저장
  154. $(document).on("click", "#btnSave", function() {
  155. if (confirm("저장 하시겠습니까?")) {
  156. let form = document.getElementById("fAdminWrite");
  157. if (form.checkValidity()) { // HTML5 폼 검증 수행
  158. form.submit();
  159. } else {
  160. form.reportValidity();
  161. }
  162. }
  163. return false;
  164. });
  165. // 인덱스 재계산 함수
  166. function recalculateIndices() {
  167. $categories.find("tr").each(function(index, tr) {
  168. $(tr)
  169. .find("input, label")
  170. .each(function() {
  171. let name = $(this).attr("name");
  172. let id = $(this).attr("id");
  173. if (name) {
  174. $(this).attr("name", name.replace(/\[\d+\]/, `[${index}]`));
  175. }
  176. if (id) {
  177. $(this).attr("id", id.replace(/_\d+_/, `_${index}_`));
  178. }
  179. });
  180. // 인덱스 기반으로 라벨의 `for` 속성도 수정
  181. $(tr)
  182. .find("label")
  183. .each(function() {
  184. let labelFor = $(this).attr("for");
  185. if (labelFor) {
  186. $(this).attr("for", labelFor.replace(/_\d+_/, `_${index}_`));
  187. }
  188. });
  189. });
  190. }
  191. });
  192. </script>
  193. }