Index.cshtml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. @model bitforum.Models.Views.FaqCategoryViewModel
  2. @{
  3. ViewData["Title"] = "FAQ 관리";
  4. var total = (Model?.FaqCategories?.Count ?? 0).ToString("N0");
  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 : @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"></form>
  21. <table class="table table-striped table-bordered table-hover mt-3">
  22. <colgroup>
  23. <col width="5%" />
  24. <col width="*" />
  25. <col width="*" />
  26. <col width="*" />
  27. <col width="*" />
  28. <col width="*" />
  29. <col width="*" />
  30. <col width="*" />
  31. <col width="*" />
  32. </colgroup>
  33. <thead>
  34. <tr class="text-center">
  35. <th>ID</th>
  36. <th>Code</th>
  37. <th>분류 명</th>
  38. <th>게시글 수</th>
  39. <th>순서</th>
  40. <th>사용</th>
  41. <th>등록일시</th>
  42. <th>수정일시</th>
  43. <th>비고</th>
  44. </tr>
  45. </thead>
  46. <tbody id="categories">
  47. @if (Model?.FaqCategories == null || !Model.FaqCategories.Any())
  48. {
  49. <tr>
  50. <td colspan="9" class="text-center align-middle">No Data.</td>
  51. </tr>
  52. }
  53. else
  54. {
  55. @foreach (var row in Model.FaqCategories)
  56. {
  57. var index = Model.FaqCategories.IndexOf(row);
  58. var faqItemRows = (ViewBag?.ItemCounts != null && ViewBag.ItemCounts.ContainsKey(row.ID) ? ViewBag.ItemCounts[row.ID] : 0);
  59. <tr class="align-middle text-center">
  60. <td>
  61. <input type="text" name="FaqCategories[@index].ID" readonly class="form-control-plaintext text-center" required form="fAdminWrite" value="@row.ID" />
  62. </td>
  63. <td>
  64. <input type="text" name="FaqCategories[@index].Code" class="form-control" maxlength="30" required form="fAdminWrite" value="@row.Code" />
  65. </td>
  66. <td>
  67. <input type="text" name="FaqCategories[@index].Subject" class="form-control" maxlength="255" required form="fAdminWrite" value="@row.Subject" />
  68. </td>
  69. <td>@faqItemRows</td>
  70. <td>
  71. <input type="number" name="FaqCategories[@index].Order" class="form-control" min="-999" max="999" required form="fAdminWrite" value="@row.Order" />
  72. </td>
  73. <td>
  74. <div class="form-check-inline">
  75. <input class="form-check-input" type="checkbox" id="FaqCategories_@(index)_IsActive" name="FaqCategories[@index].IsActive" @(Model.FaqCategories[index].IsActive ? "checked" : "") form="fAdminWrite" value="true" />
  76. <label class="form-check-label" for="FaqCategories_@(index)_IsActive">
  77. 사용
  78. </label>
  79. </div>
  80. </td>
  81. <td><input type="text" name="FaqCategories[@index].CreatedAt" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.CreatedAt" /></td>
  82. <td><input type="text" name="FaqCategories[@index].UpdatedAt" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.UpdatedAt" /></td>
  83. <td>
  84. <button type="button" class="btn btn-danger btn-sm 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 $categories = $("#categories");
  96. let total = Number(@total);
  97. // 추가
  98. $(document).on("click", "#btnAdd", function() {
  99. if (total <= 0) {
  100. $categories.empty();
  101. }
  102. let tableRow = `
  103. <tr class="align-middle text-center">
  104. <td>-</td>
  105. <td>
  106. <input type="text" name="FaqCategories[${total}].Code" class="form-control" maxlength="30" required form="fAdminWrite" />
  107. </td>
  108. <td>
  109. <input type="text" name="FaqCategories[${total}].Subject" class="form-control" maxlength="255" required form="fAdminWrite" />
  110. </td>
  111. <td>-</td>
  112. <td>
  113. <input type="number" name="FaqCategories[${total}].Order" class="form-control" min="-999" max="999" required form="fAdminWrite" />
  114. </td>
  115. <td>
  116. <div class="form-check-inline">
  117. <input class="form-check-input" type="checkbox" id="FaqCategories_${total}_IsActive" name="FaqCategories[${total}].IsActive" checked form="fAdminWrite" value="true" />
  118. <label class="form-check-label" for="FaqCategories_${total}_IsActive">
  119. 사용
  120. </label>
  121. </div>
  122. </td>
  123. <td>-</td>
  124. <td>-</td>
  125. <td>
  126. <button type="button" class="btn btn-danger btn-sm btn-delete">삭제</button>
  127. </td>
  128. </tr>
  129. `;
  130. $categories.append(tableRow);
  131. total++;
  132. recalculateIndices();
  133. });
  134. // 삭제
  135. $(document).on("click", "button.btn-delete", function(e) {
  136. e.target.closest("tr").remove();
  137. total--;
  138. if (total <= 0) {
  139. $categories.append(
  140. `<tr><td colspan="9" class="text-center align-middle">No Data.</td></tr>`
  141. );
  142. total = 0;
  143. } else {
  144. recalculateIndices();
  145. }
  146. });
  147. // 저장
  148. $(document).on("click", "#btnSave", function() {
  149. if (confirm("저장 하시겠습니까?")) {
  150. document.getElementById("fAdminWrite").submit();
  151. }
  152. return false;
  153. });
  154. // 인덱스 재계산 함수
  155. function recalculateIndices() {
  156. $categories.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>