mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2025-08-14 18:58:13 +08:00
add row_attributes and cell_attributes
This commit is contained in:
@ -26,10 +26,18 @@ General `a` tag menu.
|
|||||||
|
|
||||||
`target` attr of `a` tag, default is `_self`.
|
`target` attr of `a` tag, default is `_self`.
|
||||||
|
|
||||||
|
## Field
|
||||||
|
|
||||||
|
## Action
|
||||||
|
|
||||||
## Model
|
## Model
|
||||||
|
|
||||||
### Field
|
### get_actions
|
||||||
|
|
||||||
### Action
|
### get_bulk_actions
|
||||||
|
|
||||||
|
### row_attributes
|
||||||
|
|
||||||
|
### cell_attributes
|
||||||
|
|
||||||
## Dropdown
|
## Dropdown
|
||||||
|
@ -28,7 +28,10 @@ class AdminResource(Model):
|
|||||||
page_title = "admin model"
|
page_title = "admin model"
|
||||||
filters = [
|
filters = [
|
||||||
filters.Search(
|
filters.Search(
|
||||||
name="username", label="Name", search_mode="contains", placeholder="Search for username"
|
name="username",
|
||||||
|
label="Name",
|
||||||
|
search_mode="contains",
|
||||||
|
placeholder="Search for username",
|
||||||
),
|
),
|
||||||
filters.Date(name="created_at", label="CreatedAt"),
|
filters.Date(name="created_at", label="CreatedAt"),
|
||||||
]
|
]
|
||||||
@ -52,6 +55,11 @@ class AdminResource(Model):
|
|||||||
]
|
]
|
||||||
can_create = False
|
can_create = False
|
||||||
|
|
||||||
|
def cell_attributes(self, obj: dict, field: Field) -> dict:
|
||||||
|
if field.name == "id":
|
||||||
|
return {"class": "bg-danger"}
|
||||||
|
return super().cell_attributes(obj, field)
|
||||||
|
|
||||||
def get_actions(self) -> List[Action]:
|
def get_actions(self) -> List[Action]:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@ -111,6 +119,11 @@ class ConfigResource(Model):
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def row_attributes(self, obj: dict) -> dict:
|
||||||
|
if obj.get("status") == enums.Status.on:
|
||||||
|
return {"class": "bg-green text-white"}
|
||||||
|
return super(ConfigResource, self).row_attributes(obj)
|
||||||
|
|
||||||
|
|
||||||
@app.register
|
@app.register
|
||||||
class GithubLink(Link):
|
class GithubLink(Link):
|
||||||
|
@ -68,7 +68,8 @@ class FastAPIAdmin(FastAPI):
|
|||||||
self.resources.append(resource)
|
self.resources.append(resource)
|
||||||
|
|
||||||
def get_model_resource(self, model: Type[Model]):
|
def get_model_resource(self, model: Type[Model]):
|
||||||
return self.model_resources[model]()
|
r = self.model_resources.get(model)
|
||||||
|
return r() if r else None
|
||||||
|
|
||||||
|
|
||||||
app = FastAPIAdmin(
|
app = FastAPIAdmin(
|
||||||
|
@ -21,6 +21,8 @@ def get_model(resource: Optional[str] = Path(...)):
|
|||||||
|
|
||||||
def get_model_resource(request: Request, model=Depends(get_model)):
|
def get_model_resource(request: Request, model=Depends(get_model)):
|
||||||
model_resource = request.app.get_model_resource(model) # type:Model
|
model_resource = request.app.get_model_resource(model) # type:Model
|
||||||
|
if not model_resource:
|
||||||
|
raise HTTPException(status_code=HTTP_404_NOT_FOUND)
|
||||||
actions = model_resource.get_actions()
|
actions = model_resource.get_actions()
|
||||||
bulk_actions = model_resource.get_bulk_actions()
|
bulk_actions = model_resource.get_bulk_actions()
|
||||||
model_resource.actions = actions
|
model_resource.actions = actions
|
||||||
|
@ -70,6 +70,12 @@ class Model(Resource):
|
|||||||
can_create: bool = True
|
can_create: bool = True
|
||||||
enctype = "application/x-www-form-urlencoded"
|
enctype = "application/x-www-form-urlencoded"
|
||||||
|
|
||||||
|
def row_attributes(self, obj: dict) -> dict:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def cell_attributes(self, obj: dict, field: Field) -> dict:
|
||||||
|
return {}
|
||||||
|
|
||||||
def get_actions(self) -> List[Action]:
|
def get_actions(self) -> List[Action]:
|
||||||
return [
|
return [
|
||||||
Action(label=_("update"), icon="ti ti-edit", name="update", ajax=False),
|
Action(label=_("update"), icon="ti ti-edit", name="update", ajax=False),
|
||||||
@ -78,7 +84,12 @@ class Model(Resource):
|
|||||||
|
|
||||||
def get_bulk_actions(self) -> List[Action]:
|
def get_bulk_actions(self) -> List[Action]:
|
||||||
return [
|
return [
|
||||||
Action(label=_("delete_selected"), icon="ti ti-trash", name="delete", method="DELETE"),
|
Action(
|
||||||
|
label=_("delete_selected"),
|
||||||
|
icon="ti ti-trash",
|
||||||
|
name="delete",
|
||||||
|
method="DELETE",
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -37,14 +37,15 @@ async def list_view(
|
|||||||
else:
|
else:
|
||||||
page_size = model_resource.page_size
|
page_size = model_resource.page_size
|
||||||
qs = qs.offset((page_num - 1) * page_size)
|
qs = qs.offset((page_num - 1) * page_size)
|
||||||
values = await qs.values_list(*fields_name)
|
values = await qs.values(*fields_name)
|
||||||
values = await render_values(fields, values)
|
rendered_values = await render_values(fields, values)
|
||||||
context = {
|
context = {
|
||||||
"request": request,
|
"request": request,
|
||||||
"resources": resources,
|
"resources": resources,
|
||||||
"fields_label": fields_label,
|
"fields_label": fields_label,
|
||||||
"fields": fields,
|
"fields": fields,
|
||||||
"values": values,
|
"values": values,
|
||||||
|
"rendered_values": rendered_values,
|
||||||
"filters": filters,
|
"filters": filters,
|
||||||
"resource": resource,
|
"resource": resource,
|
||||||
"model_resource": model_resource,
|
"model_resource": model_resource,
|
||||||
|
@ -58,10 +58,10 @@ async def render_values(
|
|||||||
ret = []
|
ret = []
|
||||||
for value in values:
|
for value in values:
|
||||||
item = []
|
item = []
|
||||||
for i, v in enumerate(value):
|
for i, k in enumerate(value):
|
||||||
if display:
|
if display:
|
||||||
item.append(await fields[i].display.render(v))
|
item.append(await fields[i].display.render(value[k]))
|
||||||
else:
|
else:
|
||||||
item.append(await fields[i].input.render(v))
|
item.append(await fields[i].input.render(value[k]))
|
||||||
ret.append(item)
|
ret.append(item)
|
||||||
return ret
|
return ret
|
||||||
|
@ -107,20 +107,24 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for value in values %}
|
{% for value in rendered_values %}
|
||||||
<tr>
|
<tr {% for k,v in model_resource.row_attributes(values[loop.index0]).items() %}{{ k }}="{{ v }}"{% endfor %}>
|
||||||
{% if model_resource.bulk_actions %}
|
{% if model_resource.bulk_actions %}
|
||||||
<td>
|
<td>
|
||||||
<input
|
<input
|
||||||
data-id="{{ value[0]|int }}"
|
data-id="{{ value[0]|int }}"
|
||||||
class="form-check-input m-0 align-middle checkbox-select-item"
|
class="form-check-input m-0 align-middle checkbox-select-item"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% for v in value %}
|
{% with outer_index = loop.index0 %}
|
||||||
<td>{{ v|safe }}</td>
|
{% for x in value %}
|
||||||
|
<td
|
||||||
|
{% for k,v in model_resource.cell_attributes(values[outer_index],fields[loop.index0]).items() %}
|
||||||
|
{{ k }}="{{ v }}"{% endfor %}>{{ x|safe }}</td>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
{% endwith %}
|
||||||
{% if model_resource.actions %}
|
{% if model_resource.actions %}
|
||||||
<td class="text-end">
|
<td class="text-end">
|
||||||
<span class="dropdown">
|
<span class="dropdown">
|
||||||
|
Reference in New Issue
Block a user