kendoDropDownList cascade 2 cấp , cấp 1 chạy trước thì cấp 2 mới chạy
- User
kendoDropDownList cascade 2 cấp , cấp 1 chạy trước thì cấp 2 mới chạy
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:
- DropDownList cấp 1 load data ngay khi khởi tạo
- DropDownList cấp 2 bị disable và không load data cho đến khi cấp 1 được chọn
- Khi chọn giá trị ở cấp 1, event
changesẽ:- Enable dropdown cấp 2
- Trigger load data với tham số là giá trị đã chọn từ cấp 1
- 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.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.