You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SilverStripe 5前端职位搜索表单无法显示筛选结果求助

职位搜索结果不显示问题排查与解决

问题描述

实现前端职位搜索功能时,表单在JobSearchPage.ss模板正常显示,用户提交的字段值能在JobSearchPageController的showJobsForm方法中完成职位筛选,但表单下方的JobSearchResult.ss无法显示筛选后的职位列表,页面无任何内容。

问题分析

  1. Controller逻辑矛盾:showJobsForm方法中先返回结果数组,后续又存在重定向代码(即使被注释,原返回方式也无法将结果正确传递到原页面模板)。SilverStripe中,表单提交后若要在原页面显示结果,需将数据传递给原模板并渲染,而非直接返回数组或重定向。
  2. 模板冗余布局:JobSearchResult.ss重复包含SideBar,导致布局错乱,同时干扰数据传递的上下文。
  3. 日期格式无验证:未对用户输入的日期做有效性校验,若日期格式错误会直接导致筛选逻辑失效。

解决方案

1. 修正JobSearchPageController.php的showJobsForm方法

调整返回逻辑,将筛选结果和填充了提交数据的表单传递给原页面模板:

<?php

use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\FormAction;
use SilverStripe\ORM\ArrayLib;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use SilverStripe\Control\HTTP;
use SilverStripe\Control\Controller;

// 取消注释,确保JobPage类被正确引入
use JobPage;

class JobSearchPageController extends PageController
{

    private static $allowed_actions = [
        'SearchJobsForm',
        'showJobsForm',
    ];


    public function SearchJobsForm(): Form
    {

        $locations = [
            'On-site only' => 'On-site only',
            'Remote option' => 'Remote option',
            'Temporary remote' => 'Temporary remote',
            'Hybrid' => 'Hybrid',
            'Remote-first' => 'Remote-first',
            'Any' => 'Any'
        ];

        $types = [
            'Full-time' => 'Full-time',
            'Fresher' => 'Fresher',
            'Contract' => 'Contract',
            'Temporary' => 'Temporary',
            'Internship' => 'Internship',
            'Part-time' => 'Part-time',
            'Any' => 'Any'
        ];

        $levels = [
            'Bachelor\'s degree' => 'Bachelor\'s degree',
            'Master\'s degree' => 'Master\'s degree',
            'Doctoral degree' => 'Doctoral degree',
            'Other' => 'Other',
            'Any' => 'Any'
        ];

        $germanRequired = [
            'Required' => 'Required',
            'Not required' => 'Not required',
            'Any' => 'Any'
        ];

        $fields = FieldList::create(
                    TextField::create('Title')
                        ->setAttribute('placeholder', 'e.g: Data scientist')
                        ->addExtraClass('form-control'),
                    TextField::create('StartDate','Earliest start date')
                        ->setAttribute('data-datepicker',true)
                        ->setAttribute('data-date-format','DD-MM-YYYY')
                        ->setAttribute('placeholder', 'e.g: 20.07.2025')
                        ->addExtraClass('form-control'),
                    DropdownField::create('Location','Job location type')
                        ->setSource($locations)
                        ->addExtraClass('form-control'),
                    DropdownField::create('Type','Job type')
                        ->setSource($types)
                        ->addExtraClass('form-control'),
                    DropdownField::create('Level','Education level required')
                        ->setSource($levels)
                        ->addExtraClass('form-control'),
                    TextField::create('Postal','Job Postal code')
                        ->setAttribute('placeholder', 'e.g: 10115')
                        ->addExtraClass('form-control'),
                    DropdownField::create('GermanRequired','German language required')
                        ->setSource($germanRequired)
                        ->addExtraClass('form-control')
        );

        // 给表单按钮添加文字
        $actions = FieldList::create(FormAction::create('showJobsForm', 'Search'));

        $form = Form::create($this, 'SearchJobsForm', $fields, $actions);

        $form->setFormAction(
            Controller::join_links($this->Link(), 'showJobsForm')
        );

        return $form;
    }


    public function showJobsForm(HTTPRequest $request)
    {
        $vars = $request->postVars();

        // 初始化变量并处理空值
        $title = $vars['Title'] ?? '';
        $startDate = $vars['StartDate'] ?? '';
        $location = $vars['Location'] ?? '';
        $type = $vars['Type'] ?? '';
        $level = $vars['Level'] ?? '';
        $postal = $vars['Postal'] ?? '';
        $germanRequired = $vars['GermanRequired'] ?? '';

        $jobs = JobPage::get()->filter(['Active' => true,'Verified' => true])->sort('StartDate DESC');

        if (!empty($title)) {
            $jobs = $jobs->filter(['Title:PartialMatch' => $title]);
        }

        if (!empty($startDate)) {
            $startDateStamp = strtotime($startDate);
            // 验证日期格式有效性
            if ($startDateStamp) {
                $startDate2 = date('Y-m-d', $startDateStamp);
                $jobs = $jobs->filter(['StartDate:GreaterThanOrEqual' => $startDate2]);
            }
        }

        if (!empty($location) && $location != 'Any') {
            $jobs = $jobs->filter('Location', $location);
        }

        if (!empty($type) && $type != 'Any') {
            $jobs = $jobs->filter('Type', $type);
        }

        if (!empty($level) && $level != 'Any') {
            $jobs = $jobs->filter('Level', $level);
        }

        if (!empty($postal)) {
            $jobs = $jobs->filter(['Postal:PartialMatch' => (int)$postal]);
        }

        if (!empty($germanRequired)) {
            $gr = match($germanRequired){
                    'Required' => true,
                    'Not required' => false,
                    default => 'Any'
            };

            if ($gr != 'Any') {
                $jobs = $jobs->filter('GermanRequired', $gr);
            }
        }

        $paginatedJobs = PaginatedList::create($jobs, $request)
                        ->setPageLength(16)
                        ->setPaginationGetVar('s');

        // 加载表单并填充用户提交的数据
        $form = $this->SearchJobsForm();
        $form->loadDataFrom($vars);

        // 传递数据到原页面模板并渲染
        return $this->customise([
            'SearchJobsForm' => $form,
            'Results' => $paginatedJobs
        ])->renderWith('JobSearchPage');
    }

}

2. 修正JobSearchResult.ss模板

去掉重复的SideBar引用,修复列表中的语法错误(多余的>符号):

<div class="content-container unit size3of4 lastUnit">
    <div style="margin:auto;width:100%">
        <% if $Results %>
            <div class="row row-cols-6">
                <% loop $Results %>
                    <div class="col">
                        <p style="float:left;padding:3px">
                            <a href="$Link">
                                $Logo.Fit(80,80)
                            </a>
                        </p>
                        <ul class="">
                            <li class=""><a href="$Link">$Title</a></li>
                            <li class="">$StartDate.Format('dd/MM/yyyy')</li>
                            <li class="">$Location</li>
                            <li class="">$Type</li>
                            <li class="">$Postal</li>
                        </ul>
                    </div>
                <% end_loop %>
            </div>
            <div class="">
                <% if $Results.MoreThanOnePage %>
                    <div id="PageNumbers">
                        <div class="pagination">
                            <% if $Results.NotFirstPage %>
                                <a class="prev" href="$Results.PrevLink" title="View the previous page">&larr;</a>
                            <% end_if %>
                            <span>
                                <% loop $Results.Pages %>
                                    <% if $CurrentBool %>
                                        $PageNum
                                        <% else %>
                                            <a href="$Link" title="View page number $PageNum" class="go-to-page">$PageNum</a>
                                    <% end_if %>
                                <% end_loop %>
                            </span>
                            <% if $Results.NotLastPage %>
                                <a class="next" href="$Results.NextLink" title="View the next page">&rarr;</a>
                            <% end_if %>
                        </div>
                        <p>Page $Results.CurrentPage of $Results.TotalPages</p>
                    </div>
                <% end_if %>
            </div>
        <% else %>
            Your job search did not yield any results.
        <% end_if %>
    </div>
</div>

3. 额外检查项

  • 确保JobPage模型中存在Title、StartDate、Location等字段,且字段名称与筛选逻辑中的完全一致。
  • 验证JobPage的Active和Verified字段为布尔类型,确保初始筛选逻辑有效。

内容的提问来源于stack exchange,提问作者Kami Barut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 19:09:52