Files
tfluceecrud/index.cfm
T

112 lines
3.5 KiB
Plaintext

<cfset tableName = request.tableName />
<!--- Обработка действий --->
<cfif isDefined("FORM.action")>
<cfif FORM.action EQ "create">
<cfquery name="insertRow">
INSERT INTO #tableName# (value)
VALUES (<cfqueryparam value="#FORM.value#" cfsqltype="cf_sql_varchar" />)
</cfquery>
</cfif>
<cfif FORM.action EQ "update">
<cfquery name="updateRow">
UPDATE #tableName#
SET value = <cfqueryparam value="#FORM.value#" cfsqltype="cf_sql_varchar" />
WHERE id = <cfqueryparam value="#FORM.id#" cfsqltype="cf_sql_varchar" />
</cfquery>
</cfif>
<cfif FORM.action EQ "delete">
<cfquery name="deleteRow">
DELETE FROM #tableName#
WHERE id = <cfqueryparam value="#FORM.id#" cfsqltype="cf_sql_varchar" />
</cfquery>
</cfif>
<cflocation url="index.cfm" addtoken="no" />
</cfif>
<!--- Режим редактирования --->
<cfset editRow = {} />
<cfif isDefined("URL.edit")>
<cfquery name="editQuery">
SELECT id, value FROM #tableName#
WHERE id = <cfqueryparam value="#URL.edit#" cfsqltype="cf_sql_varchar" />
</cfquery>
<cfif editQuery.recordCount GT 0>
<cfset editRow = { id = editQuery.id, value = editQuery.value } />
</cfif>
</cfif>
<!--- Все записи --->
<cfquery name="rows">
SELECT id, value FROM #tableName# ORDER BY id
</cfquery>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8" />
<title>CRUD — #tableName#</title>
<style>
body { font-family: sans-serif; max-width: 600px; margin: 40px auto; }
table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }
th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
th { background: #f5f5f5; }
form { margin-bottom: 20px; }
input, button { padding: 6px 10px; margin: 2px; }
.actions a { margin: 0 4px; }
</style>
</head>
<body>
<h1>#tableName#</h1>
<table>
<tr>
<th>ID</th>
<th>Value</th>
<th></th>
</tr>
<cfoutput query="rows">
<tr>
<td>#id#</td>
<td>#value#</td>
<td class="actions">
<a href="index.cfm?edit=#id#">✏️</a>
<form method="post" style="display:inline">
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="#id#" />
<button type="submit" onclick="return confirm('Удалить #id#?')">🗑️</button>
</form>
</td>
</tr>
</cfoutput>
<cfif rows.recordCount EQ 0>
<tr><td colspan="3">Нет записей. Создайте первую.</td></tr>
</cfif>
</table>
<cfif NOT structIsEmpty(editRow)><cfoutput>
<h2>Редактировать #editRow.id#</h2>
<form method="post">
<input type="hidden" name="action" value="update" />
<input type="hidden" name="id" value="#editRow.id#" />
<input type="number" name="value" value="#editRow.value#" required />
<button type="submit">Сохранить</button>
<a href="index.cfm">Отмена</a>
</form></cfoutput>
<cfelse>
<h2>Добавить</h2>
<form method="post">
<input type="hidden" name="action" value="create" />
<input type="number" name="value" placeholder="Значение" required />
<button type="submit">Добавить</button>
</form>
</cfif>
</body>
</html>