π AppML β Data Filtering and Sorting: Display Only What Matters
π§² Introduction β Why Filter and Sort Data in AppML?
Displaying large datasets is common in web apps, but users often need specific information quickly. Thatβs where filtering and sorting become essential. With AppML, you can implement these features with just HTML and lightweight controller logic, eliminating the need for JavaScript frameworks.
π― In this article, youβll learn:
- How to filter AppML data using controllers or URLs
- How to sort data by fields (ascending/descending)
- Real-world examples for server-side and static filtering
- Best practices for performance and UX
π§ͺ Basic Example β Filter Products by Category
β
JSON File: data/products.json
[
{ "name": "Laptop", "category": "Electronics" },
{ "name": "Sofa", "category": "Furniture" },
{ "name": "Phone", "category": "Electronics" }
]
β HTML View with Controller
<div appml-data="data/products.json" appml-controller="controller.js">
<h3>{{name}}</h3>
<p>Category: {{category}}</p>
</div>
β
controller.js
myAppML.onshow = function() {
myAppML.filter = "category='Electronics'";
};
β Only products in the “Electronics” category will be displayed.
ποΈ Using sqlfilter
for Server-Side Filtering
When using PHP or ASP, apply filtering logic server-side using the sqlfilter
parameter.
β Controller Logic
myAppML.onshow = function() {
myAppML.sqlfilter = "price > 100";
};
β PHP Backend (get-products.php)
$filter = isset($_GET["filter"]) ? "WHERE " . $_GET["filter"] : "";
$sql = "SELECT name, price FROM products $filter";
AppML passes the sqlfilter
as a query string (e.g., ?filter=price%20%3E%20100
) to your backend.
π Client-Side Filtering (Using AppML filter
)
Use this when loading static JSON files:
myAppML.filter = "category LIKE 'Furn*'";
This uses a simple wildcard match and works great for small local datasets.
π Sorting AppML Data by Field
You can sort AppML data using:
β Controller-Based Sorting
myAppML.sort = "price";
To reverse the sort order:
myAppML.sort = "price DESC";
π Combined Example
myAppML.onshow = function() {
myAppML.filter = "category='Electronics'";
myAppML.sort = "price DESC";
};
This filters the data first and then sorts it from high to low by price.
π§ Server-Side Sorting (MySQL)
Apply sorting logic to your SQL query:
$sort = isset($_GET["sort"]) ? "ORDER BY " . $_GET["sort"] : "";
$sql = "SELECT * FROM products $filter $sort";
Pass it from AppML like this:
myAppML.sqlsort = "name ASC";
π§° Real Use Case: Filter by Input Field
You can dynamically filter data using HTML input and controllers.
β HTML
<input id="searchBox" onkeyup="filterByText(this.value)" placeholder="Search name">
<div appml-data="data/employees.json" appml-controller="emp-controller.js">
<p>{{name}}</p>
</div>
β
Controller (emp-controller.js
)
function filterByText(text) {
myAppML.filter = "name LIKE '*" + text + "*'";
myAppML.load();
}
π’ This adds live filtering functionality with no extra libraries!
π Summary β Recap & Key Takeaways
AppML makes data filtering and sorting accessible even to non-coders. With just a few attributes or a simple controller, you can enable powerful interactions in your web apps.
π Key Takeaways:
- Use
filter
for static files andsqlfilter
for server-side data - Sort using
sort
(client) orsqlsort
(server) - Filtering can use wildcards like
LIKE
for partial matches - Use input fields +
load()
to build dynamic filtering UIs - Always validate server-side filters to prevent SQL injection
βοΈ Filtering and sorting are core to building dashboards, product listings, reports, and admin interfaces in AppML.
β FAQs β Filtering and Sorting in AppML
β Can I filter by multiple fields?
β
Yes. Combine filters like category='Electronics' AND price > 100
.
β Is sorting case-sensitive?
β οΈ Sorting behavior depends on the backend (MySQL, JSON parser). Normalize text before comparing if needed.
β Whatβs the difference between filter
and sqlfilter
?
β
filter
works for static files like JSON/XML. sqlfilter
is passed to the backend PHP/ASP script for database queries.
β Can AppML sort on computed values?
β No. AppML can only sort fields directly available in the data model.
β How do I add dropdown filters in AppML?
β
Use <select>
and onchange
to set myAppML.filter
based on selected value, then call myAppML.load()
.
Share Now :