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


Update content after selecting date from dropdown using PHP and jQuery

我正在为一个列出文章的SilverStripe网站开发页面。我包含一个下拉列表,其中包含返回特定文章的可用年份。我遇到的麻烦是找到一种方法将下拉列表中的选定日期与文章数据对象的日期字段的数据库值连接起来。

下面是文章数据对象类,其中包含一个名为 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);
    }

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

   (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功能,因此我根据下拉列表选择的年份获得了正确的文章列表。现在,以我所拥有的,无论选择哪一年,我都只返回一篇文章,这是不正确的。任何建议都会有所帮助!

好的,终于明白了!

     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);
    }

我不需要分组列表。我把它弄得太复杂了。这种方法似乎有效。不确定这是否是有史以来最好的方法,但我也不认为它有什么问题。