PostTypes icon indicating copy to clipboard operation
PostTypes copied to clipboard

Add default column populate method?

Open iwillhappy1314 opened this issue 4 years ago • 2 comments

In my usecase, i added serval post metas in columns, is there any way to set default column polulate method? so we can keep the code more dry?

maybe add a method populateDefault like the code below?

$client = new PostType('client');

$client->columns()
     ->hide([
         'title',
         'date',
     ])
     ->add([
         '_name'      => __('Name'),
         '_deal_date' => __('Deal time'),
        '_is_dealed'     => __('Price'),
         '_age'       => __('Age'),
         '_phone'     => __('Phone'),
         '_price'     => __('Price'),
     ])
    ->populateDefault(function ($column, $post_id)
    {
        echo get_post_meta($post_id, $column, true);
    })
   ->populate('_is_dealed', function ($column, $post_id)
     {
         $deal_price = get_post_meta($post_id, 'deal_price');

         echo ($deal_price) ? '<span class="is-success">Dealed</span>' : '<span class="is-default">not dealed</span>';
     });

iwillhappy1314 avatar Oct 09 '20 09:10 iwillhappy1314

Or instead have the populate method accept an array of column keys as well.

public function populate($columns, $callback)
{
    $columns = (array)$columns;
    foreach ($columns as $column) {
        $this->populate[$column] = $callback;
    }

    return $this;
}

dweipert-3138720606 avatar Dec 17 '20 23:12 dweipert-3138720606