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

can not create a table with a headers row and add dynamic data to the headers and the data cells #6734

Closed
rezaimn opened this issue May 4, 2020 · 6 comments
Labels
type:question This issue asks a question (how to...).

Comments

@rezaimn
Copy link

rezaimn commented May 4, 2020

Hi. I want to create a table in Ckeditor 5 that must contain a header row and a data row. I have tried editor.execute to create a table with a header row and it was ok, but I don't know how to fill the header and data cells with my data programmatically.

      editor.execute('insertTable', {rows: 2, columns: 4});
      editor.execute('setTableRowHeader');

I also tried TableUtils plugin, I could create a table and fill it with my data, but I don't know how to make my first row as the header with this method.

     const tableUtils = this.editor.plugins.get('TableUtils');
     this.editor.model.change(writer => {
      const table = tableUtils.createTable(writer, 2, result.length);
      result.forEach((column, index) => {
            writer.insertText(`${column.Name}`, {bold: true}, 
            table.getChild(0).getChild(index).getChild(0), 0);
            writer.insertText(`{Tag.Embed.${column.Name}}`, 
            table.getChild(1).getChild(index).getChild(0), 0);
      });
      this.editor.model.insertContent(table);
    });

Is there any solution to fix each of the above ways I have tried?
thank you.

@rezaimn rezaimn changed the title can not create a table with a header row and add dynamic data to the header and the cells can not create a table with a headers row and add dynamic data to the headers and the data cells May 4, 2020
@Mgsy
Copy link
Member

Mgsy commented May 5, 2020

@jodator, can you take a look at it?

@rezaimn
Copy link
Author

rezaimn commented May 5, 2020

@jodator, can you take a look at it?

Sorry, Can I take a look at what?

@jodator
Copy link
Contributor

jodator commented May 5, 2020

Sorry, Can I take a look at what?

I will look at this - @Mgsy was pinging me here :)

@rezaimn
Copy link
Author

rezaimn commented May 5, 2020

Sorry, Can I take a look at what?

I will look at this - @Mgsy was pinging me here :)

Great, Thanks

@jodator
Copy link
Contributor

jodator commented May 6, 2020

@rezaimn You can do it like this:

const model = editor.model;

model.change( writer => {
	// Create a table.
	const table = tableUtils.createTable( writer, 2, 2 );
	// Set it's "headingRows" attribute to 1.
	writer.setAttribute( 'headingRows', 1, table );

	// To simplify we'll take the first (only) data row.
	const dataRow = table.getChild( 1 );

	for ( const tableCell of dataRow.getChildren() ) {
		// Each created table cell have an empty paragraph as a child.
		const paragraph = tableCell.getChild( 0 );

		// Crete a content to insert - here just "foo" text.
		const text = writer.createText( 'foo' );

		// Insert text to a paragraph.
		const insertPosition = writer.createPositionAt( paragraph, 0 );
		writer.insert( text, insertPosition );
	}

	// Use model.insertContent() to insert a created content.
	// It will insert table at a current document selection position.
	model.insertContent( table );
} );

@Mgsy Mgsy added pending:feedback This issue is blocked by necessary feedback. type:question This issue asks a question (how to...). labels May 6, 2020
@rezaimn
Copy link
Author

rezaimn commented May 6, 2020

@rezaimn You can do it like this:

const model = editor.model;

model.change( writer => {
	// Create a table.
	const table = tableUtils.createTable( writer, 2, 2 );
	// Set it's "headingRows" attribute to 1.
	writer.setAttribute( 'headingRows', 1, table );

	// To simplify we'll take the first (only) data row.
	const dataRow = table.getChild( 1 );

	for ( const tableCell of dataRow.getChildren() ) {
		// Each created table cell have an empty paragraph as a child.
		const paragraph = tableCell.getChild( 0 );

		// Crete a content to insert - here just "foo" text.
		const text = writer.createText( 'foo' );

		// Insert text to a paragraph.
		const insertPosition = writer.createPositionAt( paragraph, 0 );
		writer.insert( text, insertPosition );
	}

	// Use model.insertContent() to insert a created content.
	// It will insert table at a current document selection position.
	model.insertContent( table );
} );

Thank you so much.

@Mgsy Mgsy closed this as completed May 6, 2020
@Mgsy Mgsy removed the pending:feedback This issue is blocked by necessary feedback. label May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question This issue asks a question (how to...).
Projects
None yet
Development

No branches or pull requests

3 participants