selectStaff.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var app = getApp()
  2. var that;
  3. Component({
  4. mixins: [],
  5. data: {
  6. all_user_list: {},
  7. isShow: false,
  8. keyVal: '',
  9. isAll: true,
  10. },
  11. props: {
  12. onConfirm: (data) => console.log(data),
  13. onClose: (data) => console.log(data),
  14. defaultUser: '',
  15. isAllSelect: true,//是否多选
  16. },
  17. didMount() {
  18. that = this;
  19. this.setData({ isShow: this.props.isShow, isAll: that.props.isAllSelect, })
  20. this.getData();
  21. },
  22. didUpdate() { },
  23. didUnmount() { },
  24. methods: {
  25. onReset() {
  26. this.setData({
  27. isVal: false,
  28. keyVal: ''
  29. })
  30. },
  31. //搜索
  32. bindKeyInput(e) {
  33. this.setData({
  34. keyVal: e.detail.value,
  35. isVal: e.detail.value ? true : false
  36. })
  37. },
  38. onConfirm() {
  39. this.props.onConfirm(this.data.selectUserId);
  40. this.props.onClose();
  41. },
  42. radioChange: function (e) {
  43. var selectUserId = this.data.selectUserId;
  44. var isExist = false;
  45. let item = e.target.dataset.item;
  46. let index = e.target.dataset.index;
  47. let checked = `userList[${index}].checked`;//获取当前控制选中的字段
  48. let elm = this.data.userList[index].checked;//当前的状态
  49. if (this.data.isAll) {//当可多选时
  50. if (e.detail.value) {//当有值时选中
  51. selectUserId.forEach((element, _this) => {
  52. if (element.id == item.id) {
  53. isExist = true;
  54. }
  55. });
  56. if (!isExist) {
  57. selectUserId.push(item);
  58. }
  59. } else {//取消选中
  60. selectUserId.forEach((element, index, _this) => {
  61. if (element.id == item.id) {
  62. _this.splice(index, 1);
  63. }
  64. });
  65. }
  66. } else {//单选择
  67. let userList=this.data.userList;
  68. userList.map(item => {
  69. item.checked = false;
  70. })
  71. selectUserId = [];
  72. if (e.detail.value) {//当有值时选中
  73. selectUserId.push(item);
  74. this.setData({
  75. userList: userList
  76. })
  77. } else {//取消选中
  78. // selectUserId = [];
  79. }
  80. }
  81. this.setData({
  82. [checked]: !elm,
  83. })
  84. this.setData({
  85. selectUserId: selectUserId
  86. })
  87. },
  88. getData(keywords) {
  89. app.$get("api/employee/list", { dept_id: 0, keywords: keywords }).then((res) => {
  90. var list = res.data.data.list;
  91. var arr = this.props.defaultUser;
  92. if (typeof (arr) == 'string') {
  93. arr = arr.split(',');
  94. }
  95. var ids = [], userList = list;
  96. userList.map(item => {
  97. item.checked = false;
  98. if (arr.length > 0) {
  99. arr.forEach(item2 => {
  100. if (item.id == item2) {
  101. item.checked = true;
  102. ids.push(item);
  103. }
  104. })
  105. }
  106. })
  107. this.setData({
  108. userList: userList,
  109. all_user_list: userList,
  110. selectUserId: ids.length > 0 ? ids : []
  111. })
  112. })
  113. },
  114. //关闭
  115. onClose(e) {
  116. this.props.onClose();
  117. }
  118. },
  119. });