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 /

Searchable ComplexTableFields in the CMS (New Module)


Reply


1554 Views

Avatar
iculshaw

Community Member, 13 Posts

24 December 2008 at 12:22pm

Edited: 24/12/2008 12:35pm

This is intended as a step through exercise by using the following attached files and altering specific things along the way, i would recommend downloading the attached files and placing them into a module-like structure before continuing

To do this you need to extend the default ComplexTableField class, The following example is a test class i have created and works with both Silverstripe versions 2.2.2 and 2.2.3.

NOT 2.3

The way i have implemented it was through creating a new module (in this example) for a table of products; the way i constructed the new module was through the [url=http://doc.silverstripe.com/doku.php?id=private:tutorial:creating-a-module]creating a modules recipe[/url]

Firstly when extending the ComplexTableField you have to make sure you have a new Template (which will contain the search form), and javascript included as a Requirement::javascript(); in the __construct function.

Database Definition for this example

<?php

class Product extends DataObject {

   static $db = array(
      'Title' => 'Varchar',
      'Description' => 'Text',
      'Code' => 'Varchar(50)',
      'Price' => 'Currency',
      'SerialNo' => 'Varchar'      
   );
   
}

Directory Structure

[products
\
\_code
|
|_css
|
|
|_javascript
|
|
|_templates

CTF File

There are a few things you will notice when looking at the attached ProductCTF.php are the static variables:

  • template (line 5)
  • popup_class (line 6)
  • searchable_fields (line 8)

Firstly the template and popup_class is described further below in this document; secondly and importantly the $searchable_fields array is a list of fields that you define in your CTF that can be searched within the CTF, in this instance the fields are Title, Code and SerialNo; feel free to change these to any of the fields you wish to search by in your CTF.

line 21-22

$SQL_search = isset($_REQUEST['ProductSearch']) ? Convert::raw2sql($_REQUEST['ProductSearch']) : null;
      if(!empty($_REQUEST['ProductSearch'])) {

You'll notice here the ProductSearch request variable, this is set in the function below SearchForm() on line 32. Any changes you make to these parts should reflect each other otherwise your filter won't interpret what you put in the searchbox.

Line 38 describes the actual filter button, i have made slight amendments to this file for use with multiple searchable complextablefields specifically at the point of:

... value="'._t('ProductCTF.FILTER', 'Filter').'" id="'.$this->name.'FilterButton">');

$this->name

This gets the name of the CTF (the 2nd parameter of your definition in the fieldlist) and adds it to the filter button.

Finally on line 40 you will see a FieldContainer with two commented out lines, in the MemberTableField it had different types of fields to sort and organise the CTF by which i didnt require and aren't styled very pretty so i skipped these out, to enable these again please see MemberTableField.php in cms/code/MemberTableField.php

CTF Javascript

Code attached: ProductFilter2.js (has additional comments to help you out)

CTF Template

The template that i created is called ProductCTF.ss and is defined on line 5 of ProductCTF.php (attached). This can be changed to any template name as long it hasthe following differences from the normal ComplexTableField.ss; the difference is with the addition of

<div class="ProductFilter">
   $SearchForm
</div>

This goes (on line 2) after:

<div id="$id" class="$Classes">

[

The Main Module File

This file contains the "fieldlist" definition to show what fields should be shown in the right-hand form of the module. For the ProductAdmin.php my FieldList looks like this:

$fields = new FieldSet(
         new TabSet('Root',
            new Tab('All Products',
               
               new ProductCTF(
                  $this,
                  'ProductsComplexTableField',
                  'Product',
                  array(
                     'Title' => 'Title',
                     'Description' => 'Description',
                     'Code' => 'Product Code',
                     'Price' => 'Product Price',
                     'SerialNo' => 'Serial Number'   
                  )
               )
            )
         )
      );

I hope this helps anyone trying to accomplish a searchable CTF and soon i will post how to have multiple searchable complex table fields in one fieldset form.

Attached Files