Xem mẫu

Chapter 8 We use the getUserStateFromRequest() method to get the limit and limitstart variables. We use the user state variable, global.list.limit, to determine the limit. This variable is used throughout Joomla! to determine the length of lists. For example, if we were to view the Article Manager and select a limit if 5 items per page, when we move to a different list it will also be limited to 5 items. If a value is set in the request value limit (part of the listFooter) we use that value. Alternatively we use the previous value, and if that is not set we use the default value defined in the application configuration. The limitstart variable is retrieved from the user state value $option, plus .limitstart. $option is the component name, for example com_content. If we build a component that has multiple lists we should add an extra level to this, normally named after the entity. If a value is set in the request value limitstart (part of the listFooter) we use that value. Alternatively we use the previous value, and if that is not set we use the default value 0, which will lead us to the first page. At this stage you might be wondering why we handle this in the constructor and not the getPagination() method. As well as using these values for the JPagination object, we also need to use them when getting data from the database. Assuming we are using a method called getData() to retrieve the itemized data, our method might look like this: /** * Get itemized data * * @access public * @return array */ function getData() { if (empty($this->_data)) { $query = $this->_buildQuery(); $limitstart = $this->getState(`limitstart`); $limit = $this->getState(`limit`); $this->_data = $this->_getList($query, $limitstart, $limit); } return $this->_data; } [ 227 ] Rendering Output This method uses the private _buildQuery() method that we discussed earlier. We get the object state variables limit and limitstart and pass them to the _getList() method. The _getList() method is used to get an array of objects from the database based on a query and, optionally, limit and limitstart. The _getList() method is defined in the JModel class. The last two parameters will modify the first parameter, a query, in such a way that we only return the desired results. For example if we requested page 1 and were displaying a maximum of 5 items per page, the following would be appended to the query: LIMIT0, 5. Ordering It`s generally nice to allow the user to select a column in a table from which they want to be able to order itemized data. In Joomla!; we can use the JHTML grid.sort type to achieve this. Before we begin we must add two hidden fields to our form of itemized data, filter_order and filter_order_Dir. The first defines the field by which we want to order our data and the latter defines the direction in which we want to order our data, ascending or descending. At the top of each column in the itemized data table we create a heading using the grid. This is an example of a heading for a name column: lists[`order_Dir`], $this->lists[`order`]); ?> After grid.sort the parameters are the name that will appear at the top of the column, the sort value, the current order direction, and the current column by which the data is ordered. We`ll concentrate on the last two parameters. Bearing in mind that this code is to be used in a template file, the lists attribute is something that we must have assigned to the JView object in the display() method. [ 228 ] Chapter 8 This example demonstrates how we build the lists attribute; note that $option and $mainframe are declared global: // prepare list array $lists = array(); // get the user state of the order and direction $filter_order = $mainframe- >getUserStateFromRequest($option.`filter_order`, `filter_order`, `published`); $filter_order_Dir = $mainframe->getUserStateFromRequest($option.`filter_order_Dir`, `filter_order_Dir`, `ASC`); // set the table order values $lists[`order_Dir`] = $filter_order_Dir; $lists[`order`] = $filter_order; // add the lists array to the object ready for the layout $this->assignRef(`lists`, $lists); We use the application method getUserStateFromRequest() to determine the order and the direction, using the paths $option plus filter_order and filter_order_Dir respectively. The default values are published, which is the default column by which we will order the data, and ASC, the default ordering direction, ascending. We mentioned earlier that to facilitate the correct usage of JPagination we have to add two hidden fields, filter_order and filter_order_Dir. These are the fields from which these two $lists values are derived. So now that we have the lists attribute sorted we can quickly add those hidden fields to our temple. This example demonstrates how: The most important thing to notice here is that we leave the value of the filter_ order_Dir field empty. This is because the listFooter deals with this for us. Returning to our column heading there were two other parameters: the text that appears at the top of the column, and the sort value. The first of these is very straightforward. The second is slightly more ambiguous. It is the value that will be placed in the filter_order form field should we choose to order our itemized data by this column. [ 229 ] Rendering Output In order for us to be able to use these headings to their expected effect we need to modify our JModel class to deal with these. Earlier we spoke about the use of a _buildQuery() method to create the query with which we retrieve itemized data. This is an example of such a method: /** * Builds a query to get data from #__sometable * * @return string SQL query */ function _buildQuery() { return ` SELECT * ` . ` FROM #__sometable ` . $this->_buildQueryOrderBy(); } This method in turn calls a method named _buildQueryOrderBy() that builds the ORDERBY clause for the query. Let`s imagine that the entity with which we are dealing has three columns: name, published, and id. This is an example of a _buildQueryOrderBy() method: /** * Builds the ORDER part of a query * * @return string Part of an SQL query */ function _buildQueryOrderBy() { global $mainframe, $option; // Array of allowable order fields $orders = array(`name`, `published`, `id`); // get the order field and direction $filter_order = $mainframe->getUserStateFromRequest( $option.`filter_order`, `filter_order`, `published`); $filter_order_Dir = strtoupper($mainframe->getUserStateFromRequest( $option.`filter_order_Dir`, `filter_order_Dir`, `ASC`)); // validate the order direction, must be ASC or DESC if ($filter_order_Dir != `ASC` && $filter_order_Dir != `DESC`) { $filter_order_Dir = `ASC`; [ 230 ] Chapter 8 } // if order column is unknown use the default if (!in_array($filter_order, $orders)) { $filter_order = `published`; } // return the ORDER BY clause return ` ORDER BY `.$filter_order.` `.$filter_order_Dir; } As with the view, we retrieve the order column name and direction using the application getUserStateFromRequest() method. Since this data is going to be used to interact with the database, we perform some data sanity checks to ensure that the data is safe to use with the database. Finally, we build the ORDERBY clause and return it. When we deal with entities that have an ordering field, we generally build more complex ORDERBY clauses. For example, when we order by ascending name, we might want the ORDERBY clause to be ORDERBYname,ordering. Now that we have done this we can use the table headings to order itemized data. This is a screenshot of such a table: Notice that the current ordering is name ascending, as denoted by the small arrow to the right of Name. Filtering and Searching In many respects, the process of filtering and searching itemized data is very similar to ordering itemized data. We`ll begin by talking a look at filtering. This is a screenshot of the filtering and search form controls that appear at the top of the Article Manager: In this case, there are many filtering options: the section, category, author, and published state. We will start with the easiest—we will look at how to implement a published-state filter. [ 231 ] ... - tailieumienphi.vn
nguon tai.lieu . vn