如何在zend框架中过滤部分视图

时间:2012-12-24 06:06:03

标签: php zend-framework

enter image description here

我在zend中有一个视图,我们有表单提交条目,在底部我们列出了以前的条目。当我们点击搜索按钮时,我已应用搜索功能及其正常工作。现在我想要的是使用getJSON事件在keyup事件上应用搜索,这样我们就可以立即搜索内容。

我为搜索添加了两个条件...使用搜索按钮点击进行搜索。

  if($req->isPost()) {

        {
            //$ajaxPostedData = $req->getPost();
        $ajaxPostedData = $req->getParams();
        //echo $ajaxPostedData['search_term'];
        $select->where("ma_heading like '%".$ajaxPostedData['search_term']."%'");       
        //$select->where($val)
        //echo $select->__toString();die;
        //print_r($data = $db_model->fetchAll($select));
        //die;
        //echo $select->__toString();

        return $data = $db_model->fetchAll($select);

        # Paging section
        $paginator = Zend_Paginator::factory($data);
        $paginator->setItemCountPerPage(PAGING_RESULT_PER_PAGE);
        $paginator->setCurrentPageNumber($page);
        $paginator->setPageRange(PAGING_PAGES_RANGE_PER_PAGE);
        $this->view->data = $paginator;
            //$this->_helper->P($ajaxPostedData);die;
            //


    }

和keyup事件

if($req->isXmlHttpRequest())
        {
            //$ajaxPostedData = $req->getPost();
        $ajaxPostedData = $req->getParams();
        //echo $ajaxPostedData['search_term'];
        $select->where("ma_heading like '%".$ajaxPostedData['search_term']."%'");       
        //$select->where($val)
        //echo $select->__toString();die;
        //print_r($data = $db_model->fetchAll($select));
        //die;
        //echo $select->__toString();

        return $data = $db_model->fetchAll($select);

        # Paging section
        $paginator = Zend_Paginator::factory($data);
        $paginator->setItemCountPerPage(PAGING_RESULT_PER_PAGE);
        $paginator->setCurrentPageNumber($page);
        $paginator->setPageRange(PAGING_PAGES_RANGE_PER_PAGE);
        $this->view->data = $paginator;
            //$this->_helper->P($ajaxPostedData);die;
            //
        }

但是当我们在keyup上搜索内容时,内容不会动态替换。

请帮助我如何在zend中应用keyup搜索。

这是我用来发送请求的jQuery脚本

jQuery('#txt_heading_filer').keyup(function() {
        var heading = jQuery('#txt_heading_filer').val();

        $.getJSON('admin/admin/announcements/search_term/'+heading, function() {            

        });
    });

这是需要在搜索

上刷新的表格
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="top-table-bg">
        <tr>
            <th width="" align="left"><?php echo $obj_trans->_('heading'); ?></th>
            <th width="20%" align="left"><?php echo $obj_trans->_('expiry date'); ?></th>
            <th width="10%" align="center"><?php echo $obj_trans->_('publish/unpublish'); ?></th>
            <th width="10%" align="center"><?php echo $obj_trans->_('action'); ?></th>
        </tr>
        <?php  
        foreach($data_arr as $key => $data) { ?>
        <tr>
          <td align="left" class="name" ><span><?php echo $data[PREFIX_MASTER_ANNOUNCEMENTS . 'heading']; ?></span></td>
          <td align="left" class="name">
          <?php echo (!empty($data[PREFIX_MASTER_ANNOUNCEMENTS . 'expiry_date'])) ? date("j", strtotime($data[PREFIX_MASTER_ANNOUNCEMENTS . 'expiry_date'])).'<sup>'.date("S", strtotime($data[PREFIX_MASTER_ANNOUNCEMENTS . 'expiry_date'])).'</sup> '.date("M, Y", strtotime($data[PREFIX_MASTER_ANNOUNCEMENTS . 'expiry_date'])) : "N/A"; ?>
          </td>
          <td align="center">
            <?php Admin_View_Helper_GeneralHelper::status_icon($data[PREFIX_MASTER_ANNOUNCEMENTS . 'status']); ?></td>
          <td align="center">
            <a href="javascript:;" class="info_tooltip update_data" title="Edit" rel="<?php echo $data[PREFIX_MASTER_ANNOUNCEMENTS . 'id']; ?>" id="link_edit"><?php echo $this->img("img/admin/icons/edit.png", array('id' => 'icon_edit')) ?></a>

            <a href="<?php echo $this->url(array('controller' => 'admin', 'action' => 'announcements', 'do' => 'delete', 'item' => $data[PREFIX_MASTER_ANNOUNCEMENTS . 'id'])); ?>" class="info_tooltip delete_data" title="Delete" rel="<?php echo $data[PREFIX_MASTER_ANNOUNCEMENTS . 'id']; ?>" id=""><?php echo $this->img("img/admin/icons/delete.png", array('id' => 'icon_delete')) ?></a>
          </td>
        </tr>
        <?php } ?>

      </table>

1 个答案:

答案 0 :(得分:2)

首先,您必须将表格放入ID为

的div中
<div  id='search-result-table'>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="top-table-bg">
        <!-- rows here -->
    </table>
</div>

然后在js函数上,调用ajax并用响应

替换div中的内容
var heading = jQuery('#txt_heading_filer').val();
var url = 'admin/admin/announcements/search_term/'+heading;
$.get(url, function(data) {
  $('#search-result-table').html(data.result);
});

然后准备结果。不需要if($req->isPost())条件。您必须为所有类型的请求调用paginator。

if($req->isXmlHttpRequest())
{
    $this->_helper->viewRenderer->setNoRender(); //Disable view
    $this->_helper->layout->disableLayout();    //Disable layout
    $html = $this->view->render('searchresult.phtml');  //Create a new view file in the script location and put the result (same code as in the view page) in it
    echo Zend_Json::encode(array('result'=>$html));     //Get the result and encode it
}

searchresult.phtml

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="top-table-bg">
    <!-- rows here -->
</table>