uni-data-picker.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. export default {
  2. props: {
  3. localdata: {
  4. type: [Array, Object],
  5. default () {
  6. return []
  7. }
  8. },
  9. collection: {
  10. type: String,
  11. default: ''
  12. },
  13. action: {
  14. type: String,
  15. default: ''
  16. },
  17. field: {
  18. type: String,
  19. default: ''
  20. },
  21. orderby: {
  22. type: String,
  23. default: ''
  24. },
  25. where: {
  26. type: [String, Object],
  27. default: ''
  28. },
  29. pageData: {
  30. type: String,
  31. default: 'add'
  32. },
  33. pageCurrent: {
  34. type: Number,
  35. default: 1
  36. },
  37. pageSize: {
  38. type: Number,
  39. default: 20
  40. },
  41. getcount: {
  42. type: [Boolean, String],
  43. default: false
  44. },
  45. getone: {
  46. type: [Boolean, String],
  47. default: false
  48. },
  49. gettree: {
  50. type: [Boolean, String],
  51. default: false
  52. },
  53. manual: {
  54. type: Boolean,
  55. default: false
  56. },
  57. value: {
  58. type: [Array, String, Number],
  59. default () {
  60. return []
  61. }
  62. },
  63. preload: {
  64. type: Boolean,
  65. default: false
  66. },
  67. stepSearh: {
  68. type: Boolean,
  69. default: true
  70. },
  71. selfField: {
  72. type: String,
  73. default: ''
  74. },
  75. parentField: {
  76. type: String,
  77. default: ''
  78. },
  79. multiple: {
  80. type: Boolean,
  81. default: false
  82. }
  83. },
  84. data() {
  85. return {
  86. loading: false,
  87. errorMessage: '',
  88. loadMore: {
  89. contentdown: '',
  90. contentrefresh: '',
  91. contentnomore: ''
  92. },
  93. dataList: [],
  94. selected: [],
  95. selectedIndex: 0,
  96. page: {
  97. current: this.pageCurrent,
  98. size: this.pageSize,
  99. count: 0
  100. }
  101. }
  102. },
  103. computed: {
  104. isLocaldata() {
  105. return this.localdata.length > 0
  106. },
  107. postField() {
  108. return `${this.field}, ${this.parentField} as parent_value`
  109. }
  110. },
  111. created() {
  112. this.$watch(() => {
  113. var al = [];
  114. ['pageCurrent',
  115. 'pageSize',
  116. 'value',
  117. 'localdata',
  118. 'collection',
  119. 'action',
  120. 'field',
  121. 'orderby',
  122. 'where',
  123. 'getont',
  124. 'getcount',
  125. 'gettree'
  126. ].forEach(key => {
  127. al.push(this[key])
  128. });
  129. return al
  130. }, (newValue, oldValue) => {
  131. let needReset = false
  132. for (let i = 2; i < newValue.length; i++) {
  133. if (newValue[i] != oldValue[i]) {
  134. needReset = true
  135. break
  136. }
  137. }
  138. if (newValue[0] != oldValue[0]) {
  139. this.page.current = this.pageCurrent
  140. }
  141. this.page.size = this.pageSize
  142. this.onPropsChange()
  143. })
  144. this._treeData = []
  145. },
  146. methods: {
  147. onPropsChange() {
  148. this._treeData = []
  149. },
  150. getCommand(options = {}) {
  151. /* eslint-disable no-undef */
  152. let db = uniCloud.database()
  153. const action = options.action || this.action
  154. if (action) {
  155. db = db.action(action)
  156. }
  157. const collection = options.collection || this.collection
  158. db = db.collection(collection)
  159. const where = options.where || this.where
  160. if (!(!where || !Object.keys(where).length)) {
  161. db = db.where(where)
  162. }
  163. const field = options.field || this.field
  164. if (field) {
  165. db = db.field(field)
  166. }
  167. const orderby = options.orderby || this.orderby
  168. if (orderby) {
  169. db = db.orderBy(orderby)
  170. }
  171. const current = options.pageCurrent !== undefined ? options.pageCurrent : this.page.current
  172. const size = options.pageSize !== undefined ? options.pageSize : this.page.size
  173. const getCount = options.getcount !== undefined ? options.getcount : this.getcount
  174. const getTree = options.gettree !== undefined ? options.gettree : this.gettree
  175. const getOptions = {
  176. getCount,
  177. getTree
  178. }
  179. if (options.getTreePath) {
  180. getOptions.getTreePath = options.getTreePath
  181. }
  182. db = db.skip(size * (current - 1)).limit(size).get(getOptions)
  183. return db
  184. },
  185. getTreePath(callback) {
  186. if (this.loading) {
  187. return
  188. }
  189. this.loading = true
  190. this.getCommand({
  191. field: this.postField,
  192. getTreePath: {
  193. startWith: `${this.selfField}=='${this.value}'`
  194. }
  195. }).then((res) => {
  196. this.loading = false
  197. let treePath = []
  198. this._extractTreePath(res.result.data, treePath)
  199. this.selected = treePath
  200. callback && callback()
  201. }).catch((err) => {
  202. this.loading = false
  203. this.errorMessage = err
  204. })
  205. },
  206. loadData() {
  207. if (this.isLocaldata) {
  208. this._processLocalData()
  209. return
  210. }
  211. if (this.value.length) {
  212. this._loadNodeData((data) => {
  213. this._treeData = data
  214. this._updateBindData()
  215. this._updateSelected()
  216. })
  217. return
  218. }
  219. if (this.stepSearh) {
  220. this._loadNodeData((data) => {
  221. this._treeData = data
  222. this._updateBindData()
  223. })
  224. } else {
  225. this._loadAllData((data) => {
  226. this._treeData = []
  227. this._extractTree(data, this._treeData, null)
  228. this._updateBindData()
  229. })
  230. }
  231. },
  232. _loadAllData(callback) {
  233. if (this.loading) {
  234. return
  235. }
  236. this.loading = true
  237. this.getCommand({
  238. field: this.postField,
  239. gettree: true,
  240. startwith: `${this.selfField}=='${this.value}'`
  241. }).then((res) => {
  242. this.loading = false
  243. callback(res.result.data)
  244. this.onDataChange()
  245. }).catch((err) => {
  246. this.loading = false
  247. this.errorMessage = err
  248. })
  249. },
  250. _loadNodeData(callback, pw) {
  251. if (this.loading) {
  252. return
  253. }
  254. this.loading = true
  255. this.getCommand({
  256. field: this.postField,
  257. where: pw || this._postWhere(),
  258. pageSize: 500
  259. }).then((res) => {
  260. this.loading = false
  261. callback(res.result.data)
  262. this.onDataChange()
  263. }).catch((err) => {
  264. this.loading = false
  265. this.errorMessage = err
  266. })
  267. },
  268. _postWhere() {
  269. let result = []
  270. let selected = this.selected
  271. result.push(`${this.parentField} == null`)
  272. if (selected.length) {
  273. for (var i = 0; i < selected.length - 1; i++) {
  274. result.push(`${this.parentField} == '${selected[i].value}'`)
  275. }
  276. }
  277. if (this.where) {
  278. return `(${this.where}) && (${result.join(' || ')})`
  279. }
  280. return result.join(' || ')
  281. },
  282. _nodeWhere() {
  283. let result = []
  284. let selected = this.selected
  285. if (selected.length) {
  286. result.push(`${this.parentField} == '${selected[selected.length - 1].value}'`)
  287. }
  288. if (this.where) {
  289. return `(${this.where}) && (${result.join(' || ')})`
  290. }
  291. return result.join(' || ')
  292. },
  293. _updateSelected() {
  294. var dl = this.dataList
  295. var sl = this.selected
  296. for (var i = 0; i < sl.length; i++) {
  297. var value = sl[i].value
  298. var dl2 = dl[i]
  299. for (var j = 0; j < dl2.length; j++) {
  300. var item2 = dl2[j]
  301. if (item2.value === value) {
  302. sl[i].text = item2.text
  303. break
  304. }
  305. }
  306. }
  307. },
  308. _updateBindData(node) {
  309. const {
  310. dataList,
  311. hasNodes
  312. } = this._filterData(this._treeData, this.selected)
  313. let isleaf = this._stepSearh === false && !hasNodes
  314. if (node) {
  315. node.isleaf = isleaf
  316. }
  317. this.dataList = dataList
  318. this.selectedIndex = dataList.length - 1
  319. if (!isleaf && this.selected.length < dataList.length) {
  320. this.selected.push({
  321. value: null,
  322. text: "请选择"
  323. })
  324. }
  325. return {
  326. isleaf,
  327. hasNodes
  328. }
  329. },
  330. _filterData(data, paths) {
  331. let dataList = []
  332. let hasNodes = true
  333. dataList.push(data.filter((item) => {
  334. return item.parent_value === undefined
  335. }))
  336. for (let i = 0; i < paths.length; i++) {
  337. var value = paths[i].value
  338. var nodes = data.filter((item) => {
  339. return item.parent_value === value
  340. })
  341. if (nodes.length) {
  342. dataList.push(nodes)
  343. } else {
  344. hasNodes = false
  345. }
  346. }
  347. return {
  348. dataList,
  349. hasNodes
  350. }
  351. },
  352. _extractTree(nodes, result, parent_value) {
  353. let list = result || []
  354. for (let i = 0; i < nodes.length; i++) {
  355. let node = nodes[i]
  356. let child = {}
  357. for (let key in node) {
  358. if (key !== 'children') {
  359. child[key] = node[key]
  360. }
  361. }
  362. if (parent_value !== null) {
  363. child.parent_value = parent_value
  364. }
  365. result.push(child)
  366. let children = node.children
  367. if (children) {
  368. this._extractTree(children, result, node.value)
  369. }
  370. }
  371. },
  372. _extractTreePath(nodes, result) {
  373. let list = result || []
  374. for (let i = 0; i < nodes.length; i++) {
  375. let node = nodes[i]
  376. let child = {}
  377. for (let key in node) {
  378. if (key !== 'children') {
  379. child[key] = node[key]
  380. }
  381. }
  382. result.push(child)
  383. let children = node.children
  384. if (children) {
  385. this._extractTreePath(children, result)
  386. }
  387. }
  388. },
  389. _findNodePath(key, nodes, path = []) {
  390. for (let i = 0; i < nodes.length; i++) {
  391. let {
  392. value,
  393. text,
  394. children
  395. } = nodes[i]
  396. path.push({
  397. value,
  398. text
  399. })
  400. if (value === key) {
  401. return path
  402. }
  403. if (children) {
  404. const p = this._findNodePath(key, children, path)
  405. if (p.length) {
  406. return p
  407. }
  408. }
  409. path.pop()
  410. }
  411. return []
  412. },
  413. _processLocalData() {
  414. this._treeData = []
  415. this._extractTree(this.localdata, this._treeData)
  416. var inputValue = this.value
  417. if (inputValue === undefined) {
  418. return
  419. }
  420. if (Array.isArray(inputValue)) {
  421. inputValue = inputValue[inputValue.length - 1]
  422. if (typeof inputValue === 'object' && inputValue.value) {
  423. inputValue = inputValue.value
  424. }
  425. }
  426. this.selected = this._findNodePath(inputValue, this.localdata)
  427. }
  428. }
  429. }