uni-data-picker.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. let fields = [this.field];
  109. if (this.parentField) {
  110. fields.push(`${this.parentField} as parent_value`);
  111. }
  112. return fields.join(',');
  113. }
  114. },
  115. created() {
  116. this.$watch(() => {
  117. var al = [];
  118. ['pageCurrent',
  119. 'pageSize',
  120. 'value',
  121. 'localdata',
  122. 'collection',
  123. 'action',
  124. 'field',
  125. 'orderby',
  126. 'where',
  127. 'getont',
  128. 'getcount',
  129. 'gettree'
  130. ].forEach(key => {
  131. al.push(this[key])
  132. });
  133. return al
  134. }, (newValue, oldValue) => {
  135. let needReset = false
  136. for (let i = 2; i < newValue.length; i++) {
  137. if (newValue[i] != oldValue[i]) {
  138. needReset = true
  139. break
  140. }
  141. }
  142. if (newValue[0] != oldValue[0]) {
  143. this.page.current = this.pageCurrent
  144. }
  145. this.page.size = this.pageSize
  146. this.onPropsChange()
  147. })
  148. this._treeData = []
  149. },
  150. methods: {
  151. onPropsChange() {
  152. this._treeData = []
  153. },
  154. getCommand(options = {}) {
  155. /* eslint-disable no-undef */
  156. let db = uniCloud.database()
  157. const action = options.action || this.action
  158. if (action) {
  159. db = db.action(action)
  160. }
  161. const collection = options.collection || this.collection
  162. db = db.collection(collection)
  163. const where = options.where || this.where
  164. if (!(!where || !Object.keys(where).length)) {
  165. db = db.where(where)
  166. }
  167. const field = options.field || this.field
  168. if (field) {
  169. db = db.field(field)
  170. }
  171. const orderby = options.orderby || this.orderby
  172. if (orderby) {
  173. db = db.orderBy(orderby)
  174. }
  175. const current = options.pageCurrent !== undefined ? options.pageCurrent : this.page.current
  176. const size = options.pageSize !== undefined ? options.pageSize : this.page.size
  177. const getCount = options.getcount !== undefined ? options.getcount : this.getcount
  178. const getTree = options.gettree !== undefined ? options.gettree : this.gettree
  179. const getOptions = {
  180. getCount,
  181. getTree
  182. }
  183. if (options.getTreePath) {
  184. getOptions.getTreePath = options.getTreePath
  185. }
  186. db = db.skip(size * (current - 1)).limit(size).get(getOptions)
  187. return db
  188. },
  189. getNodeData(callback) {
  190. if (this.loading) {
  191. return
  192. }
  193. this.loading = true
  194. this.getCommand({
  195. field: this.postField,
  196. where: this._pathWhere()
  197. }).then((res) => {
  198. this.loading = false
  199. this.selected = res.result.data
  200. callback && callback()
  201. }).catch((err) => {
  202. this.loading = false
  203. this.errorMessage = err
  204. })
  205. },
  206. getTreePath(callback) {
  207. if (this.loading) {
  208. return
  209. }
  210. this.loading = true
  211. this.getCommand({
  212. field: this.postField,
  213. getTreePath: {
  214. startWith: `${this.selfField}=='${this.value}'`
  215. }
  216. }).then((res) => {
  217. this.loading = false
  218. let treePath = []
  219. this._extractTreePath(res.result.data, treePath)
  220. this.selected = treePath
  221. callback && callback()
  222. }).catch((err) => {
  223. this.loading = false
  224. this.errorMessage = err
  225. })
  226. },
  227. loadData() {
  228. if (this.isLocaldata) {
  229. this._processLocalData()
  230. return
  231. }
  232. if (this.value.length) {
  233. this._loadNodeData((data) => {
  234. this._treeData = data
  235. this._updateBindData()
  236. this._updateSelected()
  237. })
  238. return
  239. }
  240. if (this.stepSearh) {
  241. this._loadNodeData((data) => {
  242. this._treeData = data
  243. this._updateBindData()
  244. })
  245. } else {
  246. this._loadAllData((data) => {
  247. this._treeData = []
  248. this._extractTree(data, this._treeData, null)
  249. this._updateBindData()
  250. })
  251. }
  252. },
  253. _loadAllData(callback) {
  254. if (this.loading) {
  255. return
  256. }
  257. this.loading = true
  258. this.getCommand({
  259. field: this.postField,
  260. gettree: true,
  261. startwith: `${this.selfField}=='${this.value}'`
  262. }).then((res) => {
  263. this.loading = false
  264. callback(res.result.data)
  265. this.onDataChange()
  266. }).catch((err) => {
  267. this.loading = false
  268. this.errorMessage = err
  269. })
  270. },
  271. _loadNodeData(callback, pw) {
  272. if (this.loading) {
  273. return
  274. }
  275. this.loading = true
  276. this.getCommand({
  277. field: this.postField,
  278. where: pw || this._postWhere(),
  279. pageSize: 500
  280. }).then((res) => {
  281. this.loading = false
  282. callback(res.result.data)
  283. this.onDataChange()
  284. }).catch((err) => {
  285. this.loading = false
  286. this.errorMessage = err
  287. })
  288. },
  289. _pathWhere() {
  290. let result = []
  291. let where_field = this._getParentNameByField();
  292. if (where_field) {
  293. result.push(`${where_field} == '${this.value}'`)
  294. }
  295. if (this.where) {
  296. return `(${this.where}) && (${result.join(' || ')})`
  297. }
  298. return result.join(' || ')
  299. },
  300. _postWhere() {
  301. let result = []
  302. let selected = this.selected
  303. let parentField = this.parentField
  304. if (parentField) {
  305. result.push(`${parentField} == null || ${parentField} == ""`)
  306. }
  307. if (selected.length) {
  308. for (var i = 0; i < selected.length - 1; i++) {
  309. result.push(`${parentField} == '${selected[i].value}'`)
  310. }
  311. }
  312. let where = []
  313. if (this.where) {
  314. where.push(`(${this.where})`)
  315. }
  316. if (result.length) {
  317. where.push(`(${result.join(' || ')})`)
  318. }
  319. return where.join(' && ')
  320. },
  321. _nodeWhere() {
  322. let result = []
  323. let selected = this.selected
  324. if (selected.length) {
  325. result.push(`${this.parentField} == '${selected[selected.length - 1].value}'`)
  326. }
  327. if (this.where) {
  328. return `(${this.where}) && (${result.join(' || ')})`
  329. }
  330. return result.join(' || ')
  331. },
  332. _getParentNameByField() {
  333. const fields = this.field.split(',');
  334. let where_field = null;
  335. for (let i = 0; i < fields.length; i++) {
  336. const items = fields[i].split('as');
  337. if (items.length < 2) {
  338. continue;
  339. }
  340. if (items[1].trim() === 'value') {
  341. where_field = items[0].trim();
  342. break;
  343. }
  344. }
  345. return where_field
  346. },
  347. _isTreeView() {
  348. return (this.parentField && this.selfField)
  349. },
  350. _updateSelected() {
  351. var dl = this.dataList
  352. var sl = this.selected
  353. for (var i = 0; i < sl.length; i++) {
  354. var value = sl[i].value
  355. var dl2 = dl[i]
  356. for (var j = 0; j < dl2.length; j++) {
  357. var item2 = dl2[j]
  358. if (item2.value === value) {
  359. sl[i].text = item2.text
  360. break
  361. }
  362. }
  363. }
  364. },
  365. _updateBindData(node) {
  366. const {
  367. dataList,
  368. hasNodes
  369. } = this._filterData(this._treeData, this.selected)
  370. let isleaf = this._stepSearh === false && !hasNodes
  371. if (node) {
  372. node.isleaf = isleaf
  373. }
  374. this.dataList = dataList
  375. this.selectedIndex = dataList.length - 1
  376. if (!isleaf && this.selected.length < dataList.length) {
  377. this.selected.push({
  378. value: null,
  379. text: "请选择"
  380. })
  381. }
  382. return {
  383. isleaf,
  384. hasNodes
  385. }
  386. },
  387. _filterData(data, paths) {
  388. let dataList = []
  389. let hasNodes = true
  390. dataList.push(data.filter((item) => {
  391. return item.parent_value === undefined
  392. }))
  393. for (let i = 0; i < paths.length; i++) {
  394. var value = paths[i].value
  395. var nodes = data.filter((item) => {
  396. return item.parent_value === value
  397. })
  398. if (nodes.length) {
  399. dataList.push(nodes)
  400. } else {
  401. hasNodes = false
  402. }
  403. }
  404. return {
  405. dataList,
  406. hasNodes
  407. }
  408. },
  409. _extractTree(nodes, result, parent_value) {
  410. let list = result || []
  411. for (let i = 0; i < nodes.length; i++) {
  412. let node = nodes[i]
  413. let child = {}
  414. for (let key in node) {
  415. if (key !== 'children') {
  416. child[key] = node[key]
  417. }
  418. }
  419. if (parent_value !== undefined) {
  420. child.parent_value = parent_value
  421. }
  422. result.push(child)
  423. let children = node.children
  424. if (children) {
  425. this._extractTree(children, result, node.value)
  426. }
  427. }
  428. },
  429. _extractTreePath(nodes, result) {
  430. let list = result || []
  431. for (let i = 0; i < nodes.length; i++) {
  432. let node = nodes[i]
  433. let child = {}
  434. for (let key in node) {
  435. if (key !== 'children') {
  436. child[key] = node[key]
  437. }
  438. }
  439. result.push(child)
  440. let children = node.children
  441. if (children) {
  442. this._extractTreePath(children, result)
  443. }
  444. }
  445. },
  446. _findNodePath(key, nodes, path = []) {
  447. for (let i = 0; i < nodes.length; i++) {
  448. let {
  449. value,
  450. text,
  451. children
  452. } = nodes[i]
  453. path.push({
  454. value,
  455. text
  456. })
  457. if (value === key) {
  458. return path
  459. }
  460. if (children) {
  461. const p = this._findNodePath(key, children, path)
  462. if (p.length) {
  463. return p
  464. }
  465. }
  466. path.pop()
  467. }
  468. return []
  469. },
  470. _processLocalData() {
  471. this._treeData = []
  472. this._extractTree(this.localdata, this._treeData)
  473. var inputValue = this.value
  474. if (inputValue === undefined) {
  475. return
  476. }
  477. if (Array.isArray(inputValue)) {
  478. inputValue = inputValue[inputValue.length - 1]
  479. if (typeof inputValue === 'object' && inputValue.value) {
  480. inputValue = inputValue.value
  481. }
  482. }
  483. this.selected = this._findNodePath(inputValue, this.localdata)
  484. }
  485. }
  486. }