Magento Customizing a search function

Source: Internet
Author: User

There is a need to do a search function in the recent work, but because of the need to customize some of the appearance, so there is no traditional way to inherit the base class grid. The core of this requirement is actually the following approach.

$this->getlayout ()->getblock (' YourName ')->setyourvalue (' Yourvalue ');  

As you all know, Magento's MVC architecture is a little different from the other MVC frameworks, and the Magento v layer calls data from the block, which you can see in the configuration file in the Layout folder.

<vendors_dealer_dealer_getdealer>        <reference name= "Content" >            <block type= "vendorsproduct/ Vendor_dealersearch "Name=" Dealer_search "template=" ves/filter/dealersearch.phtml "/>        </reference> </vendors_dealer_dealer_getdealer>

The type specified here is the file location of the block, the $this on the Phtml page represents the block class itself, you can define the method in the Block class and then call this method in the Phtml page to get the data.

Now let's start by defining a search method

First, your data list page needs a search box, the following is the code of the phtml page

<divclass= "Order-filter" > <DL> <dtclass= "Last Odd" > order number:</dt> &LT;DDclass= "Last Odd" > <olclass= "Order-filter-item-list" > <li> <input type= "text"class= "Order-num"/> <a href= "javascript:void (0)"class= "Order-id-search" > Query </a> </li> </ol> </dd> </DL></div>
...... Data display

<script type= "Text/javascript" >
<! [cdata[
JQuery (". Order-id-search"). Click (function () {

var OrderID = JQuery (this). Prev (). Val ();
var url = "<?php echo mage::getbaseurl ()." Vendors/sales_order/getorder ';? > ";
Console.log (URL);

if (!orderid) {
Alert ("The query order number cannot be empty!") ");
return false;
}

Window.location.href= url+ "? order_id=" +orderid;

})
]]>
</script>

Two. You can see the input to search for the value, and then click on the search is actually access to the controller GetOrder this method, so we build this method

 Public functiongetorderaction ()
{ er _id=$this->getrequest ()->getparam (' order_id ',NULL); $vendor= Mage::getmodel (' vendors/session ')Getvendor (); $vendor _id=$vendor-getId ();
$read= Mage::getsingleton ("Core/resource")->getconnection (' Core_read ')); $sql= "Selectxfrom ' order ' where vendor_id= ' {$vendor _id} ' and order_id= ' {er _id} '";//Hypothetical SQL statement$result=$read->fetchall ($sql); $this-loadlayout (); $this->getlayout ()->getblock (' Order_search ')->setorder ($result); $this->_setactivemenu (' Sales ')->_title ($this->__ (' Sales '))->_title ($this->__ (' Orders '))); $this->_addbreadcrumb ($this->__ (' Sales '),$this->__ (' Sales ')); $this->_addbreadcrumb ($this->__ (' Orders '),$this->__ (' Orders '))); $this-renderlayout ();}

The key point here is $this->getlayout ()->getblock (' Order_search ')->setorder ($result) code, The meaning of this code is to set a template variable for block, and then use $this->getorder () to invoke the data in the template.

Three. Creating the XML for the layout file

<vendors_sales_order_getorder>        <reference name= "Content" >            <block type= "vendorssales/ Vendor_sales_ordersearch "Name=" Order_search "template=" ves/filter/ordersearch.phtml "/>        </reference ></vendors_sales_order_getorder>

The meaning of XML here is that when using GetOrder this controller method, will instantiate Ordersearch this block, and set the specified template file, here Name= "Order_search" is extremely important,$this GetLayout ()->getblock (' Order_search ')->setorder ($result) Here Order_search is this name= "Order_search".

Four. Create this block file

Because there is no need for the template to call the data from the block, it is possible to create an empty block.

Five. Create a template file

The template file is similar to the phtml page code of the data list page, but this page is more $this->getorder () This method, and then the resulting data is traversed to display on the page.

Magento Customizing a search function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.