使用 PHP 和 jQuery 从下拉列表中选择日期后更新内容

Update content after selecting date from dropdown using PHP and jQuery

我正在为一个列出文章的 SilverStripe 网站制作一个页面。我在 return 特定文章中包含了可用年份的下拉列表。我遇到的问题是找到一种方法将下拉列表中的选定日期与文章数据对象的日期字段的数据库值连接起来。

这里是文章数据对象 class,其函数将每篇文章的年份存储在一个名为 getYearCreated 的函数中:

class RecentCoverageDoc extends DataObject {

    private static $db = array(
        'Title' => 'varchar(250)',
        'Publisher' => 'varchar(250)',
        'Date' => 'date',
    );

    private static $has_one = array(
        'ArticleDoc' => 'File',
    );

    private static $summary_fields = array(
        'Title' => 'Title',
        'Publisher' => 'Publisher',
        'Date' => 'Date'
    );

    public function getYearCreated() {
      return date('Y', strtotime($this->Date));
    }

}

在 Page.php 中,这是我正在构建的函数,旨在从年份下拉列表中获取选定的值,并使用它来获取与该年份相关的正确文章列表:

public function getDocsByYear(){
        //Get the year selected by the dropdown
        $docYear = $this->getRequest()->param('ID');

        //Group together all docs that are associated with that selected year
        $documents = GroupedList::create(RecentCoverageDoc::get()->sort('Date'));

        $return = array();

        //put the articles into the array that match the selected year
        foreach($documents as $document){
            $docDate = date("Y", strtotime($document->Date));
            $selectedDate = date("Y",strtotime($docYear));
             if($docDate == $selectedDate){
                $return[] = array(
                   'title' => $document->Title,
                   'date' => $document->Date,
                    'publisher' =>$document->Publisher
                );
             }
        }

        return json_encode($return);

    }

最后,jQuery 代码获取年份下拉列表的值并将其发送到 getDocsByYear 函数:

   (function($) {
    $(document).ready(function() {

        var SelectYear = $('#SelectYear');

        SelectYear.change(function() {
            if (SelectYear.val() != "" && SelectYear.val() != null) {
                sendYear();
            }
        });

        function sendYear(){
            var year = SelectYear.find('option:selected').attr('value');
            $.ajax({
                type: "POST",
                url: "/home/getDocsByYear/"+year,
                dataType: "json"
            }).done(function (response) {
                var list = '';
                var docSection = $('.RecentDocs');

                for (var i=0;i<response.length;i++){
                    list += response[i].date + ', ' + response[i].publisher + ', ' + '<a href="' + response[i].title + ' target="_blank">"' + response[i].title +"</a> <br />";
                }
                docSection.empty();
                docSection.append(list);
            });
        }

    });
}(jQuery));

我正在考虑根据从下拉列表中选择的年份获取文章列表的最佳方法。页面上的年份下拉列表和文章列表是使用 GroupedList 功能填充的:

<select id="SelectYear">
    <% loop $GroupedDocsByDate.GroupedBy(YearCreated) %>
        <option value="$YearCreated">$YearCreated</option>
    <% end_loop %>
</select>
<br /><br />
<div class="RecentDocs">
    <% loop $getAllRecentCoverageDocs %>
        $Date, <a href="$ArticleDoc.URL" target="_blank">$Title</a>, $Publisher<br /><br />
    <% end_loop %>
</div>

我认为使用类似的方法可以按年份获取文章,但我对如何完成 getDocsByYear 功能感到困惑,因此我根据选择的年份获得了正确的文章列表下拉列表。现在,以我所拥有的,无论选择哪一年,我都只能得到一篇文章 returned,这是不正确的。任何建议都会有所帮助!

好的,终于明白了!

     public function getDocsByYear(){
        //Get the year selected by the dropdown
        $docYear = $this->getRequest()->param('ID');

        //Group together all docs that are associated with that selected year
        $documents = RecentCoverageDoc::get();
        $return = array();

        //put the articles into the array that match the selected year
        foreach($documents as $document){
            $docDate = date("Y", strtotime($document->Date));

             if($docDate == $docYear){
                $return[] = array(
                   'title' => $document->Title,
                   'date' => $document->Date,
                    'publisher' =>$document->Publisher
                );
             }
        }

        return json_encode($return);

    }

我不需要 GroupedList。我把它弄得太复杂了。这种方法似乎有效。不确定这是否是有史以来最好的方法,但我也没有发现它有任何问题。