Skip to content

Instantly share code, notes, and snippets.

@KasperJack
Created December 16, 2024 09:35
Show Gist options
  • Select an option

  • Save KasperJack/2fbed70054efd5d64ab8500a5514fbea to your computer and use it in GitHub Desktop.

Select an option

Save KasperJack/2fbed70054efd5d64ab8500a5514fbea to your computer and use it in GitHub Desktop.
self.load_students_to_table()
self.search_bar.textChanged.connect(self.filter_students_table)
self.class_combo_box.currentTextChanged.connect(self.filter_students_table)
self.class_combo_box.addItem("All Classes") # Default option to show all students
self.class_combo_box.addItems(get_classes()) # Populate with class names
def load_students_to_table(self):
"""Load all students into the table."""
self.students = get_students_info() # Fetch the full dataset
#self.display_students(self.students)
self.filter_students_table()
def display_students(self, students):
"""Display a given list of students in the table."""
self.students_table.setRowCount(len(students))
self.students_table.setColumnCount(9)
self.students_table.setHorizontalHeaderLabels([
"Student ID", "Full Name", "Gender", "Grade",
"Class Name", "Birth Date", "Address", "Phone", "Email"
])
for row_idx, student in enumerate(students):
for col_idx, data in enumerate(student):
item = QTableWidgetItem(str(data) if data is not None else "")
self.students_table.setItem(row_idx, col_idx, item)
def filter_students_table(self):
"""Filter the table based on the search input and selected class."""
search_text = self.search_bar.text().strip().lower()
selected_class = self.class_combo_box.currentText()
# Filter based on class and search text
filtered_students = self.students
# If a specific class is selected, filter by class
if selected_class != "All Classes":
filtered_students = [
student for student in filtered_students if student[4] == selected_class
]
# Further filter by search text
if search_text:
filtered_students = [
student for student in filtered_students if search_text in student[1].lower()
]
self.display_students(filtered_students)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment