2016-04-20 27 views
1

我正在为列出文章的SilverStripe网站的页面工作。我列入了可用年份的下拉列表以返回特定文章。我遇到的麻烦是找到一种方法将下拉列表中的选定日期与文章数据对象的日期字段的数据库值关联起来。使用PHP和jQuery从下拉菜单中选择日期后更新内容

下面是文章的数据对象类与存储一年,用于在功能每一篇名为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函数,以便根据下拉列表选择的年份获得正确的文章列表。现在,就我所知,我只收到一篇文章,无论选择哪一年,这都是不正确的。任何的意见都将会有帮助!

回答

2

好吧,终于明白了!

 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。我让它太复杂了。这种方法似乎正在工作。不知道这是否是最好的方法,但我也没有看到任何问题。

+0

随时接受你自己的答案。我看起来很好。 – wmk

相关问题