KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Only load report on button click #2339

Open Mathieu Viennet opened this topic on on Sep 15, 2021 - 2 comments

Mathieu Viennet commented on Sep 15, 2021

Hi again!

How can i load a report ONLY after a form as been submitted...i'm using Dashboard.

I have a date range picker, a text field and a submit button then a DataTables

Right now, i need to give a default value to the Text field, otherwise, my query wont work. I would need the query to execute only after i press submit.

Board:

<?php

use \koolreport\dashboard\Dashboard;
use \koolreport\dashboard\containers\Row;
use \koolreport\dashboard\containers\Panel;
use \koolreport\dashboard\widgets\Text;
use \koolreport\dashboard\containers\Html;

class InventoryProductsSalesBoard extends Dashboard
{
    protected function widgets()
    {
        return [
	        Row::create([
                Panel::create()->width(1/1)
					->sub([ 
						Html::h1("Revenu"),
						Html::h6("Please choose a time period")
					])
					->cssClass("trans-row"),
                ]),

            Row::create()->sub([
                InventoryProductsSalesDateRange::create()->width(1/3),
            ]),
              Row::create()->sub([
                InventoryProductsSalesSearch::create()->width(1/3),
            ]),
                Row::create()->sub([
                InventoryProductsSalesButton::create()->width(1/3),
            ]),

			Row::create([
                Panel::create()->width(1/1)
					->sub([ Html::h2("Revenu details by day") ])
					->cssClass("trans-row")
                ]),
            InventoryProductsSalesTable::create(),
        ];
    }
}

Date picker

<?php

use \koolreport\dashboard\inputs\DateRangePicker;

class InventoryProductsSalesDateRange extends DateRangePicker
{
    protected function onCreated()
    {
        $this->defaultValue($this::thisMonth());
    }
}

Text field

<?php

use \koolreport\dashboard\inputs\TextBox;

class InventoryProductsSalesSearch extends TextBox
{
	  protected function onCreated()
    {
        $this->defaultValue("art-");
    }

}

Button

<?php

use \koolreport\dashboard\inputs\Button;

class InventoryProductsSalesButton extends Button
{

   
    protected function actionSubmit($request, $response)
    {
	$this->sibling("InventoryProductsSalesSearch")->update();
        $this->sibling("InventoryProductsSalesDateRange")->update();
        $this->sibling("InventoryProductsSalesTable")->update();

    }
}

DataTables

<?php

use \koolreport\dashboard\widgets\KWidget;

class InventoryProductsSalesTable extends KWidget
{


    protected function dataSource()
    {
        //Get value from the date range picker
        $range = $this->sibling("InventoryProductsSalesDateRange")->value();
        $search_term = $this->sibling("InventoryProductsSalesSearch")->value();
        $time_1 = DateTime::createFromFormat("Y-m-d H:i:s", $range[0]);
        $time_2 = DateTime::createFromFormat("Y-m-d H:i:s", $range[1]);
		$start = $time_1->format('Y-m-d');
		$end = $time_2->format('Y-m-d');
        //Apply to query

        Return Incognito::procedure()->call("rene_product_sales", [$start, $end, $search_term . "%"])->run(); 

    }

    protected function onCreated()
    {
        $this
        ->use(\koolreport\datagrid\DataTables::class)
        ->settings([
            "options"=>array(
                "paging"=>true,
                "searching"=>true,
                "pageLength"=> 50,               
            ),            
			"columns"=>array(
            "product_name"=>array(
	            "label"=>"Product Name"
            ),
            "_stock"=>array(
	            "label"=>"Current Stock"
            ),
            "_qty"=>array(
	            "label"=>"Quantity sold"
            ),
            "_line_total"=>array(
            	"label"=>"Sales Amount",
				"type"=>"number",
				"prefix"=>"$",
				"decimals"=>2,
        ),
            "_inc_product_status"=>array(
	            "label"=>"Status"
            ))
        ]);
    }
}

Thank you!

KoolReport commented on Sep 17, 2021

You do this:

<?php

use \koolreport\dashboard\inputs\Button;

class InventoryProductsSalesButton extends Button
{

    
   
    protected function actionSubmit($request, $response)
    {
        $this->value("submit"); //<--important

	$this->sibling("InventoryProductsSalesSearch")->update();
        $this->sibling("InventoryProductsSalesDateRange")->update();
        $this->sibling("InventoryProductsSalesTable")->update();

    }
}
<?php

use \koolreport\dashboard\widgets\KWidget;

class InventoryProductsSalesTable extends KWidget
{


    protected function dataSource()
    {
        if($this->sibling("InventoryProductsSalesButton")->value()===null) {
            //Form is not submitted
            $this->hidden(true);
            return [];
        }
        //Get value from the date range picker
        $range = $this->sibling("InventoryProductsSalesDateRange")->value();
        $search_term = $this->sibling("InventoryProductsSalesSearch")->value();
        $time_1 = DateTime::createFromFormat("Y-m-d H:i:s", $range[0]);
        $time_2 = DateTime::createFromFormat("Y-m-d H:i:s", $range[1]);
		$start = $time_1->format('Y-m-d');
		$end = $time_2->format('Y-m-d');
        //Apply to query

        Return Incognito::procedure()->call("rene_product_sales", [$start, $end, $search_term . "%"])->run(); 

    }
    ...
}

Hope that helps.

Mathieu Viennet commented on Sep 17, 2021

Thank you very much again for your help, works perfect!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
solved

Dashboard