Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-4-20250514-thinking-16k· AnthropicHugging Face LMArena Human Preference 140K

kendoDropDownList cascade 2 cấp , cấp 1 chạy trước thì cấp 2 mới chạy

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-sonnet-4-20250514’s answer.
  1. User

    kendoDropDownList cascade 2 cấp , cấp 1 chạy trước thì cấp 2 mới chạy

  2. Claude Assistant

    Để tạo cascading DropDownList 2 cấp với Kendo UI, bạn có thể làm như sau:

    HTML Structure

    <input id="category" />
    <input id="product" />
    

    JavaScript Implementation

    $(document).ready(function() {
        // DropDownList cấp 1 - Category
        $("#category").kendoDropDownList({
            dataTextField: "CategoryName",
            dataValueField: "CategoryID",
            dataSource: {
                transport: {
                    read: {
                        url: "/api/categories",
                        dataType: "json"
                    }
                }
            },
            optionLabel: "Chọn danh mục...",
            change: function() {
                var value = this.value();
                
                // Lấy reference của dropdown cấp 2
                var productDropdown = $("#product").data("kendoDropDownList");
                
                if (value) {
                    // Enable và load data cho dropdown cấp 2
                    productDropdown.enable(true);
                    productDropdown.dataSource.read();
                } else {
                    // Disable và clear dropdown cấp 2 khi không chọn gì
                    productDropdown.enable(false);
                    productDropdown.value("");
                }
            }
        });
        
        // DropDownList cấp 2 - Product
        $("#product").kendoDropDownList({
            dataTextField: "ProductName",
            dataValueField: "ProductID",
            dataSource: {
                transport: {
                    read: {
                        url: "/api/products",
                        dataType: "json",
                        data: function() {
                            // Gửi CategoryID đã chọn
                            var categoryDropdown = $("#category").data("kendoDropDownList");
                            return {
                                categoryId: categoryDropdown.value()
                            };
                        }
                    }
                }
            },
            optionLabel: "Chọn sản phẩm...",
            enable: false, // Disable mặc định
            autoBind: false // Không tự động load data khi khởi tạo
        });
    });
    

    Cách hoạt động:

    1. DropDownList cấp 1 load data ngay khi khởi tạo
    2. DropDownList cấp 2 bị disable và không load data cho đến khi cấp 1 được chọn
    3. Khi chọn giá trị ở cấp 1, event change sẽ:
      • Enable dropdown cấp 2
      • Trigger load data với tham số là giá trị đã chọn từ cấp 1
    4. Server API cần xử lý tham số categoryId để trả về danh sách products phù hợp

    Ví dụ với data tĩnh:

    // Data mẫu
    var categories = [
        { CategoryID: 1, CategoryName: "Điện tử" },
        { CategoryID: 2, CategoryName: "Thời trang" }
    ];
    
    var products = [
        { ProductID: 1, ProductName: "Laptop", CategoryID: 1 },
        { ProductID: 2, ProductName: "Điện thoại", CategoryID: 1 },
        { ProductID: 3, ProductName: "Áo thun", CategoryID: 2 },
        { ProductID: 4, ProductName: "Quần jean", CategoryID: 2 }
    ];
    
    // Setup dropdowns
    $("#category").kendoDropDownList({
        dataSource: categories,
        dataTextField: "CategoryName",
        dataValueField: "CategoryID",
        optionLabel: "Chọn danh mục...",
        change: onCategoryChange
    });
    
    $("#product").kendoDropDownList({
        dataSource: [],
        dataTextField: "ProductName",
        dataValueField: "ProductID",
        optionLabel: "Chọn sản phẩm...",
        enable: false
    });
    
    function onCategoryChange() {
        var categoryId = $("#category").val();
        var productDropdown = $("#product").data("kendoDropDownList");
        
        if (categoryId) {
            // Filter products theo category
            var filteredProducts = products.filter(function(p) {
                return p.CategoryID == categoryId;
            });
            
            productDropdown.setDataSource(filteredProducts);
            productDropdown.enable(true);
            productDropdown.value("");
        } else {
            productDropdown.setDataSource([]);
            productDropdown.enable(false);
        }
    }
    

    Như vậy dropdown cấp 2 sẽ chỉ hoạt động sau khi người dùng chọn giá trị ở dropdown cấp 1.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.