Yêu cầu các giá trị Tự động điền khi cần tải các bản ghi trên phần tử con dựa trên giá trị phần tử cha
Giá trị phần tử thay thế bằng các mục mới bất cứ khi nào giá trị phần tử gốc thay đổi
Trong hướng dẫn này, tôi chỉ cho bạn cách tự động điền danh sách thả xuống bằng jQuery AJAX trong Laravel
nội dung
1. Cấu hình cơ sở dữ liệu
Mở tệp
php artisan make:migration create_departments_table php artisan make:migration create_employees_table3
Chỉ định máy chủ, tên cơ sở dữ liệu, tên người dùng và mật khẩu
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=
2. Cấu trúc bảng
- Tạo bảng
php artisan make:migration create_departments_table php artisan make:migration create_employees_table
4 vàphp artisan make:migration create_departments_table php artisan make:migration create_employees_table
5 bằng cách sử dụng di chuyển và thêm một số bản ghi
php artisan make:migration create_departments_table php artisan make:migration create_employees_table
- Bây giờ, điều hướng đến thư mục
php artisan make:migration create_departments_table php artisan make:migration create_employees_table
6 từ thư mục gốc của dự án - Tìm một tệp PHP kết thúc bằng
php artisan make:migration create_departments_table php artisan make:migration create_employees_table
7 và mở nó - Xác định cấu trúc bảng trong phương thức
php artisan make:migration create_departments_table php artisan make:migration create_employees_table
8
- Tương tự, tìm một tệp PHP kết thúc bằng
php artisan make:migration create_departments_table php artisan make:migration create_employees_table
9 và mở nó - Xác định cấu trúc bảng trong phương thức
php artisan make:migration create_departments_table php artisan make:migration create_employees_table
8
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=1
- Chạy di chuyển -
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=2
- Bảng đã được tạo và tôi đã thêm một số bản ghi vào đó
3. Mô hình
Tạo 2 Mô hình –
Phòng ban –
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=3
- Chỉ định các thuộc tính Mô hình có thể gán hàng loạt – tên bằng cách sử dụng thuộc tính
public function up[] { Schema::create['departments', function [Blueprint $table] { $table->id[]; $table->string['name']; $table->timestamps[]; }]; }
1
Mã hoàn thành
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=5
Người lao động -
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=6
- Chỉ định các thuộc tính Mô hình có thể gán hàng loạt – tên người dùng, tên, email và bộ phận bằng cách sử dụng thuộc tính
public function up[] { Schema::create['departments', function [Blueprint $table] { $table->id[]; $table->string['name']; $table->timestamps[]; }]; }
1
Mã hoàn thành
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=8
4. Bộ điều khiển
Tạo bộ điều khiển _______83
php artisan make:migration create_departments_table php artisan make:migration create_employees_table0
Nhập mô hình
public function up[] { Schema::create['departments', function [Blueprint $table] { $table->id[]; $table->string['name']; $table->timestamps[]; }]; }4 và
public function up[] { Schema::create['departments', function [Blueprint $table] { $table->id[]; $table->string['name']; $table->timestamps[]; }]; }5
Tại đây, tạo hai phương thức –
- chỉ mục – Lấy tất cả các bản ghi từ bảng
php artisan make:migration create_departments_table php artisan make:migration create_employees_table
4 và chỉ định trongpublic function up[] { Schema::create['departments', function [Blueprint $table] { $table->id[]; $table->string['name']; $table->timestamps[]; }]; }
7
Tải
public function up[] { Schema::create['departments', function [Blueprint $table] { $table->id[]; $table->string['name']; $table->timestamps[]; }]; }8 xem và vượt qua
public function up[] { Schema::create['departments', function [Blueprint $table] { $table->id[]; $table->string['name']; $table->timestamps[]; }]; }9
- getEmployees – Phương thức này được sử dụng để xử lý yêu cầu AJAX. Lấy các bản ghi từ bảng
php artisan make:migration create_departments_table php artisan make:migration create_employees_table
5 trong đóDB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=
11
Trả lại Mảng
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=12 ở định dạng JSON
Mã hoàn thành
php artisan make:migration create_departments_table php artisan make:migration create_employees_table0
5. Tuyến đường
Mở tệp
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=13
Ở đây, xác định hai tuyến đường -
- /
- /getEmployees/{id} – Tuyến này sử dụng trong yêu cầu jQuery AJAX để lấy danh sách nhân viên
php artisan make:migration create_departments_table php artisan make:migration create_employees_table1
6. Xem
Tạo một tệp
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=14 mới trong thư mục
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=15
HTML –
Tạo hai phần tử
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=16 –
- Trong
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=
16 hiển thị tên bộ phận đầu tiên bằng cách lặp trênpublic function up[] { Schema::create['departments', function [Blueprint $table] { $table->id[]; $table->string['name']; $table->timestamps[]; }]; }
7 DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=
16 thứ hai được sử dụng để hiển thị tên nhân viên theo bộ phận đã chọn bằng jQuery
jquery –
Xác định sự kiện
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=20 trong danh sách thả xuống đầu tiên [
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=21]
Đọc giá trị đã chọn và làm trống phần tử
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=22 thứ hai, tất cả các tùy chọn ngoại trừ tùy chọn đầu tiên
Gửi yêu cầu AJAX GET tới
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=23 và cũng chuyển giá trị
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=24
Khi gọi lại thành công, hãy kiểm tra độ dài
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=25 nếu nó lớn hơn 0 thì lặp lại trên
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=25. Tạo các tùy chọn và thêm vào bộ chọn
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=27
Mã hoàn thành
php artisan make:migration create_departments_table php artisan make:migration create_employees_table2
7. Đầu ra
Xem đầu ra
8. Kết luận
Xác định sự kiện
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=20 trên phần tử cha và theo các bản ghi tìm nạp giá trị đã chọn cho phần tử con bằng jQuery AJAX
Bạn có thể sửa đổi mã nếu muốn thực hiện việc này với nhiều hơn hai menu thả xuống. Đối với điều này, bạn cần xác định sự kiện
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=tutorial DB_USERNAME=root DB_PASSWORD=20 phù hợp và tìm nạp các bản ghi bằng jQuery AJAX