dede filtersearch如何高效使用?

99ANYc3cd6
预计阅读时长 29 分钟
位置: 首页 DEDE建站 正文

Of course! Let's break down dede:filtersearch. This is a very common and powerful tag in the DedeCMS (织梦CMS) templating system, used specifically for creating search forms that allow users to filter results based on specific custom fields.

dede filtersearch
(图片来源网络,侵删)

What is dede:filtersearch?

In simple terms, dede:filtersearch is a DedeCMS tag that generates an HTML <form> element. This form is designed to submit search queries that not only include a keyword search but also filter the results based on the values of one or more custom fields (also known as additional fields) associated with an article model.

It's the standard way to create advanced search functionality on your website, like searching for products by brand, color, or price range, or searching for articles by author, category, or a specific custom attribute.


How It Works: The Core Concept

  1. Associated Model: The tag doesn't work in isolation. It must be linked to a specific article model (文章模型) in DedeCMS. This model defines the custom fields available for filtering (e.g., brand, price, author, etc.).
  2. Form Generation: When the page is loaded, DedeCMS looks at the custom fields defined for that model.
  3. Dynamic HTML: It then dynamically generates the form, creating a dropdown list (<select>) for each custom field. The options in these dropdowns are populated with the unique values that actually exist in your database for that field.
  4. Form Submission: When a user submits the form, it sends the keyword and the selected filter values to a search script (usually plus/search.php).
  5. Filtering: The search script processes these parameters and performs a database query that finds articles matching both the keyword and the selected custom field filters.

Syntax and Parameters

The basic syntax of the tag is very simple:

{dede:filtersearch formid='searchform' infolen='60'}

Common Parameters:

Parameter Description Example
formid (required) The id attribute for the generated <form> element. This is useful for styling with CSS. formid='mysearch'
infolen (optional) The size attribute for the main keyword input field. infolen='80'
typeid (optional) Pre-selects a specific channel (arctype) for the search. If not set, it searches across all channels. typeid='1'
defaultchannel (optional) Sets the default channel to be searched in the dropdown. defaultchannel='1'
channeltype (optional) Specifies the type of channels to include in the channel dropdown. channeltype='1' (for article model)

Practical Example: A Product Search Page

Let's imagine you have a product model with custom fields like brand (品牌) and price_range (价格区间).

dede filtersearch
(图片来源网络,侵删)

Step 1: Create the Article Model

In your DedeCMS backend, you would have already created an "Article Model" (文章模型) and added the necessary custom fields:

  • brand (text input)
  • price_range (select dropdown with options: "0-100", "100-500", "500+")

Step 2: Use the Tag in Your Template

You would place the dede:filtersearch tag in your search template file (e.g., templets/default/search.htm).

<!DOCTYPE html>
<html>
<head>Advanced Product Search</title>
    <style>
        /* Simple styling for the form */
        #productSearchForm { border: 1px solid #ccc; padding: 15px; margin-bottom: 20px; }
        #productSearchForm p { margin: 10px 0; }
        #productSearchForm label { display: inline-block; width: 100px; }
    </style>
</head>
<body>
    <h1>Search Our Products</h1>
    <!-- This is where the magic happens -->
    {dede:filtersearch formid='productSearchForm' infolen='50' typeid='2' /}
    <hr>
    <!-- The search results will be displayed here -->
    {dede:arclist titlelen='40' orderby='pubdate'}
        <li>
            <a href="[field:arcurl/]">[field:title]</a>
            - [field:brand/] - [field:price_range/]
        </li>
    {/dede:arclist}
</body>
</html>

What the Generated HTML Looks Like

When you load this page, DedeCMS will replace {dede:filtersearch...} with the following HTML (the exact values in the dropdowns will depend on your data):

<form name="formsearch" id="productSearchForm" action="/plus/search.php">
    <input type="hidden" name="kwtype" value="0" />
    <input type="hidden" name="searchtype" value="image" />
    <p>
        <label>Keyword:</label>
        <input type="text" name="q" size="50" value="" />
    </p>
    <p>
        <label>Channel:</label>
        <select name="typeid">
            <option value=''>All Channels</option>
            <option value='2' selected>Products</option>
            <!-- other channels would be listed here -->
        </select>
    </p>
    <p>
        <label>Brand:</label>
        <select name="brand">
            <option value=''>-- Any Brand --</option>
            <option value='Apple'>Apple</option>
            <option value='Samsung'>Samsung</option>
            <option value='Sony'>Sony</option>
        </select>
    </p>
    <p>
        <label>Price Range:</label>
        <select name="price_range">
            <option value=''>-- Any Price --</option>
            <option value='0-100'>$0 - $100</option>
            <option value='100-500'>$100 - $500</option>
            <option value='500+'>$500+</option>
        </select>
    </p>
    <p>
        <button type="submit">Search</button>
    </p>
</form>

As you can see, the tag automatically created the "Brand" and "Price Range" dropdowns based on the custom fields in your "Products" model.

dede filtersearch
(图片来源网络,侵删)

Important Considerations and Troubleshooting

  1. It's Tied to a Channel: The dede:filtersearch tag gets its custom fields from the channel (arctype) specified by the typeid parameter. If you don't set typeid, it might not work as expected or could pull fields from a default channel. Always ensure the channel you are searching has the custom fields you want to filter by.
  2. Data Must Exist: The dropdown lists are populated by querying the dede_archives table for existing values in the custom field columns. If you have a custom field color but no articles have a value for it, the dropdown will be empty (or just show the "Any" option).
  3. Form Action: The form's action attribute is almost always /plus/search.php. You don't need to set this manually; the tag handles it.
  4. Custom Field Names: The name attribute of the generated dropdowns will be the lowercase name of your custom field (e.g., name="brand"). This is how the search script knows which field to filter on.
  5. When Not to Use It: If you only need a simple keyword search without any custom field filtering, you can use a basic HTML form:
    <form name="formsearch" action="/plus/search.php">
        <input type="hidden" name="kwtype" value="0" />
        <input type="text" name="q" />
        <button type="submit">Search</button>
    </form>

In summary, dede:filtersearch is an essential tool for creating user-friendly and powerful advanced search interfaces in DedeCMS, bridging the gap between simple keyword searches and complex database queries.

-- 展开阅读全文 --
头像
nginx环境下织梦验证码不显示怎么办?
« 上一篇 2025-12-19
dede typegetsunid作用是什么?
下一篇 » 2025-12-19

相关文章

取消
微信二维码
支付宝二维码

目录[+]