uni-data-pickerview.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <template>
  2. <view class="uni-data-pickerview">
  3. <uni-combox v-if="showSearch" :candidates="candidates" placeholder="输入搜索内容" @input="handleInput"></uni-combox>
  4. <!-- <input placeholder="请输入搜索内容" style="border:1px solid #ddd;width:80%;margin:0 auto;border-radius:5px;padding:5px;" @input="handleInput"/> -->
  5. <scroll-view class="selected-area" scroll-x="true" scroll-y="false" :show-scrollbar="false">
  6. <view class="selected-list">
  7. <template v-for="(item,index) in selected">
  8. <view class="selected-item"
  9. :class="{'selected-item-active':index==selectedIndex, 'selected-item-text-overflow': ellipsis}"
  10. :key="index" v-if="item.text" @click="handleSelect(index)">
  11. <text class="">{{item.text}}</text>
  12. </view>
  13. </template>
  14. </view>
  15. </scroll-view>
  16. <view class="tab-c">
  17. <template v-for="(child, i) in dataList">
  18. <scroll-view class="list" :key="i" v-if="i==selectedIndex" :scroll-y="true">
  19. <view class="item" :class="{'is-disabled': !!item.disable}" v-for="(item, j) in child" :key="j"
  20. >
  21. <checkbox @click ="handleClick(item)" v-if="showSearch && i==0" :checked=" selected.length > j && item[map.value] == selected[j].value"/>
  22. <text @click="handleNodeClick(item, i, j)" class="item-text item-text-overflow">{{item[map.text]}}</text>
  23. <view class="check" v-if="i == 1 && selected.length > i && item[map.value] == selected[i].value"></view>
  24. </view>
  25. </scroll-view>
  26. </template>
  27. <view class="loading-cover" v-if="loading">
  28. <uni-load-more class="load-more" :contentText="loadMore" status="loading"></uni-load-more>
  29. </view>
  30. <view class="error-message" v-if="errorMessage">
  31. <text class="error-text">{{errorMessage}}</text>
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. import dataPicker from "./uni-data-picker.js"
  38. /**
  39. * DataPickerview
  40. * @description uni-data-pickerview
  41. * @tutorial https://ext.dcloud.net.cn/plugin?id=3796
  42. * @property {Array} localdata 本地数据,参考
  43. * @property {Boolean} step-searh = [true|false] 是否分布查询
  44. * @value true 启用分布查询,仅查询当前选中节点
  45. * @value false 关闭分布查询,一次查询出所有数据
  46. * @property {String|DBFieldString} self-field 分布查询当前字段名称
  47. * @property {String|DBFieldString} parent-field 分布查询父字段名称
  48. * @property {String|DBCollectionString} collection 表名
  49. * @property {String|DBFieldString} field 查询字段,多个字段用 `,` 分割
  50. * @property {String} orderby 排序字段及正序倒叙设置
  51. * @property {String|JQLString} where 查询条件
  52. */
  53. export default {
  54. name: 'UniDataPickerView',
  55. emits: ['nodeclick', 'change', 'datachange', 'update:modelValue'],
  56. mixins: [dataPicker],
  57. props: {
  58. managedMode: {
  59. type: Boolean,
  60. default: false
  61. },
  62. ellipsis: {
  63. type: Boolean,
  64. default: true
  65. },
  66. showSearch: {
  67. type: Boolean,
  68. default: false
  69. },
  70. },
  71. data() {
  72. return {
  73. candidates:[],
  74. nodeList:[],
  75. }
  76. },
  77. created() {
  78. if (this.managedMode) {
  79. return
  80. }
  81. this.$nextTick(() => {
  82. this.load()
  83. })
  84. },
  85. methods: {
  86. //搜索框选择员工
  87. handleInput(e){
  88. if(this.candidates.indexOf(e) != -1){
  89. let index = this.candidates.indexOf(e);
  90. let node = this.nodeList[index];
  91. console.log(this.candidates.indexOf(e));
  92. console.log(node);
  93. this._dispatchEvent()
  94. this.$emit('nodeclick', node)
  95. }
  96. //console.log(this.loadData());
  97. },
  98. //点击选择组别
  99. handleClick(item){
  100. let text = item.text;
  101. for(let i in this.nodeList){
  102. if(this.nodeList[i].parent_value == text){
  103. this.$emit('nodeclick', this.nodeList[i])
  104. }
  105. }
  106. this._dispatchEvent()
  107. },
  108. onPropsChange() {
  109. this._treeData = []
  110. this.selectedIndex = 0
  111. this.load()
  112. },
  113. load() {
  114. if (this.isLocaldata) {
  115. this.loadData()
  116. } else if (this.dataValue.length) {
  117. this.getTreePath((res) => {
  118. this.loadData()
  119. })
  120. }
  121. },
  122. handleSelect(index) {
  123. this.selectedIndex = index
  124. },
  125. handleNodeClick(item, i, j) {
  126. if (item.disable) {
  127. return
  128. }
  129. const node = this.dataList[i][j]
  130. const text = node[this.map.text]
  131. const value = node[this.map.value]
  132. if (i < this.selected.length - 1) {
  133. this.selected.splice(i, this.selected.length - i)
  134. this.selected.push({
  135. text,
  136. value
  137. })
  138. console.log(this.selected);
  139. } else if (i === this.selected.length - 1) {
  140. this.selected.splice(i, 1, {
  141. text,
  142. value
  143. })
  144. console.log(this.selected);
  145. }
  146. if (node.isleaf) {
  147. this.onSelectedChange(node, node.isleaf)
  148. return
  149. }
  150. const {
  151. isleaf,
  152. hasNodes
  153. } = this._updateBindData()
  154. if (!this._isTreeView() && !hasNodes) {
  155. this.onSelectedChange(node, true)
  156. return
  157. }
  158. if (this.isLocaldata && (!hasNodes || isleaf)) {
  159. this.onSelectedChange(node, true)
  160. return
  161. }
  162. if (!isleaf && !hasNodes) {
  163. this._loadNodeData((data) => {
  164. if (!data.length) {
  165. node.isleaf = true
  166. } else {
  167. this._treeData.push(...data)
  168. this._updateBindData(node)
  169. }
  170. this.onSelectedChange(node, node.isleaf)
  171. }, this._nodeWhere())
  172. return
  173. }
  174. this.onSelectedChange(node, false)
  175. },
  176. updateData(data) {
  177. //新增筛选用户功能ld
  178. let curTreeData=data.treeData;
  179. this.candidates = [];
  180. this.nodeList =[];
  181. for(let i in curTreeData){
  182. if(curTreeData[i].parent_value){
  183. this.candidates.push(curTreeData[i].text);
  184. this.nodeList.push(curTreeData[i])
  185. }
  186. }
  187. this._treeData = data.treeData
  188. this.selected = data.selected
  189. if (!this._treeData.length) {
  190. this.loadData()
  191. } else {
  192. //this.selected = data.selected
  193. this._updateBindData()
  194. }
  195. },
  196. onDataChange() {
  197. this.$emit('datachange')
  198. },
  199. onSelectedChange(node, isleaf) {
  200. if (isleaf) {
  201. this._dispatchEvent()
  202. }
  203. if (node) {
  204. this.$emit('nodeclick', node)
  205. }
  206. },
  207. _dispatchEvent() {
  208. this.$emit('change', this.selected.slice(0))
  209. }
  210. }
  211. }
  212. </script>
  213. <style >
  214. .uni-data-pickerview {
  215. flex: 1;
  216. /* #ifndef APP-NVUE */
  217. display: flex;
  218. /* #endif */
  219. flex-direction: column;
  220. overflow: hidden;
  221. height: 100%;
  222. }
  223. .error-text {
  224. color: #DD524D;
  225. }
  226. .loading-cover {
  227. position: absolute;
  228. left: 0;
  229. top: 0;
  230. right: 0;
  231. bottom: 0;
  232. background-color: rgba(255, 255, 255, .5);
  233. /* #ifndef APP-NVUE */
  234. display: flex;
  235. /* #endif */
  236. flex-direction: column;
  237. align-items: center;
  238. z-index: 1001;
  239. }
  240. .load-more {
  241. /* #ifndef APP-NVUE */
  242. margin: auto;
  243. /* #endif */
  244. }
  245. .error-message {
  246. background-color: #fff;
  247. position: absolute;
  248. left: 0;
  249. top: 0;
  250. right: 0;
  251. bottom: 0;
  252. padding: 15px;
  253. opacity: .9;
  254. z-index: 102;
  255. }
  256. /* #ifdef APP-NVUE */
  257. .selected-area {
  258. width: 750rpx;
  259. }
  260. /* #endif */
  261. .selected-list {
  262. /* #ifndef APP-NVUE */
  263. display: flex;
  264. /* #endif */
  265. flex-direction: row;
  266. flex-wrap: nowrap;
  267. padding: 0 5px;
  268. border-bottom: 1px solid #f8f8f8;
  269. }
  270. .selected-item {
  271. margin-left: 10px;
  272. margin-right: 10px;
  273. padding: 12px 0;
  274. text-align: center;
  275. /* #ifndef APP-NVUE */
  276. white-space: nowrap;
  277. /* #endif */
  278. }
  279. .selected-item-text-overflow {
  280. width: 168px;
  281. /* fix nvue */
  282. overflow: hidden;
  283. /* #ifndef APP-NVUE */
  284. width: 6em;
  285. white-space: nowrap;
  286. text-overflow: ellipsis;
  287. -o-text-overflow: ellipsis;
  288. /* #endif */
  289. }
  290. .selected-item-active {
  291. border-bottom: 2px solid #007aff;
  292. }
  293. .selected-item-text {
  294. color: #007aff;
  295. }
  296. .tab-c {
  297. position: relative;
  298. flex: 1;
  299. /* #ifndef APP-NVUE */
  300. display: flex;
  301. /* #endif */
  302. flex-direction: row;
  303. overflow: hidden;
  304. }
  305. .list {
  306. flex: 1;
  307. }
  308. .item {
  309. padding: 12px 15px;
  310. /* border-bottom: 1px solid #f0f0f0; */
  311. /* #ifndef APP-NVUE */
  312. display: flex;
  313. /* #endif */
  314. flex-direction: row;
  315. justify-content: space-between;
  316. }
  317. .is-disabled {
  318. opacity: .5;
  319. }
  320. .item-text {
  321. /* flex: 1; */
  322. color: #333333;
  323. }
  324. .item-text-overflow {
  325. width: 280px;
  326. /* fix nvue */
  327. overflow: hidden;
  328. /* #ifndef APP-NVUE */
  329. width: 20em;
  330. white-space: nowrap;
  331. text-overflow: ellipsis;
  332. -o-text-overflow: ellipsis;
  333. /* #endif */
  334. }
  335. .check {
  336. margin-right: 5px;
  337. border: 2px solid #007aff;
  338. border-left: 0;
  339. border-top: 0;
  340. height: 12px;
  341. width: 6px;
  342. transform-origin: center;
  343. /* #ifndef APP-NVUE */
  344. transition: all 0.3s;
  345. /* #endif */
  346. transform: rotate(45deg);
  347. }
  348. </style>