You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用ReactiveSearch UI Kit创建层级复选框菜单?

Solution for Hierarchical Checkbox Menu with ReactiveSearch UI Kit

Alright, let's build that hierarchical checkbox menu you need—one that pulls data from your Elasticsearch index, only shows cities when their parent country is selected, and lets users pick cities across multiple countries. Here's how to make it work:

1. First, Ensure Your Elasticsearch Index is Set Up Correctly

For the hierarchical aggregations to work smoothly, your country and cities fields should be of keyword type (since we need to do term-based aggregations). If you haven't set up the mapping yet, here's a sample:

{
  "mappings": {
    "properties": {
      "country": { "type": "keyword" },
      "cities": { "type": "keyword" }
    }
  }
}

2. Use ReactiveSearch's HierarchicalMenu Component

The HierarchicalMenu component is perfect for this use case—it supports checkbox selections and can handle parent-child field relationships out of the box. Here's a complete React example:

import { HierarchicalMenu, ReactiveBase } from '@appbaseio/reactivesearch';

function CountryCityHierarchy() {
  return (
    <ReactiveBase
      app="your-es-index-name" // Replace with your index name
      url="https://your-es-endpoint.com" // Replace with your ES endpoint
      // Add credentials if your ES instance requires authentication
      // credentials="username:password"
    >
      <HierarchicalMenu
        componentId="CountryCitySelector"
        field="country"
        subField="cities"
        showCheckbox={true} // Enables checkbox-style selection
        expandAll={false} // Starts with all countries collapsed (default)
        showCount={false} // Optional: Hide result counts if you don't need them
        // Custom render function to get that traditional checkbox look
        renderItem={(item, isSelected, isExpanded, handleClick) => (
          <div className="menu-item" onClick={handleClick}>
            <input
              type="checkbox"
              checked={isSelected}
              onChange={() => {}} // Let handleClick manage selection state
            />
            <span>{item.label}</span>
            {/* Add expand/collapse arrow for countries with cities */}
            {item.hasChildren && (
              <span className="arrow">{isExpanded ? '▼' : '▶'}</span>
            )}
          </div>
        )}
      />
    </ReactiveBase>
  );
}

export default CountryCityHierarchy;

3. Key Configurations Explained

  • showCheckbox={true}: This turns the menu items into checkbox controls, matching your "traditional checkbox style" requirement.
  • field="country" & subField="cities": These tell the component to first aggregate by country, then load the corresponding cities for any selected country.
  • renderItem: This lets you customize the look of each menu item—here we've added a checkbox and expand/collapse arrow for a classic hierarchical feel. You can tweak the CSS for .menu-item and .arrow to match your app's styling.

4. How It Works

  • By default, only country checkboxes are visible.
  • When you select a country, its corresponding cities will expand below it, ready to be selected.
  • You can select multiple countries at once, and each selected country's cities will stay expanded for cross-country city selection.

Quick Styling Tip

Add some basic CSS to make it look polished:

.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  cursor: pointer;
}

.arrow {
  margin-left: auto;
  font-size: 12px;
}

内容的提问来源于stack exchange,提问作者Manel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:13:45