How to display the table headers in a row, in angular material table?












3















My Material table displays the value in below manner:



Name |quatity



Mango|10



    <div class="table-cover center-table">
<div class="mat-elevation-z8 elevation-scroll-control">
<table mat-table class="full-width-table left-margin" [dataSource]="dataSource" matSort aria-label="Elements">

<!-- Name Column -->
<ng-container matColumnDef="name">
<td mat-header-cell *matHeaderCellDef >Name</td>
<td mat-cell *matCellDef="let row" class="">
<div class="cell-style padding-left">{{row.Name}}</div>
</td>
</ng-container>

<!-- quantity Column -->
<ng-container matColumnDef="quantity">
<td mat-header-cell *matHeaderCellDef>quantity</td>
<td mat-cell *matCellDef="let row">
<div class="cell-style">{{row.quantity}}</div>
</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"> </tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"> </tr>
</table>

</div>
</div>


But I want to display the value like below:



Name | Mango



quatity | 10



How can I achieve this? any help is much appreciated










share|improve this question

























  • Arrange you element and data as given in this link w3.org/WAI/tutorials/tables/irregular

    – Vijay Sankhat
    Nov 27 '18 at 5:44











  • can you please create a stackblitz demo for the problem ?

    – Abhishek Kumar
    Nov 27 '18 at 10:12
















3















My Material table displays the value in below manner:



Name |quatity



Mango|10



    <div class="table-cover center-table">
<div class="mat-elevation-z8 elevation-scroll-control">
<table mat-table class="full-width-table left-margin" [dataSource]="dataSource" matSort aria-label="Elements">

<!-- Name Column -->
<ng-container matColumnDef="name">
<td mat-header-cell *matHeaderCellDef >Name</td>
<td mat-cell *matCellDef="let row" class="">
<div class="cell-style padding-left">{{row.Name}}</div>
</td>
</ng-container>

<!-- quantity Column -->
<ng-container matColumnDef="quantity">
<td mat-header-cell *matHeaderCellDef>quantity</td>
<td mat-cell *matCellDef="let row">
<div class="cell-style">{{row.quantity}}</div>
</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"> </tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"> </tr>
</table>

</div>
</div>


But I want to display the value like below:



Name | Mango



quatity | 10



How can I achieve this? any help is much appreciated










share|improve this question

























  • Arrange you element and data as given in this link w3.org/WAI/tutorials/tables/irregular

    – Vijay Sankhat
    Nov 27 '18 at 5:44











  • can you please create a stackblitz demo for the problem ?

    – Abhishek Kumar
    Nov 27 '18 at 10:12














3












3








3


1






My Material table displays the value in below manner:



Name |quatity



Mango|10



    <div class="table-cover center-table">
<div class="mat-elevation-z8 elevation-scroll-control">
<table mat-table class="full-width-table left-margin" [dataSource]="dataSource" matSort aria-label="Elements">

<!-- Name Column -->
<ng-container matColumnDef="name">
<td mat-header-cell *matHeaderCellDef >Name</td>
<td mat-cell *matCellDef="let row" class="">
<div class="cell-style padding-left">{{row.Name}}</div>
</td>
</ng-container>

<!-- quantity Column -->
<ng-container matColumnDef="quantity">
<td mat-header-cell *matHeaderCellDef>quantity</td>
<td mat-cell *matCellDef="let row">
<div class="cell-style">{{row.quantity}}</div>
</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"> </tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"> </tr>
</table>

</div>
</div>


But I want to display the value like below:



Name | Mango



quatity | 10



How can I achieve this? any help is much appreciated










share|improve this question
















My Material table displays the value in below manner:



Name |quatity



Mango|10



    <div class="table-cover center-table">
<div class="mat-elevation-z8 elevation-scroll-control">
<table mat-table class="full-width-table left-margin" [dataSource]="dataSource" matSort aria-label="Elements">

<!-- Name Column -->
<ng-container matColumnDef="name">
<td mat-header-cell *matHeaderCellDef >Name</td>
<td mat-cell *matCellDef="let row" class="">
<div class="cell-style padding-left">{{row.Name}}</div>
</td>
</ng-container>

<!-- quantity Column -->
<ng-container matColumnDef="quantity">
<td mat-header-cell *matHeaderCellDef>quantity</td>
<td mat-cell *matCellDef="let row">
<div class="cell-style">{{row.quantity}}</div>
</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"> </tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"> </tr>
</table>

</div>
</div>


But I want to display the value like below:



Name | Mango



quatity | 10



How can I achieve this? any help is much appreciated







angular angular-material angular7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 6:40







Rajath

















asked Nov 27 '18 at 5:19









RajathRajath

8410




8410













  • Arrange you element and data as given in this link w3.org/WAI/tutorials/tables/irregular

    – Vijay Sankhat
    Nov 27 '18 at 5:44











  • can you please create a stackblitz demo for the problem ?

    – Abhishek Kumar
    Nov 27 '18 at 10:12



















  • Arrange you element and data as given in this link w3.org/WAI/tutorials/tables/irregular

    – Vijay Sankhat
    Nov 27 '18 at 5:44











  • can you please create a stackblitz demo for the problem ?

    – Abhishek Kumar
    Nov 27 '18 at 10:12

















Arrange you element and data as given in this link w3.org/WAI/tutorials/tables/irregular

– Vijay Sankhat
Nov 27 '18 at 5:44





Arrange you element and data as given in this link w3.org/WAI/tutorials/tables/irregular

– Vijay Sankhat
Nov 27 '18 at 5:44













can you please create a stackblitz demo for the problem ?

– Abhishek Kumar
Nov 27 '18 at 10:12





can you please create a stackblitz demo for the problem ?

– Abhishek Kumar
Nov 27 '18 at 10:12












1 Answer
1






active

oldest

votes


















1














You may do that by:





  1. Transposing your data from columns to row:



    Before:
    Mango |10
    Banana|5



    After:
    Mango|Banana
    10|5




  2. Including the labels as the first column of your data:



    Name|Mango|Banana
    Quantity|10|5



  3. Adjust your table to display columns dynamically (so that you may have any number of columns), as on this example from angular material table documentation:



<table mat-table [dataSource]="data" class="mat-elevation-z8">
<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef> {{column}} </th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>



  1. If not required you may remove the header row



  2. If you want to format the first column, you may use some css as:



    td.mat-cell:nth-child(1) {
    // formatting
    }



  3. Also, if required, you may use property sticky of MatColumnDef to keep the first column sticky (in this case you may want to have the first column separate from the dynamic *ngFor loop, so that you may easily have only the first column sticky)



I've created a working stackblitz example






share|improve this answer


























  • Thank you for your response @GCSDC. I tried your solution but it still not displaying the expected result. Can please alter my code for better understanding.

    – Rajath
    Nov 27 '18 at 9:02











  • @Rajath I've included a working stackblitz example on my answer. Please let me know if this solves the issue.

    – GCSDC
    Nov 27 '18 at 16:53











  • Thank you very much! One more concern - if we do like that, then it becomes very difficult to update the data in the table(database) right? How we can address this issue.

    – Rajath
    Dec 3 '18 at 4:06













  • Not sure if I got your point, but if you need the data on the initial format, you may just transpose it again.

    – GCSDC
    Dec 3 '18 at 18:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53493197%2fhow-to-display-the-table-headers-in-a-row-in-angular-material-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You may do that by:





  1. Transposing your data from columns to row:



    Before:
    Mango |10
    Banana|5



    After:
    Mango|Banana
    10|5




  2. Including the labels as the first column of your data:



    Name|Mango|Banana
    Quantity|10|5



  3. Adjust your table to display columns dynamically (so that you may have any number of columns), as on this example from angular material table documentation:



<table mat-table [dataSource]="data" class="mat-elevation-z8">
<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef> {{column}} </th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>



  1. If not required you may remove the header row



  2. If you want to format the first column, you may use some css as:



    td.mat-cell:nth-child(1) {
    // formatting
    }



  3. Also, if required, you may use property sticky of MatColumnDef to keep the first column sticky (in this case you may want to have the first column separate from the dynamic *ngFor loop, so that you may easily have only the first column sticky)



I've created a working stackblitz example






share|improve this answer


























  • Thank you for your response @GCSDC. I tried your solution but it still not displaying the expected result. Can please alter my code for better understanding.

    – Rajath
    Nov 27 '18 at 9:02











  • @Rajath I've included a working stackblitz example on my answer. Please let me know if this solves the issue.

    – GCSDC
    Nov 27 '18 at 16:53











  • Thank you very much! One more concern - if we do like that, then it becomes very difficult to update the data in the table(database) right? How we can address this issue.

    – Rajath
    Dec 3 '18 at 4:06













  • Not sure if I got your point, but if you need the data on the initial format, you may just transpose it again.

    – GCSDC
    Dec 3 '18 at 18:49
















1














You may do that by:





  1. Transposing your data from columns to row:



    Before:
    Mango |10
    Banana|5



    After:
    Mango|Banana
    10|5




  2. Including the labels as the first column of your data:



    Name|Mango|Banana
    Quantity|10|5



  3. Adjust your table to display columns dynamically (so that you may have any number of columns), as on this example from angular material table documentation:



<table mat-table [dataSource]="data" class="mat-elevation-z8">
<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef> {{column}} </th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>



  1. If not required you may remove the header row



  2. If you want to format the first column, you may use some css as:



    td.mat-cell:nth-child(1) {
    // formatting
    }



  3. Also, if required, you may use property sticky of MatColumnDef to keep the first column sticky (in this case you may want to have the first column separate from the dynamic *ngFor loop, so that you may easily have only the first column sticky)



I've created a working stackblitz example






share|improve this answer


























  • Thank you for your response @GCSDC. I tried your solution but it still not displaying the expected result. Can please alter my code for better understanding.

    – Rajath
    Nov 27 '18 at 9:02











  • @Rajath I've included a working stackblitz example on my answer. Please let me know if this solves the issue.

    – GCSDC
    Nov 27 '18 at 16:53











  • Thank you very much! One more concern - if we do like that, then it becomes very difficult to update the data in the table(database) right? How we can address this issue.

    – Rajath
    Dec 3 '18 at 4:06













  • Not sure if I got your point, but if you need the data on the initial format, you may just transpose it again.

    – GCSDC
    Dec 3 '18 at 18:49














1












1








1







You may do that by:





  1. Transposing your data from columns to row:



    Before:
    Mango |10
    Banana|5



    After:
    Mango|Banana
    10|5




  2. Including the labels as the first column of your data:



    Name|Mango|Banana
    Quantity|10|5



  3. Adjust your table to display columns dynamically (so that you may have any number of columns), as on this example from angular material table documentation:



<table mat-table [dataSource]="data" class="mat-elevation-z8">
<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef> {{column}} </th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>



  1. If not required you may remove the header row



  2. If you want to format the first column, you may use some css as:



    td.mat-cell:nth-child(1) {
    // formatting
    }



  3. Also, if required, you may use property sticky of MatColumnDef to keep the first column sticky (in this case you may want to have the first column separate from the dynamic *ngFor loop, so that you may easily have only the first column sticky)



I've created a working stackblitz example






share|improve this answer















You may do that by:





  1. Transposing your data from columns to row:



    Before:
    Mango |10
    Banana|5



    After:
    Mango|Banana
    10|5




  2. Including the labels as the first column of your data:



    Name|Mango|Banana
    Quantity|10|5



  3. Adjust your table to display columns dynamically (so that you may have any number of columns), as on this example from angular material table documentation:



<table mat-table [dataSource]="data" class="mat-elevation-z8">
<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef> {{column}} </th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>



  1. If not required you may remove the header row



  2. If you want to format the first column, you may use some css as:



    td.mat-cell:nth-child(1) {
    // formatting
    }



  3. Also, if required, you may use property sticky of MatColumnDef to keep the first column sticky (in this case you may want to have the first column separate from the dynamic *ngFor loop, so that you may easily have only the first column sticky)



I've created a working stackblitz example







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 16:53

























answered Nov 27 '18 at 5:52









GCSDCGCSDC

545516




545516













  • Thank you for your response @GCSDC. I tried your solution but it still not displaying the expected result. Can please alter my code for better understanding.

    – Rajath
    Nov 27 '18 at 9:02











  • @Rajath I've included a working stackblitz example on my answer. Please let me know if this solves the issue.

    – GCSDC
    Nov 27 '18 at 16:53











  • Thank you very much! One more concern - if we do like that, then it becomes very difficult to update the data in the table(database) right? How we can address this issue.

    – Rajath
    Dec 3 '18 at 4:06













  • Not sure if I got your point, but if you need the data on the initial format, you may just transpose it again.

    – GCSDC
    Dec 3 '18 at 18:49



















  • Thank you for your response @GCSDC. I tried your solution but it still not displaying the expected result. Can please alter my code for better understanding.

    – Rajath
    Nov 27 '18 at 9:02











  • @Rajath I've included a working stackblitz example on my answer. Please let me know if this solves the issue.

    – GCSDC
    Nov 27 '18 at 16:53











  • Thank you very much! One more concern - if we do like that, then it becomes very difficult to update the data in the table(database) right? How we can address this issue.

    – Rajath
    Dec 3 '18 at 4:06













  • Not sure if I got your point, but if you need the data on the initial format, you may just transpose it again.

    – GCSDC
    Dec 3 '18 at 18:49

















Thank you for your response @GCSDC. I tried your solution but it still not displaying the expected result. Can please alter my code for better understanding.

– Rajath
Nov 27 '18 at 9:02





Thank you for your response @GCSDC. I tried your solution but it still not displaying the expected result. Can please alter my code for better understanding.

– Rajath
Nov 27 '18 at 9:02













@Rajath I've included a working stackblitz example on my answer. Please let me know if this solves the issue.

– GCSDC
Nov 27 '18 at 16:53





@Rajath I've included a working stackblitz example on my answer. Please let me know if this solves the issue.

– GCSDC
Nov 27 '18 at 16:53













Thank you very much! One more concern - if we do like that, then it becomes very difficult to update the data in the table(database) right? How we can address this issue.

– Rajath
Dec 3 '18 at 4:06







Thank you very much! One more concern - if we do like that, then it becomes very difficult to update the data in the table(database) right? How we can address this issue.

– Rajath
Dec 3 '18 at 4:06















Not sure if I got your point, but if you need the data on the initial format, you may just transpose it again.

– GCSDC
Dec 3 '18 at 18:49





Not sure if I got your point, but if you need the data on the initial format, you may just transpose it again.

– GCSDC
Dec 3 '18 at 18:49




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53493197%2fhow-to-display-the-table-headers-in-a-row-in-angular-material-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)