Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

Treemultiselect field and List box ( multiple select )


Reply


2 Posts   1564 Views

Avatar
Arun

Community Member, 8 Posts

23 February 2010 at 5:58pm

Hi All,

In one of my form ( a separate module) i have one Treemultipleselect field. In this field i have to show the Site Contents Tree Structure. Also i have to insert the selected page ids in to the db and while retrieving the inserted record i have to show the selected ids are checked. How can achieve this...?

Also in the same module i have one List box ( multiple select ). After insertion I have checked in the db and found that for the listbox field its shown as "Array". So my question is how to insert the selected listbox items in to db and how we can show its selected when we retrieve this record for editing...?

The above 2 questions are almost same... but i really need a help for the first one. I think any of you have the same issue and sorted out. If so please help me for implementing the above two....

Any help would be much appreciated....

Thanks
Arun.

Avatar
Katy

Community Member, 4 Posts

15 December 2010 at 9:04am

I solved this as follows:

I wanted to allow registrants members to indicate which key "Regions" they were interested in.

In the form I included this code to initialise the list box:

      $member = Member::CurrentMember();

      // initialise field values so already selected regions are shown in list box
      if($member) {

         // initialise Region Values
         $wherevalue = 'Member_Regions.MemberID=' . $member->ID;
         $memberregions = DataObject::get('Region',$wherevalue,'',"LEFT JOIN `Member_Regions` ON `Region`.`ID` = `Member_Regions`.`RegionID`");
         $memberregionsIDs = array();
         if($memberregions) {
            foreach ($memberregions as $memberregion) {
               $memberregionsIDs[] = (string)$memberregion->ID;
            }
         }
      } else {
            $memberregionsIDs[] = '';
      }

      // Initialise Region Title
      $regionTitle = 'Select one or more Regions below:';
      
      // create field set
      $region = DataObject::get('Region');
      if ($region) {
         $regionsize = ($region->Count()+1);
         $region = $region->toDropdownMap('ID', 'Title', '(Select one or more options)', true);
      }

      $fields->push(new ListBoxField(
         $name = 'regionIDs',
         $title = $regionTitle,
         $source = $region,
         $value = $memberregionsIDs,
         $size = 10,
         $multiple = true
      ));

The form data selected in the list box was then processed as follows:

      // Create a new Member object and load the form data into it
      $newmember = new Member();

      // Check if new entry is for logged-in member
      if($member = Member::CurrentMember()) {
         if($newmember->ID = $member->ID) {
            $newmember = $member;
         }
      }

         $regions = $data['regionIDs'];

         // remove existing Regions for this Member
         
         $wherevalue = 'Member_Regions.MemberID=' . $newmember->ID;
         $memberregions = DataObject::get('Region',$wherevalue,'',"LEFT JOIN `Member_Regions` ON `Region`.`ID` = `Member_Regions`.`RegionID`");
         if($memberregions) {
            foreach ($memberregions as $memberregion) {
                  $newmember->Regions()->remove($memberregion);
            }
         }

         // add new Regions for this Member

         if($regions) {
            foreach ($regions as $region) {
                  $regionID = (int)$region;
                  $newmember->Regions()->add($regionID);
            }
         }      
         
         $newmember->write();