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. postWhere() {
  111. let result = []
  112. let selected = this.selected
  113. result.push(`${this.parentField} == null`)
  114. if (selected.length) {
  115. for (var i = 0; i < selected.length - 1; i++) {
  116. result.push(`${this.parentField} == '${selected[i].value}'`)
  117. }
  118. }
  119. if (this.where) {
  120. return `(${this.where}) && (${result.join(' || ')})`
  121. }
  122. return result.join(' || ')
  123. },
  124. nodeWhere() {
  125. let result = []
  126. let selected = this.selected
  127. if (selected.length) {
  128. result.push(`${this.parentField} == '${selected[selected.length - 1].value}'`)
  129. }
  130. if (this.where) {
  131. return `(${this.where}) && (${result.join(' || ')})`
  132. }
  133. return result.join(' || ')
  134. }
  135. },
  136. created() {
  137. this.$watch(() => {
  138. var al = [];
  139. ['pageCurrent',
  140. 'pageSize',
  141. 'value',
  142. 'localdata',
  143. 'collection',
  144. 'action',
  145. 'field',
  146. 'orderby',
  147. 'where',
  148. 'getont',
  149. 'getcount',
  150. 'gettree'
  151. ].forEach(key => {
  152. al.push(this[key])
  153. });
  154. return al
  155. }, (newValue, oldValue) => {
  156. let needReset = false
  157. for (let i = 2; i < newValue.length; i++) {
  158. if (newValue[i] != oldValue[i]) {
  159. needReset = true
  160. break
  161. }
  162. }
  163. if (newValue[0] != oldValue[0]) {
  164. this.page.current = this.pageCurrent
  165. }
  166. this.page.size = this.pageSize
  167. this.onPropsChange()
  168. })
  169. this._treeData = []
  170. },
  171. methods: {
  172. onPropsChange() {
  173. this._treeData = []
  174. },
  175. getCommand(options = {}) {
  176. /* eslint-disable no-undef */
  177. let db = uniCloud.database()
  178. const action = options.action || this.action
  179. if (action) {
  180. db = db.action(action)
  181. }
  182. const collection = options.collection || this.collection
  183. db = db.collection(collection)
  184. const where = options.where || this.where
  185. if (!(!where || !Object.keys(where).length)) {
  186. db = db.where(where)
  187. }
  188. const field = options.field || this.field
  189. if (field) {
  190. db = db.field(field)
  191. }
  192. const orderby = options.orderby || this.orderby
  193. if (orderby) {
  194. db = db.orderBy(orderby)
  195. }
  196. const current = options.pageCurrent !== undefined ? options.pageCurrent : this.page.current
  197. const size = options.pageSize !== undefined ? options.pageSize : this.page.size
  198. const getCount = options.getcount !== undefined ? options.getcount : this.getcount
  199. const getTree = options.gettree !== undefined ? options.gettree : this.gettree
  200. const getOptions = {
  201. getCount,
  202. getTree
  203. }
  204. if (options.getTreePath) {
  205. getOptions.getTreePath = options.getTreePath
  206. }
  207. db = db.skip(size * (current - 1)).limit(size).get(getOptions)
  208. return db
  209. },
  210. getTreePath(callback) {
  211. if (this.loading) {
  212. return
  213. }
  214. this.loading = true
  215. this.getCommand({
  216. field: this.postField,
  217. getTreePath: {
  218. startWith: `${this.selfField}=='${this.value}'`
  219. }
  220. }).then((res) => {
  221. this.loading = false
  222. let treePath = []
  223. this._extractTreePath(res.result.data, treePath)
  224. this.selected = treePath
  225. callback && callback()
  226. }).catch((err) => {
  227. this.loading = false
  228. this.errorMessage = err
  229. })
  230. },
  231. loadData() {
  232. if (this.isLocaldata) {
  233. this._processLocalData()
  234. return
  235. }
  236. if (this.value.length) {
  237. this._loadNodeData((data) => {
  238. this._treeData = data
  239. this._updateBindData()
  240. this._updateSelected()
  241. })
  242. return
  243. }
  244. if (this.stepSearh) {
  245. this._loadNodeData((data) => {
  246. this._treeData = data
  247. this._updateBindData()
  248. })
  249. } else {
  250. this._loadAllData((data) => {
  251. this._treeData = []
  252. this._extractTree(data, this._treeData, null)
  253. this._updateBindData()
  254. })
  255. }
  256. },
  257. _loadAllData(callback) {
  258. if (this.loading) {
  259. return
  260. }
  261. this.loading = true
  262. this.getCommand({
  263. field: this.postField,
  264. gettree: true,
  265. startwith: `${this.selfField}=='${this.value}'`
  266. }).then((res) => {
  267. this.loading = false
  268. callback(res.result.data)
  269. this.onDataChange()
  270. }).catch((err) => {
  271. this.loading = false
  272. this.errorMessage = err
  273. })
  274. },
  275. _loadNodeData(callback, pw) {
  276. if (this.loading) {
  277. return
  278. }
  279. this.loading = true
  280. this.getCommand({
  281. field: this.postField,
  282. where: pw || this.postWhere,
  283. pageSize: 500
  284. }).then((res) => {
  285. this.loading = false
  286. callback(res.result.data)
  287. this.onDataChange()
  288. }).catch((err) => {
  289. this.loading = false
  290. this.errorMessage = err
  291. })
  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. }