Jump to content

Ordering attributes within a node: Difference between revisions

From NusaATK
Mjs (talk | contribs)
if fields are added via a constructor, they will appear in the order in which they are added
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 3: Line 3:
==Ordering attributes within a node==
==Ordering attributes within a node==


By default, fields appear in the order in which they appear in the database.  To control the order in which attributes appear within a node (i.e. on a "page"), use <tt>atkMetaPolicy::setOrder()</tt>, <tt>atkMetaPolicy::setIncludes()</tt> or the <tt>$order</tt> argument to either <tt>atkMetaNode::add()</tt> or <tt>atkMetaPolicy::add()</tt>.
If you have an <tt>atkNode</tt>, attributes (i.e. database columns) appear in the order in which they are added via <tt>atkNode::add()</tt>.


For example:
If you have an <tt>atkMetaNode</tt> (or derived class), fields appear in the order in which they appear in the database. To control the order in which attributes appear within a node (i.e. on a "page"), use <tt>atkMetaPolicy::setOrder()</tt>, <tt>atkMetaPolicy::setIncludes()</tt> or the <tt>$order</tt> argument to either <tt>atkMetaNode::add()</tt> or <tt>atkMetaPolicy::add()</tt>.  If you don't have access to an <tt>atkMetaPolicy</tt>, use <tt>atkMetaNode::setAttributeOrder()</tt>.
 
===Examples===
 
With <tt>atkMetaPolicy</tt>:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
public static function meta(atkMetaPolicy $policy) {
public static function meta(atkMetaPolicy $policy)  
{
   $policy->add("invoice_start_date", "atkDateAttribute");
   $policy->add("invoice_start_date", "atkDateAttribute");
   $policy->add("invoice_end_date", "atkDateAttribute");
   $policy->add("invoice_end_date", "atkDateAttribute");
Line 19: Line 24:
</syntaxhighlight>
</syntaxhighlight>


(The order in which fields are added has no effect because fields are added to nodes before both the settings from <tt>atkMetaNode::meta()</tt> are applied, and the <tt>atkMetaNode::postMeta()</tt> method is run--"adding" a field via either of these fields reconfigures an existing node.  Fields <em>will</em> appear in the order in which they are added in a constructor, but configuring nodes via a constructor is discouraged.)
Using <tt>[[atkMetaAttributeModifier]]</tt> attribute modifiers:
 
<syntaxhighlight lang="php">
public static function meta(atkMetaPolicy $policy)
{
  // Make sure that field1 appears after field 2
  $policy->get("field1")->insertAfter("field2");
 
  // Make sure the remark field appears at the bottom.
  $policy->get("remark")->insertAtBottom();
}
 
See the [http://www.atk-framework.com/docs/atk/trunk/atk/meta/atkMetaAttributeModifier.html atkMetaAttributeModifier] API docs for more possibilities on reordering.
 
</syntaxhighlight>
 
 
=== Independently ordering attributes on the "admin" and "view" pages ===
 
If you want your attributes to appear in one order in the "admin" (list) page, and in another in the "view" (detail) page, you can do something like the following to ensure that the "name" attribute will appear before the "email" attribute of "view" pages:
 
<syntaxhighlight lang="php">
  public function action_view($handler) {
    $this->setAttributeOrder("name", 10);
    $this->setAttributeOrder("email", 20);
    $this->attribSort();
    $handler->action_view($handler);
  }
</syntaxhighlight>
 
(Note that where they will appear in relation to the other attributes is dependent on the "order" value of the other attributes.)

Latest revision as of 15:21, 16 March 2011

ATK Howto: Ordering attributes within a node

Complexity: Basic
Author: Ivo Jansch <ivo@achievo.org>

List of other Howto's

Ordering attributes within a node

If you have an atkNode, attributes (i.e. database columns) appear in the order in which they are added via atkNode::add().

If you have an atkMetaNode (or derived class), fields appear in the order in which they appear in the database. To control the order in which attributes appear within a node (i.e. on a "page"), use atkMetaPolicy::setOrder(), atkMetaPolicy::setIncludes() or the $order argument to either atkMetaNode::add() or atkMetaPolicy::add(). If you don't have access to an atkMetaPolicy, use atkMetaNode::setAttributeOrder().

Examples

With atkMetaPolicy:

public static function meta(atkMetaPolicy $policy) 
{
  $policy->add("invoice_start_date", "atkDateAttribute");
  $policy->add("invoice_end_date", "atkDateAttribute");

  $policy->setOrder(array(
    "invoice_start_date",
    "invoice_end_date"
  ));
}

Using atkMetaAttributeModifier attribute modifiers:

public static function meta(atkMetaPolicy $policy) 
{
  // Make sure that field1 appears after field 2
  $policy->get("field1")->insertAfter("field2");

  // Make sure the remark field appears at the bottom.
  $policy->get("remark")->insertAtBottom();
}

See the [http://www.atk-framework.com/docs/atk/trunk/atk/meta/atkMetaAttributeModifier.html atkMetaAttributeModifier] API docs for more possibilities on reordering.


Independently ordering attributes on the "admin" and "view" pages

If you want your attributes to appear in one order in the "admin" (list) page, and in another in the "view" (detail) page, you can do something like the following to ensure that the "name" attribute will appear before the "email" attribute of "view" pages:

  public function action_view($handler) {
    $this->setAttributeOrder("name", 10);
    $this->setAttributeOrder("email", 20);
    $this->attribSort();
    $handler->action_view($handler);
  }

(Note that where they will appear in relation to the other attributes is dependent on the "order" value of the other attributes.)