2015-10-27 24 views
0

我安裝了Dc搜索插件。我在交易頁面上應用這個插件,它可以正常工作。 但在聯繫索引頁上它不起作用。在這兩個頁面上的區別是聯繫頁面包含多個表,如公司,company_contacts,單位,稅收等等的關係。但在交易頁面數據只來自交易表。另外,這兩者都沒有區別。第二個是作爲聯繫人索引頁面的模型,我使用company.php而不是contact.php。Dc搜索框插件不能在cakephp中工作?

在我的index.php

 <?php if(!empty($search_args['search_cname'])): ?> 
     <strong><?php echo __('Company Name:'); ?> </strong><span><?php echo $search_args['search_cname']; ?></span> 
     <?php endif; ?> 
     <?php if(!empty($search_args['search_name'])): ?> 
     <strong><?php echo __('Contact Person:'); ?> </strong><span><?php echo $search_args['search_name']; ?></span> 
     <?php endif; ?> 
     <?php if(!empty($search_args['search_status'])): ?> 
     <strong><?php echo __('Status:'); ?> </strong><span><?php echo $search_args['search_status']; ?></span> 
     <?php endif; ?> 
     <?php if(!empty($search_args['search_uname'])): ?> 
     <strong><?php echo __('User Name:'); ?> </strong><span><?php echo $search_args['search_uname']; ?></span> 
     <?php endif; ?> 
    </div> 
    <?php endif; ?> 
    <div class="filter-box" style="display:none"> 
     <?php echo $this->Form->create('company', array('url' => array_merge(array('controller'=>'contacts','action' => 'index'), $this->params['pass']),'class'=>'form-inline')); ?> 
     <fieldset> 
     <legend><?php echo __(Filters); ?></legend> 
      <?php echo $this->Form->input('search_status',array('div'=>false,'class'=>'span2','options'=>array(4=>'Lead',5=>'Opportunity',6=>'Account'),'label'=>'Status ',)); ?> 
      <?php echo $this->Form->input('search_cname',array('div'=>false,'class'=>'span2','label'=>'Company Name ','placeholder'=>'Company Name')); ?> 
      <?php echo $this->Form->input('search_name',array('div'=>false,'class'=>'span2','label'=>'Contact Person ','placeholder'=>'Contact Person')); ?> 
      <?php echo $this->Form->input('search_uname',array('div'=>false,'class'=>'span2','label'=>'User Name ','placeholder'=>'User Name')); ?> 
      <?php echo $this->Form->submit('Filter',array('div'=>false,'class'=>'btn btn-info')); ?> 
     </fieldset> 
     <?php echo $this->Form->end(); ?> 
    </div> 

在ContactController.php

$searched = false; 
if ($this->passedArgs) { 
     $args = $this->passedArgs; 
     if(isset($args['search_cname'])){ 
      $searched = true; 
     } 
     if(isset($args['search_name'])){ 
      $searched = true; 
     } 
    } 
    $this->set('searched',$searched); 

在型號contact.php

public $actsAs = array(
     'Search.Searchable', 
     ); 

      public $filterArgs = array(
        'search_status' => array('type'=>'value','field'=>'company.contact_status_id'), 
        'search_cname' => array('type'=>'like','field'=>array('company.company_name')), 
        'search_name' => array('type'=>'like','field'=>array('company_contacts.first_name','company_contacts.last_name')), 
        'search_uname' => array('type'=>'like','field'=>'User.full_name'), 

     'search_all' => array('type'=>'query','method'=>'searchDefault') 
); 

public function searchDefault($data = array()) { 
    $filter = $data['search_all']; 
    $cond = array(
      'OR' => array(
       // 'company.company_name LIKE' => '%' . $filter . '%', 
       'company_contacts.first_name LIKE' => '%' . $filter . '%', 

       'company_contacts.last_name LIKE' => '%' . $filter . '%', 

       'company_contacts.email LIKE' => '%' . $filter . '%', 
       'company_contacts.mobile_number LIKE' => '%' . $filter . '%', 

        $this->alias . '.company_name LIKE' => '%' . $filter . '%', 
         'User.full_name LIKE' => '%' . $filter . '%', 
       // $this->alias . '.first_name LIKE' => '%' . $filter . '%', 
       // $this->alias . '.last_name LIKE' => '%' . $filter . '%', 
      //  $this->alias . '.status LIKE' => '%' . $filter . '%' 
      )); 
    return $cond; 
} 

回答

0

首先包括在控制器的PRG組件如下

public $components = array("Search.Prg"); 
在你的行動

然後加在頂部

$this->Prg->commonProcess(); 

$this->Paginator->settings['conditions'] = $this->Contact->parseCriteria($this->passedArgs); 
$this->set('searched', $this->Paginator->paginate()); 

欲瞭解更多詳情,以下行,請看看here

+0

我嘗試這一點,但它吉斯以下錯誤 –

+0

我試試這個。但不幸的是它給出了以下錯誤錯誤:調用未定義的方法PrgComponent :: parsedParams() File:C:\ xampp \ htdocs \ salescrm \ app \ Controller \ ContactsController.php Line:51 注意:定製這個錯誤信息,創建應用程序\視圖\錯誤\ fatal_error.ctp –

+0

好吧,得到你點 請用$ this-> passedArgs取代$ this-> Prg-> parsedParams()並重試。讓我知道如果仍然不工作 –

0

這裏是另一種方式來做到這一點,我自己的自定義

添加控制器中的以下行

public $presetVars = array(); 

然後

public function index() { 
    $this->Prg->commonProcess(); 
    $this->Contact->data["Contact"] = $this->passedArgs; 
    $this->request->data["Contact"] = $this->passedArgs; 
    $parsedConditions[] = $this->filter_condition($this->passedArgs); 

    $this->paginate = array(
     "conditions" => $parsedConditions 
    ); 
    $this->set('result', $this->paginate()); 
} 

public function filter_condition($data = array()) { 
    $parsedConditions = array(); 
    if (!empty($data)) { 
     $data = array_map('trim', $data); 
     if (is_array($data)) { 
      foreach ($data as $key => $value) { 
       foreach ($this-> Contact->filterArgs as $k => $v) { 
        if ($key == $v["name"]) { 
         $value = preg_replace('/\s+/', ' ', $value); 
         $value = Sanitize::clean($value); 
         if ($value != null) { 
          if ($v["type"] == "string") { 
           $parsedConditions[] = array("Contact.$key LIKE" => "%" . $value . "%"); 
          } else if ($v["type"] == "value") { 
           $parsedConditions[] = array("Contact.$key" => $value); 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
    return $parsedConditions; 
} 

和模型做寫過濾器ARGS這樣

/*過濾器的Fileds搜索*/

public $filterArgs = array(
     array("name" => "field1", "type" => "value"), 
     array("name" => "field2", "type" => "string") 
);