table.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. data="";
  2. function creatTable(){
  3. //這個函式的引數可以是從後臺傳過來的也可以是從其他任何地方傳過來的
  4. //這裡我假設這個data是一個長度為5的字串陣列 我要把他放在表格的一行裡面,分成五列
  5. //alert("eee");
  6. var tableData = '<div class="border rounded-3 py-3"><table style="font-size: 18px;" id="system-table" class="table text-center table-borderless"><thead><tr><th>單位分類</th><th>使用者分類</th><th>帳號</th><th>電子郵件</th><th>權限設定</th><th>備註</th><th>+新增</th></tr></thead><tbody></div>'
  7. //動態增加5個td,並且把data陣列的五個值賦給每個td
  8. // for(var i=0;i < data.length;i++){
  9. // tableData += "<td>" + data[i] + "</td>"
  10. // }
  11. for(var i = 0; i < data.length; i++) {
  12. tableData += "<tr class='bg-transparent'>";
  13. str = "";
  14. str2="";
  15. if(data[i].roleType==1){
  16. str = "第一層(集團)";
  17. }
  18. else if(data[i].roleType==2){
  19. str = "第二層(公司)";
  20. }
  21. else if(data[i].roleType==3){
  22. str = "第三層(廠區)";
  23. }
  24. else{
  25. str = "第四層(課)";
  26. }
  27. if(data[i].isAdmin==1){
  28. str2 = data[i].role_name;
  29. }
  30. else{
  31. str2 = "檢視者";
  32. }
  33. tableData += "<td>" + str + "</td>" +
  34. "<td>" + str2 + "</td>" +
  35. "<td>" + data[i].username + "</td>" +
  36. "<td></td>" +
  37. "<td><button class='btn btn-primary m-2' onclick=\"member_auth(\'"+data[i].username+"\');\"><i style='color:#fff;' class='fas fa-cog me-2'></i>設定</button></td>"+
  38. "<td></td>" +
  39. "<td></td>" ;
  40. tableData += "</tr>";
  41. }
  42. tableData+= "</tbody></table>";
  43. //現在tableData已經生成好了,把他賦值給上面的tbody
  44. $("#info_table").html(tableData);
  45. }
  46. // function member_auth(username){
  47. // $("#info_table").html("");
  48. // inner = "<p>請設定以下權限:</p><br>";
  49. // inner += "<table class='table table-borderless' id = \"change\"> <tr> <th>帳號</th><th>管理員</th><th>可用</th></tr>" +
  50. // " <tr><td >" + username + "</td><td><input class='form-check-input ms-3' type='checkbox' type=\"checkbox\" id=\"isAdmin\" >"+ " </td><td><input class='form-check-input ms-3' type='checkbox' type=\"checkbox\" id=\"isEnable\" ></td></tr></table>";
  51. // inner += "<br><button type='button' class='btn btn-primary me-3' onclick=\"put();\">確認 </button>" + "<button type='button' class='btn btn-primary me-3' onclick=\"start();\">取消 </button>"
  52. // $("#member_auth").html(inner);
  53. // console.log($("#isAdmin").prop('checked'));
  54. // }
  55. function member_auth(username){
  56. $("#info_table").html("");
  57. inner = "<p>請設定以下權限:</p>\
  58. <div style='font-size: 16px;' class='border rounded-3 py-3'>\
  59. <table class='table table-borderless' id = \"change\"> <tr> <th>帳號</th><th>管理員</th><th>可用</th></tr>\
  60. <tr>\
  61. <td >" + username + "</td>\
  62. <td><input class='form-check-input ms-3' type='checkbox' type=\"checkbox\" id=\"isAdmin\" >"+ " </td>\
  63. <td><input class='form-check-input ms-3' type='checkbox' type=\"checkbox\" id=\"isEnable\" ></td>\
  64. </tr>\
  65. </table>\
  66. </div>\
  67. <div class='mt-4 pb-3'>\
  68. <button type='button' class='btn btn-primary me-3' onclick=\"put();\">確認 </button>\
  69. <button type='button' class='btn btn-primary me-3' onclick=\"start();\">取消 </button>\
  70. </div>\
  71. ";
  72. $("#member_auth").html(inner);
  73. console.log($("#isAdmin").prop('checked'));
  74. }
  75. function put(){
  76. var xhr2 = new XMLHttpRequest();
  77. var url2 = "member/edit/?name=" + document.getElementById("change").rows[1].cells[0].innerHTML;
  78. if($("#isAdmin").prop('checked')){
  79. url2 += "&isAdmin=1" ;
  80. }
  81. else{
  82. url2 += "&isAdmin=0" ;
  83. }
  84. if($("#isEnable").prop('checked')){
  85. url2 += "&isEnable=1" ;
  86. }
  87. else{
  88. url2 += "&isEnable=0" ;
  89. }
  90. xhr2.open("GET",url2);
  91. xhr2.onload = function(){
  92. obj = JSON.parse(xhr2.responseText);
  93. alert(obj[0].msg);
  94. start();
  95. $("#member_auth").html("");
  96. //insertHTML();
  97. };
  98. xhr2.send();
  99. }
  100. //alert("response");
  101. start();
  102. // 之後透過 fetch 取得fastapi後端資料!
  103. function start(){
  104. var xhr = new XMLHttpRequest();
  105. var url = "member";
  106. obj = "";
  107. xhr.open("GET",url);
  108. xhr.onload = function(){
  109. obj = JSON.parse(xhr.responseText);
  110. data = obj ;
  111. creatTable();
  112. //insertHTML();
  113. };
  114. xhr.send();
  115. $("#member_auth").html("");
  116. console.log('Table data 寫入!');
  117. }