Of course! Let's dive deep into the datalist.php file in DedeCMS (织梦CMS).

This file is one of the most important and frequently modified files in the DedeCMS backend. It serves as the core AJAX data provider for many of the system's list pages, especially those in the content management area.
What is datalist.php?
In simple terms, datalist.php is a PHP script that acts as a backend API. When you are on a list page in the DedeCMS admin panel (e.g., "Content Management" -> "Articles"), and you perform an action like:
- Searching for content
- Filtering by status (all, pending, published)
- Filtering by a specific user or column
- Sorting by date or title
...the JavaScript on the page sends an AJAX request to datalist.php. This script then processes the request, queries the database based on the given parameters, and returns the results in a structured format (usually JSON) that the frontend JavaScript can then use to update the table without reloading the entire page.
Location and Basic Structure
You can find this file in the DedeCMS admin directory:
/dede/datalist.php

Its basic structure looks like this:
<?php
require_once(dirname(__FILE__).'/config.php');
CheckPurview('a_List,a_AccEdit,a_AccDel');
// Get the action from the request
$action = isset($action) ? $action : '';
// Switch statement to handle different actions
switch($action)
{
case 'getlist':
// This is the most common action: getting the data list
// 1. Get filters from the POST request (keyword, typeid, etc.)
// 2. Build the SQL query based on the filters
// 3. Execute the query
// 4. Loop through the results and format them
// 5. Echo the results as a JSON string
break;
case 'moveArchives':
// Handles moving articles to another channel
break;
case 'updateOrder':
// Handles updating the order of items
break;
// ... other cases for various administrative tasks
default:
// Default action or error
break;
}
?>
The Most Important Case: getlist
The getlist case is the heart of the file. It's responsible for fetching the content for the data table. Let's break down how it typically works.
Receiving Parameters
The script first collects all the filtering and sorting parameters sent from the frontend via a POST request. Common parameters include:
typeid: The ID of the channel or category to filter by.keyword: The search keyword.channelid: The specific channel ID.starttime/endtime: Date range for filtering.state: The status of the content (e.g., -1 for all, 0 for pending, 1 for published).orderby: The column to sort by (e.g.,pubdatefor publish date).orderway: The sort direction (descfor descending,ascfor ascending).pageno: The current page number for pagination.
Building the SQL Query
Based on the received parameters, the script dynamically constructs a SELECT query. It always starts with a base query and then adds WHERE clauses conditionally.

// Example of building the query (simplified)
$sql = "SELECT arc.id, arc.title, arc.typeid, arc.arcrank, arc.pubdate, t.typename
FROM `#@__archives` arc
LEFT JOIN `#@__arctype` t ON arc.typeid = t.id
WHERE 1=1 ";
// Add conditions based on parameters
if (!empty($typeid)) {
$sql .= " AND arc.typeid = '$typeid' ";
}
if (!empty($keyword)) {
$sql .= " AND arc.title LIKE '%$keyword%' ";
}
if ($state != '-1') {
$sql .= " AND arc.arcrank = '$state' ";
}
// Add ordering
$sql .= " ORDER BY arc.$orderby $orderway ";
Executing the Query and Handling Pagination
The script then executes this query. Since it's for a list page, it needs to handle pagination. This is usually done using the datalist function from DedeCMS's core library (include/datalist.class.php).
The datalist class simplifies pagination by:
- Executing a
COUNT(*)query to get the total number of records. - Executing the main
SELECTquery with aLIMITclause for the current page. - Returning an array containing the data for the current page and the total record count.
// Use the datalist class for easy pagination
$dlist = new DataList();
$dlist->pageSize = 20; // Set items per page
$dlist->SetParameter('typeid', $typeid);
$dlist->SetParameter('keyword', $keyword);
// ... other parameters
// Execute the query and get the data
$dlist->SetTemplate($tempfile); // This is often for generating static files, not for AJAX
$mainlist = $dlist->GetList($sql, $totalresult);
// $mainlist now contains the data for the current page
// $totalresult contains the total number of matching records
Formatting and Returning the JSON Response
Finally, the script loops through the $mainlist array, formats each row into a simple associative array, and then uses json_encode() to convert the entire collection into a JSON string. This string is then echo'd back to the browser.
// Prepare the data for JSON response
$data = array();
foreach ($mainlist as $row) {
$data[] = array(
'id' => $row['id'],
'title' => $row['title'],
'typename' => $row['typename'],
'pubdate' => date('Y-m-d H:i:s', $row['pubdate']),
'arcrank' => $row['arcrank'],
// ... other fields you need to display
);
}
// The response structure expected by the frontend JS
$result = array(
'data' => $data,
'total' => $totalresult,
'code' => 200
);
// Output the JSON
echo json_encode($result);
exit;
How to Modify datalist.php (Common Customizations)
Developers often need to modify this file to add custom fields or change filtering logic.
Example 1: Adding a Custom Field to the List
Let's say you have a custom field called service_level in your #@__archives table and you want to display it in the article list.
-
Modify the SQL Query: Add the new field to your
SELECTstatement.// In the 'getlist' case $sql = "SELECT arc.id, arc.title, arc.typeid, arc.arcrank, arc.pubdate, arc.service_level, t.typename ...";
-
Modify the JSON Formatting: Add the new field to the data array you are encoding.
// Inside the foreach loop $data[] = array( 'id' => $row['id'], 'title' => $row['title'], 'service_level' => $row['service_level'], // Add the new field // ... other fields ); -
Update the Frontend Template: You will also need to modify the corresponding JavaScript file (e.g.,
js/list.js) that renders the table to add a new column forservice_level.
Example 2: Adding a Custom Filter
Suppose you want to filter articles by a custom field is_featured (a tinyint where 1 is featured and 0 is not).
-
Receive the New Parameter: Get the value from the POST request.
$is_featured = isset($is_featured) ? (int)$is_featured : -1;
-
Add the Condition to the SQL Query:
if ($is_featured != -1) { $sql .= " AND arc.is_featured = '$is_featured' "; } -
Update the Frontend: The page that sends the AJAX request (e.g.,
article_list.php) needs to have a new dropdown or checkbox for "Is Featured?" that adds theis_featuredparameter to the AJAX call.
Summary
| Feature | Description |
|---|---|
| Purpose | Acts as the AJAX backend for DedeCMS admin list pages. |
| Mechanism | Receives filtering/sorting parameters via POST, queries the database, and returns results as JSON. |
| Key Case | getlist is the primary action for fetching and displaying data. |
| Core Logic | Build dynamic SQL, execute query (often with pagination), format results, and json_encode. |
| Customization | The primary file developers modify to add custom fields, new filters, or change data display logic. |
| Dependencies | Relies on config.php for the environment and often datalist.class.php for pagination. |
Understanding datalist.php is crucial for any DedeCMS developer who wants to customize the backend's data presentation and filtering capabilities.
