KoolReport's Forum

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

Dashboard Admin: How to display Name and pass ID in Create, Update, Detail? #2681

Closed Issac Rosa opened this topic on on May 17, 2022 - 5 comments

Issac Rosa commented on May 17, 2022
<?php
//CourierStopsResource.php

use \koolreport\dashboard\admin\Resource;
use \koolreport\dashboard\fields\ID;
use \koolreport\dashboard\fields\Text;
use \koolreport\dashboard\fields\Date;
use \koolreport\dashboard\fields\Number;

class CourierStopsResource extends Resource
{
    
    protected function onCreated()
    {
        $this->manageTable("mec_courierstops")->inSource(MECLogs::class);
    }

    protected function fields()
    {
        return [
            ID::create("stopNumber")
                ->showOnCreate(false)
                ->showOnIndex(false),
            Date::create("stopDate")
                ->sortable(true)
                ->sort("desc"),
            
            Text::create("locationName")
                ->inputWidget(
                    Select::create()
                    ->dataSource(function(){
                        return MECLogs::table("mec_locations")
                                ->select("locationId,locationName");
                    })
                    ->fields(function(){
                        return [
                            Text::create("locationName")
                        ];
                    })
                ),
                
            Number::create("covidCount")
            ->label("COVID Count"),
            Number::create("utiCount")
            ->label("UTI Count"),
            Text::create("notes")
            ->label("Notes"),
        ];
    }
}

On Resource Table List, I want to see: Stop Date, Location Name, Covid Count, UTI Count, Notes

On Create page, I want to enter: Stop Date, Location ID, Covid Count, UTI Count, Notes

Create page works but on list page, I see the Location Id instead of Location Name. How can I change it to show the Location Name from the Locations table?

KoolReport commented on May 18, 2022

You may have a look at our Order resource inside dashboard demo. The Order resource requires name of customer. Here is the example code. The solution is to provide a join query to the resource like following:

    protected function onCreated()
    {
        $this->manageTable("mec_courierstops")->inSource(MECLogs::class);
    }

    protected function query($query)
    {
        $query->leftJoin("mec_locations","mec_locations.locationId","=","mec_courierstops.locationId")
            ->select("stopNumber","stopDate","locationName","mec_locations.locationId","covidCount","utiCount","notes");
        return $query;
    }

Hope that helps.

Issac Rosa commented on May 18, 2022

Thanks, I'm returning the right data. How do I show the locationName on the table and in the Select option on the Create page but pass the locationId on the Create page. This code is showing and passing the right information on the Create page but not showing the locationName on the List table.

Text::create("locationName")

            ->inputWidget(
                Select::create()
                ->dataSource(function(){
                    return MECLogs::table("mec_locations")
                            ->select("locationId,locationName");
                })
                ->fields(function(){
                    return [
                        Text::create("locationName")
                    ];
                })
            ),
Issac Rosa commented on May 18, 2022

Ok, figured out the list page and the create page. When click the View and Edit for the row, it doesn't show the row details. How do I get that to work?

Issac Rosa commented on May 19, 2022

Still having an issue. So, if I use this code, it shows the correct locationName on the list page but when I try create a new entry, it fails because I need to pass locationId to the table not locationName. But if I change to Number::create("locationId") the create entry works but the list table shows locationId.

How do I show locationName but pass locationId?

Text::create("locationName")

        ->inputWidget(
            Select::create()
            ->dataSource(function(){
                return MECLogs::table("mec_locations")
                        ->select("locationId,locationName");
            })
            ->fields(function(){
                return [
                    Text::create("locationName")
                ];
            })
        ),
Issac Rosa commented on May 20, 2022

Got it working. Added an additional Text field showing on the Index List only.

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
help needed
solved

Dashboard