Category.cshtml 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. @page
  2. @model Admin.Pages.Crypto.CategoryModel
  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 mt-2">
  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-primary" form="fAdminWrite">추가</button>
  16. <button type="submit" id="btnSave" class="btn btn-success" form="fAdminWrite">저장</button>
  17. </div>
  18. </div>
  19. <div class="table-responsive">
  20. <form id="fAdminWrite" method="post" accept-charset="utf-8" autocomplete="off"></form>
  21. <table class="table table-striped table-bordered table-hover mt-3">
  22. <caption>
  23. <ul>
  24. <li>코인이 등록된 카테고리는 삭제할 수 없습니다.</li>
  25. <li>카테고리를 삭제하려면 해당 코인의 카테고리를 먼저 변경해 주세요.</li>
  26. <li>카테고리 순서는 숫자가 낮을수록 먼저 노출됩니다. (음수 허용)</li>
  27. </ul>
  28. </caption>
  29. <colgroup>
  30. <col style="width: 5%;" />
  31. <col />
  32. <col />
  33. <col />
  34. <col />
  35. <col />
  36. <col />
  37. <col />
  38. <col />
  39. </colgroup>
  40. <thead>
  41. <tr class="text-center">
  42. <th>ID</th>
  43. <th>Code</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="categories">
  54. @if (Model.List == null || Model.List.Count <= 0)
  55. {
  56. <tr>
  57. <td colspan="9">No Data.</td>
  58. </tr>
  59. }
  60. else
  61. {
  62. @foreach (var row in Model.List)
  63. {
  64. var index = row.Index;
  65. <tr>
  66. <td>
  67. <input type="text" readonly class="form-control-plaintext text-center @(row.CoinCount > 0 ? "text-white bg-danger" : "")" value="@row.Num" />
  68. <input type="hidden" name="request[@index].ID" readonly class="form-control-plaintext text-center" required form="fAdminWrite" value="@row.ID" />
  69. </td>
  70. <td>
  71. <input type="text" name="request[@index].Code" class="form-control" maxlength="30" required form="fAdminWrite" value="@row.Code" />
  72. </td>
  73. <td>
  74. <input type="text" name="request[@index].Name" class="form-control" maxlength="100" required form="fAdminWrite" value="@row.Name" />
  75. </td>
  76. <td>@row.CoinCount</td>
  77. <td>
  78. <input type="number" name="request[@index].Order" class="form-control" min="-999" max="999" required form="fAdminWrite" value="@row.Order" />
  79. </td>
  80. <td>
  81. <div class="form-check-inline">
  82. <input class="form-check-input" type="checkbox" id="request_@(index)_IsActive" name="request[@index].IsActive" checked="@Model.Data[index].IsActive" form="fAdminWrite" value="true" />
  83. <label class="form-check-label" for="request_@(index)_IsActive">
  84. 사용
  85. </label>
  86. </div>
  87. </td>
  88. <td><input type="text" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.CreatedAt" /></td>
  89. <td><input type="text" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.UpdatedAt" /></td>
  90. <td>
  91. <button type="button" class="btn btn-sm btn-danger btn-delete">삭제</button>
  92. </td>
  93. </tr>
  94. }
  95. }
  96. </tbody>
  97. </table>
  98. </div>
  99. </div>
  100. @section Scripts {
  101. <script>
  102. $(function() {
  103. let $categories = $("#categories");
  104. let total = Number(@Model.Total);
  105. $(document).on("click", "#btnAdd", function() {
  106. if (total <= 0) {
  107. $categories.empty();
  108. }
  109. let tableRow = `
  110. <tr>
  111. <td>-</td>
  112. <td>
  113. <input type="text" name="request[${total}].Code" class="form-control" maxlength="30" required form="fAdminWrite" />
  114. </td>
  115. <td>
  116. <input type="text" name="request[${total}].Name" class="form-control" maxlength="100" required form="fAdminWrite" />
  117. </td>
  118. <td>-</td>
  119. <td>
  120. <input type="number" name="request[${total}].Order" class="form-control" min="-999" max="999" required form="fAdminWrite" />
  121. </td>
  122. <td>
  123. <div class="form-check-inline">
  124. <input class="form-check-input" type="checkbox" id="request_${total}_IsActive" name="request[${total}].IsActive" checked form="fAdminWrite" value="true" />
  125. <label class="form-check-label" for="request_${total}_IsActive">
  126. 사용
  127. </label>
  128. </div>
  129. </td>
  130. <td>-</td>
  131. <td>-</td>
  132. <td>
  133. <button type="button" class="btn btn-danger btn-sm btn-delete">삭제</button>
  134. </td>
  135. </tr>
  136. `;
  137. $categories.append(tableRow);
  138. total++;
  139. recalculateIndices();
  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. $(document).on("click", "#btnSave", function() {
  154. if (confirm("저장 하시겠습니까?")) {
  155. let form = document.getElementById("fAdminWrite");
  156. if (form.checkValidity()) {
  157. form.submit();
  158. } else {
  159. form.reportValidity();
  160. }
  161. }
  162. return false;
  163. });
  164. function recalculateIndices() {
  165. $categories.find("tr").each(function(index, tr) {
  166. $(tr)
  167. .find("input, label")
  168. .each(function() {
  169. let name = $(this).attr("name");
  170. let id = $(this).attr("id");
  171. if (name) {
  172. $(this).attr("name", name.replace(/\[\d+\]/, `[${index}]`));
  173. }
  174. if (id) {
  175. $(this).attr("id", id.replace(/_\d+_/, `_${index}_`));
  176. }
  177. });
  178. $(tr)
  179. .find("label")
  180. .each(function() {
  181. let labelFor = $(this).attr("for");
  182. if (labelFor) {
  183. $(this).attr("for", labelFor.replace(/_\d+_/, `_${index}_`));
  184. }
  185. });
  186. });
  187. }
  188. });
  189. </script>
  190. }