Jump to:

5521 Posts in 1733 Topics by 1220 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Searchable ComplexTableFields in the CMS (New Module)

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1473 Views
  • iculshaw
    Avatar
    Community Member
    13 Posts

    Searchable ComplexTableFields in the CMS (New Module) Link to this post

    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 creating a modules recipe

    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.

    1473 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.