This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Daniel81
Daniel81

Hi, does anyone know of a way to add custom columns to the List Behavior for plugins?

ie:

I've built a products manager where each product can have a status of either "Live", "Scheduled" or "Draft". There is no "status" field in the DB table for the products as the status is determined by a number of factors based on other field attributes for each of the products - this is a column that I'd just like to add to the Index List to show the status of a product based on some logic I've written which factors in the other attributes. I just need to display the logic outcome in an extra column in the list called "Status".

I've added a "Status" column in the columns.yaml for the Model, I just need to populate it, but I don't know where/how to go about this.

Hope that makes sense :)

Last updated

CNESmeteo
CNESmeteo

Hi Daniel,

I think the most simple way would be to use Laravel accessors.

YAML

    status:
        label: Status
        searchable: true

In your model:

/// @return status
    public function getStatusAttribute()
    {
        $status = '';
        if ($this->whatever){
        // NOTE: being $this the object of your current model
              $status = 'Live';
        }
        return $status;
    }
Daniel81
Daniel81

Thanks @CNESmeteo, that's exactly what I was looking for :)

phplee
phplee

Noticed if i do this you can't search that column if you do it this way. its throws an sql error. is there a way round this

alxy
alxy

Set searchable: false in the columns.yaml for these columns.

phplee
phplee

Hi guys,

I've tried this solution but it effects the form data to i just want to format the columns list. wasn't sure this could be done without a partial for each column.

for example i have a column of integer value of 3 but in the list want it to say 3 months etc

Andrey
Andrey

Hi, all!

I have same problem. I have columns without fields in DB. I fill them in afterFetch(). But I can't sort and search in these columns. Can you help me? Is it possible?

Thanks

alezzzzz
alezzzzz

Same problem here, wish there was an easy solution for sorting and searches using Accesors (getSomethingAttribute) ... any help will be greatly appreciated!!

MarekP
MarekP

The same problem is in my case. If I have 5-10 items I don't care but more than 20-30 items included to the page then is huge mess - completely not practical. Is any one know how to keep all list elements (accordions) INITIALLY collapsed? Cheers

Last updated

Mohsin
Mohsin

alxy said:

Set searchable: false in the columns.yaml for these columns.

Ok, what if the user wanted to search those columns anyway with whatever custom search logic? How would the developer go about doing that?

daftspunky
daftspunky

One option is duplicating the data on the local table and maintaining its state with model events. This makes them searchable

public function afterSave()
{
    $this->parent->searachable_attribute = $this->searachable_attribute;
    $this->parent->save();
}

I have made some complex applications with this pattern with different models calculating local attributes for their relationships. It works well and is a simple solution

1-11 of 11

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.