Table
Overview #
Table is the most basic and most used widget to represent data. The widget will need you to provide data source and list of fields that you want to show on table.
Example:
<?php
use \koolreport\dashboard\widgets\Table;
use \koolreport\dashboard\fields\Number;
use \koolreport\dashboard\fields\Text;
use \koolreport\dashboard\fields\Currency;
class SaleTable extends Table
{
protected function dataSource()
{
return AutoMaker::table("sales")
->select("id","customerName","amount");
}
protected function fields()
{
return [
Number::create("id"),
Text::create("customerName"),
Currency::create("amount")->USD()->symbol()
];
}
}
Properties #
Name | type | default | description |
---|---|---|---|
tableHover | bool | false | Get/set whether table is able to be hovered, default is false |
tableOutline | bool | false | Get/set where table is outline, default is false |
tableStriped | bool | false | Get/set whether table shows stripped rows, default is false |
tableSmall | bool | false | Get/set whether table is shown in small size, default is false |
tableBordered | bool | false | Get/set whether table has borders, default is false |
tableResponsive | bool | true | Get/set whether table is responsive, default is true |
headerDark | bool | false | Get/set whether header is on dark mode, default is false |
hidePagingOnSinglePage | bool | false | Get/set whether Table will hide paging when table has only 1 page |
pageSize | int | null | Get/set the number of row on each page |
pageSizeOptions | array | null | Get/set the options for user to selet in pagesize. The default value is null and this feature is disabled. |
pageIndex | int | null | Get/set the current page index |
pageAlign | string | "left" | Get/set position of paging bar, accept "left" , "center" , "right" |
showFooter | bool | false | Get/set whether footer is shown |
autoFields | bool | true | Get/set whether auto fields should be used when no settings for fields() |
searchable | bool | false | Get/set whether search box is show and table can be searched |
searchFields | array | null | Get/set list of fields that are searchable |
searchAlign | string | "right" | Get/set alignment of search box, accept "left" , "center" , "right" |
searchText | string | null | Get/set the search text |
searchWidth | string | "200px" | Get/set with of search box |
Note: Those above properties follows this code rules.
Example:
<?php
use \koolreport\dashboard\widgets\Table;
class SaleTable extends Table
{
protected function onInit()
{
$this->tableHover(true)
->tableOutline(true)
->tableSmall(true)
->tableBordered(true)
->tableResponsive(true)
->headerDark(true)
->pageSizeOptions([10,50,100])
->pageSize(10)
->pageIndex(2)
->pageAlign("right")
->showFooter(true)
->autoFields(true);
}
}
Methods #
Name | return | description |
---|---|---|
dataView() | DataView | Get data view object of current data, the object contains properties data , fields , aggregates , count and paging to let you have data of table. |
getRowSelectField() | RowSelect | Get the row select field object |
getRowSelectedConditions() | array | List of selected result from user presented in an array form |
Example:
<?php
use \koolreport\dashboard\inputs\Button;
use \koolreport\dashboard\notifications\Note;
class MySubmitButton extends Button
{
protected function actionSubmit($request, $response)
{
$table = $this->sibling("MyTable");
$selectedConditions = $table->getRowSelectedConditions();
//Get all selected rows from users
$selectedCustomers = AutoMaker::table("customers")->applyContext($selectedConditions)->run();
foreach($selectedCustomers as $customer) {
// Take action for each user
}
return Note::success("Actions for selected customers has been completed");
}
}
Table Fields #
Beside the list of common fields that can be used with table. Below are special fields for Table only
Badge #
Name | type | default | description |
---|---|---|---|
text | string/function | Get/set the text inside badge | |
type | string/function | "primary" | Get/set type of badge, accept "primary" , "secondary" , "success" , "danger" , "warning" , "info" |
cssStyle | string/function | Get/set extra css style for badge, accept string or function |
Example:
Badge::create("amount_status")
->text(function($value, $row){
return $row["amount"]>1000?"Good":"Bad";
})
->type(function($value, row){
return $row["amount"]>1000?"success":"waring";
})
->cssStyle("font-weight:bold;")
Button #
Settings #
Name | type | default | description |
---|---|---|---|
text | string | "Submit" | Get/set the text inside button |
type | string | "primary" | Get/set the button type, accept "primary" , "info" , "danger" , "success" |
size | string | "md" | Get/set the size of button, accept "sm" , "md" , "lg" |
outline | boolean | false | Get/set the whether button is outline |
onClick | mixed | Get/set client action on click event, more details | |
disabled | boolean | false | Get/set whether button is disabled |
blockLevel | boolean | false | Get/set whether button take full size of available space |
attributes | array | Get/set the extra attributes to <button> element |
Note: Those above properties follows this code rules.
Click Event #
onClick is client-side event when user clicks to button. The property can accept string or function.
Example:
//You can run any javascript function
Button::create()
->onClick(function($value, $row){
return "alert('".$row["customerName"]."')";
})
use \koolreport\dashboard\Client;
//Button that will load UserDetail dashboard when click, it will transmit the user id as parameter.
Button::create()
->onClick(function($value, $row){
return Client::dashboard("UserDetail")->load(["userId"=>$row["userId"]]);
})
//If you want to trigger action of any widget you can do:
Button::create()
->onClick(function($value, $row){
return Client::widget("table")->action("index",["userId"=>$row["userId"]]);
})
Icon #
Name | type | default | description |
---|---|---|---|
icon | string/function | Get/set the icon | |
type | string/function | Get/set type of icon, accept "primary" , "secondary" , "danger" , "success" , "info" , "warning" | |
cssStyle | string/function | Get/set the css style of icon |
Note: Those above properties follows this code rules.
Example:
Icon::create()
->icon("fa fa-settings")
->type("secondary")
->cssStyle("font-size:16px")
//With function
Icon::create()
->icon(function($value, $row) {
return "fab fa-cc-".$row["payment-method"]; // "fab fa-cc-visa" | "fab fa-cc-master"
})
Image #
Name | type | default | description |
---|---|---|---|
width | string | Get/set width of image | |
height | string | Get/set height of image | |
rounded | boolean | false | Get/set whether image is rounded at corner |
roundedCircle | boolean | false | Get/set whether image is circle |
thumbnail | boolean | Get/set whether image is contained in thumbnail | |
responsive | boolean | Get/set whether image is responsive | |
cssClass | string | Get/set extra css class for image | |
cssStyle | string | Get/set extra css style for image | |
squared([string $size]) | Make image appear in square, default size is 32px | ||
circle([string $diameter]) | Make image appear in circle, default diameter is 32px |
Note: Those above properties follows this code rules.
Example:
//Prefix url location to image file
Image::create("imageFile")
->prefix("https://www.example.com/images/")
Image::create("imageFile")
->formatUsing(function($imageFile){
return "https://www.example.com/images/"+$imageFile;
})
//Resolve url using other columns
Image::create()
->resolveUsing(function($originalValue, $row){
return "https://www.example.com/images/".$row["folder"]."/".$row["date"]."jpg";
});
//Make squared image and rounded conner
Image::create("imageUrl")
->squared("64px")
->rounded(true)
//Make circle image with thumbnail
Image::create("imageUrl")
->circle("64px")
->thumbnail(true)
Progress #
Name | type | default | description |
---|---|---|---|
type | string/function | Get/set type of progress, accept "primary" , "secondary" , "danger" , "success" , "info" , "warning" | |
cssStyle | string/function | Get/set css style applied to progress | |
range | array/function | Get/set range for color customization |
Note: Those above properties follows this code rules.
Example:
//Normally created
Progress::create("progress")
->type("success")
->cssStyle("margin-left:10px;")
//Customize type
Progress::create("progress")
->type(function($value){
if($value>50) {
return "success";
}
return "danger";
})
// Use range to setup range, the progress show
// red when value is smaller than 20, "warning" when value < 50
// and "success" when value is greater than 80.
Progress::create("progress")
->range([
"danger"=>20,
"warning"=>50,
"success"=>80
])
Sparkline #
Name | type | default | description |
---|---|---|---|
type | string/function | Get/set type of sparkline, accept "bar" , "box" , "tristate" , "bullet" , "line" , "pie" | |
dataSource | DataSource | Get/set the source for sparkline |
Note: Those above properties follows this code rules.
Example:
Sparkline::create("paymentHistory")
->colName("customerNumber")
->type("bar")
->dataSource(function(){
return AutoMaker::table("payments")
->where("customerNumber",$this->value())
->select("amount");
}),
Searchable #
To turn on the searchable
feature of Table , you only need to set ->searchable(true)
like following:
...
protected function onCreated()
{
$this
->searchable(true) //Turn on searchable feature
->searchWidth("200px") //Set size of search box
->searchText(null) //Pre set the search text
->searchAlign("left") //Search box will align left
->searchFields(["country","city"]); //List of fields that used to search.
}
...
Alternatively to set list of fields to be searchable, you can mark those fields inside fields()
like following:
...
protected function fields()
{
return [
Text::create("country")
->searchable(true),
Text::create("city")
->searchable(true)
];
}
...
State persistent #
Table has 3 state persistent elements:
- Field's sort: The field sort always reserved is direction during ajax post back.
- Field's filter: The field's filter always reserved its value during ajax post back.
- Page index: Preserve the index if the
pageIndex
is not null. So to turn on this persistence feature, try to set->pageIndex(0)
ononCreated
event for example.
Traits #
Table has been provided with following traits:
- TAppLink: Able to refer to application with
app()
method - TDashboadLink: Able to refer to parent dashboard with
dashboard()
method - TEnabledPermit: Use
enabled()
andenabledWhen()
to set permission - TParams: Able to get/set parameters with
params()
method - TWidgetState: Able to get/set persisted state for widget
- TParamsPersisted: Able to make params set to widget persisted
- TDataSource: Able to receive datasource via
dataSource()
method - TFields: Able to receive fields via
fields()
method - TDetailAction: Able to open detail modal
- TExportable: Able to export widget to different formats
Get started with KoolReport
KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.