Jump to content

Dynamic and Dependant atkListAttribute

From NusaATK

Values available in list_2 are dependant on value selected in list_1


<?php

// --------------------------------------------------------------------
// testlists class
// --------------------------------------------------------------------

class testlists extends atkNode
{

  function testlists()
  {
    $this -> atkNode("testlists");

    $attr_list_1 = &$this -> add(new atklistattribute("list_1",
        array("Tens", "Twentys"), array(10, 20)));
    $attr_list_1 -> setLoadType(NOLOAD);
    $attr_list_1 -> setStorageType(NOSTORE);
    $attr_list_1 -> addDependee("list_2");

    $attr_list_2 = &$this -> add(new Dynamiclistattribute("list_2",
        array(), array()));
    $attr_list_2 -> setLoadType(NOLOAD);
    $attr_list_2 -> setStorageType(NOSTORE);
  }

}

// -------------------------------------------------------------------
// DynamicListAttribute class
// -------------------------------------------------------------------

class DynamicListAttribute extends atkListAttribute
{

  function edit($record, $prefix = "", $mode = "")
  {
    $optionarray = array();
    $valuearray  = array();

    if ($record["list_1"] == "10")
    {
      // NB These vales could be read from a DB table :)
      $optionarray = array("10", "11", "12");
      $valuearray  = array("10", "11", "12");
    }

    if ($record["list_1"] == "20")
    {
      // NB These vales could be read from a DB table :)
      $optionarray = array("20", "21", "22");
      $valuearray  = array("20", "21", "22");
    }

    $this -> setoptions($optionarray, $valuearray);

    return parent::edit($record, $prefix, $mode);
  }

}

//------------------------------------------------------------------
?>