Implement search
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
from typing import List, Callable, Any
|
||||
|
||||
from PySide6.QtCore import QModelIndex, Qt, QAbstractTableModel, Slot
|
||||
from PySide6.QtCore import QModelIndex, Qt, QAbstractTableModel, Slot, Property, Signal
|
||||
from PySide6.QtQml import QJSValue
|
||||
from peewee import Select
|
||||
|
||||
@@ -19,10 +19,12 @@ COLUMN_NAMES = ["Vorname", "Nachname", "PLZ", "Ort"]
|
||||
|
||||
class ApplicantModel(QAbstractTableModel):
|
||||
_applicants: Select
|
||||
_search_query: str = ""
|
||||
search_query_changed = Signal(str)
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._applicants = Applicant.select_table_data()
|
||||
self._query_applicants()
|
||||
|
||||
def rowCount(self, /, parent=...):
|
||||
return len(self._applicants)
|
||||
@@ -36,6 +38,15 @@ class ApplicantModel(QAbstractTableModel):
|
||||
return COLUMNS[index.column()](applicant)
|
||||
return None
|
||||
|
||||
@Property(str, notify=search_query_changed)
|
||||
def searchQuery(self):
|
||||
return self._search_query
|
||||
|
||||
@searchQuery.setter
|
||||
def searchQuery(self, value: str):
|
||||
self._search_query = value
|
||||
self._query_applicants()
|
||||
|
||||
@Slot(int, result=dict)
|
||||
def applicant(self, row) -> dict:
|
||||
applicant = Applicant.get_by_id(self._applicants[row].id)
|
||||
@@ -68,9 +79,13 @@ class ApplicantModel(QAbstractTableModel):
|
||||
applicant.email_address = values.property("emailAddress").toString() or None
|
||||
applicant.salutation = values.property("salutation").toString() or None
|
||||
applicant.save(force_insert=True)
|
||||
self._applicants = Applicant.select_table_data()
|
||||
self._query_applicants()
|
||||
|
||||
def headerData(self, section: int, orientation: Qt.Orientation, role: int = Qt.ItemDataRole.DisplayRole):
|
||||
if role == Qt.ItemDataRole.DisplayRole:
|
||||
return COLUMN_NAMES[section]
|
||||
return None
|
||||
|
||||
def _query_applicants(self):
|
||||
self._applicants = Applicant.select_table_data(self._search_query)
|
||||
self.layoutChanged.emit()
|
||||
|
||||
Reference in New Issue
Block a user