tool.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * constrcut 方法
  3. * 根据提供的 id, pid 和 children 将一个个节点构建成一棵或者多棵树
  4. * @param nodes 节点对象
  5. * @param config 配置对象
  6. */
  7. function construct (nodes, config) {
  8. var id = (config && config.id) || 'id'
  9. var pid = (config && config.pid) || 'pid'
  10. var children = (config && config.children) || 'children'
  11. var idMap = {}
  12. var jsonTree = []
  13. nodes.forEach(function (v) { idMap[v[id]] = v })
  14. nodes.forEach(function (v) {
  15. var parent = idMap[v[pid]]
  16. if (parent) {
  17. !parent[children] && (parent[children] = [])
  18. parent[children].push(v)
  19. } else {
  20. jsonTree.push(v)
  21. }
  22. })
  23. return jsonTree
  24. }
  25. function getQueryVariable (url, variable) {
  26. let query = url.split('?')
  27. let queryArr = []
  28. let value = ''
  29. if (query[1]) {
  30. queryArr = query[1].split('&')
  31. queryArr.forEach((e) => {
  32. let pair = e.split('=')
  33. if (pair[0] === variable) {
  34. value = pair[1]
  35. }
  36. })
  37. return value || false
  38. } else {
  39. return false
  40. }
  41. }
  42. let tool = {
  43. construct,
  44. getQueryVariable
  45. }
  46. export default tool