setting_unit_list_order.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const unitListOrder = (function () {
  10. let orderSetting = getLocalCache('zh-calc-unit-list-order');
  11. if (!orderSetting) orderSetting = 'create_time|up';
  12. const pinyin = new PinYinOrder();
  13. function CompareStr (x, y) {
  14. return pinyin.compareWord(x, y);
  15. }
  16. function reOrderUnits (orderStr, htmlClass = '#unit_list') {
  17. if (orderStr) {
  18. orderSetting = orderStr;
  19. setLocalCache('zh-calc-unit-list-order', orderStr);
  20. }
  21. const orders = orderSetting.split('|');
  22. if (orders[0] === 'name') {
  23. unitList.sort(function (a, b) {
  24. return orders[1] === 'up'
  25. ? CompareStr(a[orders[0]], b[orders[0]])
  26. : CompareStr(b[orders[0]], a[orders[0]]);
  27. });
  28. } else if (orders[0] === 'create_time') {
  29. unitList.sort(function (a, b){
  30. return orders[1] === 'up'
  31. ? Date.parse(a[orders[0]]) - Date.parse(b[orders[0]])
  32. : Date.parse(b[orders[0]]) - Date.parse(a[orders[0]]);
  33. })
  34. } else if (orders[0] === 'type') {
  35. unitList.sort(function (a, b){
  36. return orders[1] === 'up'
  37. ? a[orders[0]] - b[orders[0]]
  38. : b[orders[0]] - a[orders[0]];
  39. })
  40. }
  41. resetHeaderHtml();
  42. $(htmlClass).html(getUnitsHtml());
  43. setUnitRightHtml(unitList[0].id);
  44. }
  45. function getOrderButton(field) {
  46. const orders = orderSetting.split('|');
  47. const button = field === orders[0]
  48. ? (orders[1] === 'up'
  49. ? '<i class="fa fa-sort-amount-asc" aria-hidden="true" onclick="unitListOrder.reOrderUnits(\'' + field + '|down' + '\')"></i>'
  50. : '<i class="fa fa-sort-amount-desc" aria-hidden="true" onclick="unitListOrder.reOrderUnits(\'' + field + '|up' + '\')"></i>')
  51. : '<i class="fa fa-sort" aria-hidden="true" onclick="unitListOrder.reOrderUnits(\'' + field + '|up' + '\')"></i>';
  52. return '<a href="javascript:void(0)" class="btn btn-sm ml-1">' + button + '</a>';
  53. }
  54. function resetHeaderHtml() {
  55. $('#unit_header').html(`<tr>
  56. <th class="text-center">序号</th>
  57. <th class="text-center">单位名称 ${unitListOrder.getOrderButton('name')}</th>
  58. <th class="text-center">账号数</th>
  59. <th class="text-center">类型 ${unitListOrder.getOrderButton('type')}</th>
  60. <th class="text-center">备注</th></tr>`);
  61. }
  62. function getUnitsHtml() {
  63. let html = '';
  64. if (unitList.length > 0) {
  65. for (const [index, u] of unitList.entries()) {
  66. html += `<tr ${index === 0 ? 'class="table-warning"' : ''} data-id="${u.id}">
  67. <td class="text-center" width="50px">${index+1}</td>
  68. <td ><a href="javascript:void(0)">${u.name}</a></td>
  69. <td class="text-right" width="80px">${u.account_num}</td>
  70. <td width="100px">${accountGroup[u.type]}</td>
  71. <td width="150px">${u.basic ? u.basic : ''}</td>
  72. </tr>`;
  73. }
  74. }
  75. return html;
  76. }
  77. return { reOrderUnits, getOrderButton, resetHeaderHtml }
  78. })();