Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to add row in a loop to existing already-rendered table #166

Open
1 task done
slaFFik opened this issue Sep 28, 2023 · 3 comments
Open
1 task done

Add ability to add row in a loop to existing already-rendered table #166

slaFFik opened this issue Sep 28, 2023 · 3 comments

Comments

@slaFFik
Copy link
Contributor

slaFFik commented Sep 28, 2023

Feature Request

Describe your use case and the problem you are facing

I want to "register" an empty table, and then later in a loop using table reference ($table) add rows.

Right now if I register a table, call the display() method to render it, and then later call the

$table->addRow(); 
$table->display();

it will render the whole table again - with headers and previously added rows.

Describe the solution you'd like

I think separating resetTable() method into individual methods for header, rows, footer is needed.

So I should be able to either do this:

$table->setHeaders();
$table->display();

foreach ($rows as $row) {
	$table->resetRows();
	$table->addRow($row);
	$table->display();
}

or something like this:

$table->setHeaders();
$table->display();

foreach ($rows as $row) {
	$table->displayRow($row);
}

Also, because header is a mandatory table data, displaying a new row should not render the top border but render the bottom row.

@slaFFik slaFFik changed the title Introduce Table::resetRows() Add ability to add row in a loop to existing already-rendered table Sep 28, 2023
@slaFFik
Copy link
Contributor Author

slaFFik commented Sep 28, 2023

My dumb proof of concept (just to continue working on my package):

use cli\Streams;
use cli\Table;

class DynamicTable extends Table {

	public function displayRow( $row ) {

		$this->_renderer->setWidths( $this->_width, $fallback = true );
		$border = $this->_renderer->border();

		$out = [];

		$row = $this->_renderer->row( $row );
		$row = explode( PHP_EOL, $row );
		$out = array_merge( $out, $row );

		if ( isset( $border ) ) {
			$out[] = $border;
		}

		foreach ( $out as $line ) {
			Streams::line( $line );
		}
	}
}

@danielbachhuber
Copy link
Member

Seems fine to me, as long as there aren't any backcompat breaks. Feel free to submit a PR if you'd like.

@slaFFik
Copy link
Contributor Author

slaFFik commented Sep 29, 2023

Awesome, @danielbachhuber. I will do that later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants