temp.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. let toRegexp = (str) => {
  2. str = str.replace(/ +/g, " ").trim();
  3. str = "/" + str.split(" ").join("|") + "/";
  4. return str;
  5. };
  6. let toQuery = (str, fields = ["POLE1", "POLE2"]) => {
  7. str = toRegexp(str);
  8. let arr = fields.map((s) => {
  9. console.log(s, typeof s);
  10. return { [s]: str };
  11. });
  12. return { $or: arr };
  13. };
  14. toQuery(" klk lk nnnj klkm 555 ");
  15. async function categories(cats, parentId = null) {
  16. let result = await gql(
  17. `query categories($query:String){
  18. CategoryFind(query: $query){
  19. _id
  20. name,
  21. goods{
  22. name
  23. },
  24. parent{
  25. name
  26. }
  27. }
  28. }`,
  29. { query: JSON.stringify([{ "parent._id": parentId }]) }
  30. );
  31. if (result.errors) return;
  32. //cats = document.getElementById("cats")
  33. for (let { name, _id } of result.data.CategoryFind) {
  34. let li = document.createElement("li");
  35. li.innerText = name;
  36. let loaded;
  37. li.onclick = () => {
  38. if (!loaded) {
  39. categories(li, _id);
  40. loaded = true;
  41. }
  42. };
  43. cats.append(li);
  44. }
  45. }
  46. categories();
  47. <li class="nav-item dropdown">
  48. <a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  49. Dropdown
  50. </a>
  51. <ul id="cats" class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDarkDropdownMenuLink">
  52. <!-- <li><a class="dropdown-item" href="#">Action</a></li> -->
  53. <!-- <li><a class="dropdown-item" href="#">Another action</a></li> -->
  54. <!-- <li><a class="dropdown-item" href="#">Something else here</a></li> -->
  55. </ul>
  56. </li>