You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The example demonstrate how to build a dynamic reports with multiple data filters. In this example, the data selection can be filter by `year`, `productLine` and `customerName`. Those data filters can be multi-selected meaning that you can select more than 1 years, more than 1 product lines or more than 1 customer to view.
2
+
3
+
The example use `Select2` from [inputs package](https://www.koolreport.com/packages/inputs) to construct the parameters selection.
4
+
5
+
We use the [Row Group] feature of Table for better data visualization. This grouping features support unlimited levels of grouping. There is very limited data table in market supporting this features. This feature is totally free with KoolReport.
6
+
7
+
__Code Explanation__:
8
+
9
+
The report use `\koolreport\inputs\Bindable` and `\koolreport\inputs\POSTBinding` services which will allows data binding between the report parameters and inputs controls. So the selection from select2 controls like years,productLines and customerNames will be bound to the corresponding report params.
10
+
11
+
We use the `defaultParamValues()` methods in the report to define default starting selection of users. As you can see from the code, we pre-select the year `2003`. You may add different year, or set preselected for customerNames or productLines
12
+
13
+
We use the `bindParamsToInputs()` to bind the name of report parameters to the name of the input controls. To keep things simple, the report parameters and name of input controls are the same:
14
+
15
+
```
16
+
protected function bindParamsToInputs()
17
+
{
18
+
return array(
19
+
"years",
20
+
"customerNames",
21
+
"productLines"
22
+
);
23
+
}
24
+
```
25
+
26
+
It is equivalent to
27
+
28
+
```
29
+
protected function bindParamsToInputs()
30
+
{
31
+
return array(
32
+
"years"=>"years",
33
+
"customerNames"=>"customerNames",
34
+
"productLines"=>"productLines"
35
+
);
36
+
}
37
+
```
38
+
39
+
In `setup()` function, base on the selection of users we add custom condition to the SQL query. Below code means that do not add the year condition if user does not select year.
40
+
41
+
```
42
+
".(($this->params["years"]!=array())?"and YEAR(orderDate) in (:years)":"")."
43
+
```
44
+
45
+
And also, we have this line of code:
46
+
47
+
```
48
+
$query_params = array();
49
+
if($this->params["years"]!=array())
50
+
{
51
+
$query_params[":years"] = $this->params["years"];
52
+
}
53
+
```
54
+
55
+
meaning that if user select year, then we add the year to variable `$query_params` to be used as parameters for sql query.
56
+
57
+
In the view, we have some advance code for `Select2` widget:
58
+
59
+
```
60
+
<?php
61
+
Select2::create(array(
62
+
"multiple"=>true,
63
+
"name"=>"years",
64
+
"dataSource"=>$this->src("automaker")->query("
65
+
select YEAR(orderDate) as year
66
+
from orders
67
+
group by year
68
+
"),
69
+
"attributes"=>array(
70
+
"class"=>"form-control"
71
+
)
72
+
));
73
+
?>
74
+
```
75
+
76
+
As you may notice, in `"dataSource"` of Select2 we use directly SQL command to query the available year.
77
+
78
+
So as user selects years, productLines and customerNames, we will execute and store result to `"orders"` dataStore later be visualized in `Table`.
0 commit comments