Development

/plugins/sfGridPlugin/trunk/lib/grid/sfWebGridJavaScript.class.php

You must first sign up to be able to contribute.

root/plugins/sfGridPlugin/trunk/lib/grid/sfWebGridJavaScript.class.php

Revision 29131, 3.3 kB (checked in by Leon.van.der.Ree, 3 years ago)

updated interfaces of grids, adding routing-class for grids

Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Leon van der Ree <leon@fun4me.demon.nl>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * This abstract class is there as a base for your JavaScript-Grid class.
13  * You should define the JavaScript formatter in the specialised class
14  *
15  * All JavaScript Grids "should" still render to HTML, while unobtrusive JavaScript should progressive enhance the intactivity of your grid,
16  * To dynamically load new content a dataFormatter can be defined to output JSON or XML-data. 
17  *
18  */
19 abstract class sfWebGridJavaScript extends sfWebGrid
20 {
21   /**
22    * formatter to output data (json/xml)
23    *
24    * @var sfGridFormatterInterface
25    */
26   protected $dataFormatter;
27
28   /**
29    * formatter that returns (unobtrusive) JavaScript
30    *
31    * @var sfGridFormatterInterface
32    */
33   protected $javaScriptFormatter;
34  
35   /**
36    * This method should be implemented in your specialised GridClass, to define
37    * the dataFormatter and javaScriptFormatter
38    */
39   public function configure()
40   {
41     //set html formatter
42     parent::configure();
43     
44     // define the data formatter
45     $this->setDataFormatter(new sfGridFormatterJson($this));
46     
47     // $this->setJavaScriptFormatter(new sfGridFormatterYOUR_JS_FORMATTER($this));
48   }
49  
50   /**
51    * returns the DataFormatter (to format data in json/xml)
52    *
53    * @return sfGridFormatterInterface
54    */
55   public function getDataFormatter()
56   {
57     return $this->dataFormatter;
58   }
59
60   /**
61    * Sets the data formatter that should be used to render the data of the grid, E.G. in json or xml.
62    *
63    * @param  sfGridFormatterInterface $formatter A Data Formatter
64    */
65   public function setDataFormatter(sfGridFormatterInterface $formatter)
66   {
67     $this->dataFormatter = $formatter;
68   }
69  
70     public function renderData()
71   {
72     // set default sort-column, if set
73     if (!$this->getSortColumn() && $this->defaultSortColumn)
74     {
75       $this->setSort($this->defaultSortColumn, $this->defaultSortOrder);
76     }
77
78     // update offset lazy, now is a good time to request last page and check if we don't requested a higher pager
79     $this->getDataSource()->setOffset($this->getPager()->getFirstIndex());
80
81     if ($this->getDataFormatter() === null)
82     {
83       throw new LogicException('A Data formatter must be set before calling renderData()');
84     }
85
86     return $this->getDataFormatter()->render();
87   }
88  
89  
90   /**
91    * returns the JavaScriptFormatter (to format (the structure) in (unobstrusive) JavaScript)
92    *
93    * @return sfGridFormatterInterface
94    */
95   public function getJavaScriptFormatter()
96   {
97     return $this->javaScriptFormatter;
98   }
99
100   /**
101    * Sets the JavaScript formatter that should be used to render the grid in JavaScript.
102    *
103    * @param  sfGridFormatterInterface $formatter A JavaScriptFormatter
104    */
105   public function setJavaScriptFormatter(sfGridFormatterInterface $formatter)
106   {
107     $this->javaScriptFormatter = $formatter;
108   }
109  
110  
111   /**
112    * Renders static JavaScript (not based on rows)
113    *
114    * return string
115    */
116   public function renderJavaScript()
117   {
118     if ($this->getJavaScriptFormatter() === null)
119     {
120       throw new LogicException('A JavaScript formatter must be set before calling renderJavaScript()');
121     }
122
123     return $this->getJavaScriptFormatter()->render();
124   }
125 }
Note: See TracBrowser for help on using the browser.