如何根据 json 填充类别和子类别?

How to populate category and subcategory based on json?

我正在创建此管理表单,其中类别和子类别将下拉 selection,选项将来自给定的 Json (categories.json) 和创建产品时,将在 selecting 类别之后填充相应的子类别。 例如,如果我 select 一件 category_slug 男装,那么会出现 T 恤等子类别。 如何在 React 上做到这一点? json 文件

 {
    "categories": [
        {
            "id": "1",
            "name": "Men's Fashion",
            "category_slug": "men's-fashion",
            "category_type": "product",
            "child_categories": [
                {
                    "id": "2",
                    "name": "T-Shirt",
                    "category_slug": "t-shirt"
                },
                {
                    "id": "3",
                    "name": "Panjabi & Fatua",
                    "category_slug": "panjabi-&-fatua"
                },
                {
                    "id": "4",
                    "name": "Polo Shirts",
                    "category_slug": "polo-shirts"
                },
                {
                    "id": "5",
                    "name": "Jeans",
                    "category_slug": "jeans"
                },
                {
                    "id": "6",
                    "name": "Men's Sweaters ",
                    "category_slug": "men's-sweaters-"
                }
            ]
        },
        {
            "id": "7",
            "name": "Electronic Device ",
            "category_slug": "electronic-device-",
            "child_cat": [
                {
                    "id": "8",
                    "name": "Mobile ",
                    "category_slug": "mobile-12"
                },
                {
                    "id": "9",
                    "name": "Tablets",
                    "category_slug": "tablets"
                },
                {
                    "id": "10",
                    "name": "Laptops",
                    "category_slug": "laptops"
                },
                {
                    "id": "11",
                    "name": "Desktops",
                    "category_slug": "desktops"
                },
                {
                    "id": "12",
                    "name": "Gaming Consoles",
                    "category_slug": "gaming-consoles"
                },
                {
                    "id": "13",
                    "name": "Cameras",
                    "category_slug": "cameras"
                },
                {
                    "id": "14",
                    "name": "Security Cameras",
                    "category_slug": "security-cameras"
                },
                {
                    "id": "15",
                    "name": "Test",
                    "category_slug": "test"
                }
            ]
        },
        {
            "id": "16",
            "name": "Electronic Accessories ",
            "category_slug": "electronic-accessories-",
            "child_cat": [
                {
                    "id": "17",
                    "name": "Mobile Accessories",
                    "category_slug": "mobile-accessories"
                },
                {
                    "id": "18",
                    "name": "Wearable",
                    "category_slug": "wearable"
                },
                {
                    "id": "19",
                    "name": "Console Accessories",
                    "category_slug": "console-accessories"
                },
                {
                    "id": "20",
                    "name": "Camera Accessories",
                    "category_slug": "camera-accessories"
                },
                {
                    "id": "21",
                    "name": "Computer Accessories",
                    "category_slug": "computer-accessories"
                },
                {
                    "id": "22",
                    "name": "Storage",
                    "category_slug": "storage"
                },
                {
                    "id": "23",
                    "name": "Printers",
                    "category_slug": "printers"
                },
                {
                    "id": "24",
                    "name": "Computer Components",
                    "category_slug": "computer-components"
                },
                {
                    "id": "25",
                    "name": "Network Components",
                    "category_slug": "network-components"
                },
                {
                    "id": "26",
                    "name": "Software",
                    "category_slug": "software"
                }
            ]
        },
        {
            "id": "27",
            "name": "TV & Home Appliances",
            "category_slug": "tv-&-home-appliances"
        },
        {
            "id": "28",
            "name": "Health & Beauty",
            "category_slug": "health-&-beauty",
            "child_cat": [
                {
                    "id": "29",
                    "name": "Bath & Body",
                    "category_slug": "bath-&-body"
                },
                {
                    "id": "30",
                    "name": "Beauty Tools",
                    "category_slug": "beauty-tools"
                },
                {
                    "id": "31",
                    "name": "Fragrances",
                    "category_slug": "fragrances"
                },
                {
                    "id": "32",
                    "name": "Hair Care",
                    "category_slug": "hair-care"
                },
                {
                    "id": "33",
                    "name": "Makeup",
                    "category_slug": "makeup"
                },
                {
                    "id": "35",
                    "name": "Personal Care",
                    "category_slug": "personal-care"
                },
                {
                    "id": "36",
                    "name": "Skin Care",
                    "category_slug": "skin-care"
                },
                {
                    "id": "5f2a9798e7abc4290b6c6275",
                    "name": "Medical Supplies",
                    "category_slug": "medical-supplies"
                }
            ]
        }
    ]
}

表单。为此,我使用了 chakraUI。

export default function AddProductForm() {
  return (
    <Box display='flex' justifyContent='center' alignItems='center'>
      <Box
        w={[300, 400, 600]}
        border='1px'
        boxShadow='md'
        p={5}
        rounded='md'
        bg='white'
        borderColor='gray.400'
        mx='auto'
      >
        <FormControl id='product' isRequired>
          <FormLabel>Product Name</FormLabel>
          <Input type='text' />
          <FormLabel>Category</FormLabel>   
          <Select>
            <option>United Arab Emirates</option>
            <option>Nigeria</option>
          </Select>
          <FormLabel>Sub-category</FormLabel>
          <Select>
            <option>United Arab Emirates</option>
            <option>Nigeria</option>
          </Select>
          <FormLabel>Description</FormLabel>
          <Input type='text' />
          <FormLabel>Image</FormLabel>
          <Input type='text' />
          <FormLabel>Regular Price</FormLabel>
          <Input type='number' />
          <FormLabel>Sell Price</FormLabel>
          <Input type='number' />
          <FormLabel>Stock Quantity</FormLabel>
          <NumberInput max={5} min={1}>
            <NumberInputField />
            <NumberInputStepper>
              <NumberIncrementStepper />
              <NumberDecrementStepper />
            </NumberInputStepper>
          </NumberInput>
        </FormControl>
      </Box>
    </Box>
  );
}

使用 useState() 挂钩跟踪 parent select 的值。对于 child select 只需通过一些唯一键(在您的情况下为 'id' 或 'slug')找到 parent 并映射找到的 'child_categories' object 作为选项标签。

您的 JSON 密钥也不一致,请修正。

您的代码应如下所示:

export default function Example() {
  const [parent, setParent] = useState("");

  return (
    <div>
      {/* parent */}
      <select value={parent} onChange={(e) => setParent(e.target.value)}>
        <option value=""></option>
        {categories.map((category) => (
          <option value={category.id}>{category.name}</option>
        ))}
      </select>
      {/* child */}
      <select>
        <option value=""></option>
        {categories
          .find((x) => x.id === parent)
          ?.child_categories?.map((category) => (
            <option value={category.id}>{category.name}</option>
          ))}
      </select>
    </div>
  );
}