How to toggle disabled property from button if it's row has been selected by checkbox in a p-datatable












1















I am currently developing the user interface of a web application using angular6. I have a p-dataTable component (primeNG) with several rows, each row has as a first column a checkbox and as last one a disabled button by default.



The goal is opening a dialog if I select a row by clicking it's checkbox and then clicking it's dialog trigger button after unable that button (and just that one, not the rest of row buttons).



I thought I could get the row selected index and style the button via CSS with nth-child(index) but I read p-columns in a p-datatable doesn't create an index.



It looks like something trivial, but I'm not able to unable just the button of the selected row.



I'll appreciate your help.



That's a simplified markup example:



    <p-datatable [value]="data" scrollable="true" scrollHeight="350px [(selection)]="selectedItems" columnResizeMode="expand">
<p-header>
<div class="ui-helper-clearfix">Example Table</div>
</p-header>
<p-column class="checkbox" selectionMode="multiple"></p-column>
<p-column class="data-column" field="dataColumn1" header="Column1"></p-column>
<p-column class="data-column" field="dataColumn2"header="Column2"></p-column>
<p-column>
<ng-template>
<button type="button" class="btn" icon="fa fa-user" [disabled]="??????"></button>
</ng-template>
</p-column>
</p-datatable>


Thanks in advance and greetings!










share|improve this question

























  • You need to test if selectedItems includes the row. I've never used primeng, but the selection example of the datatable documentation shows that you can use let-rowData to access to the current row.

    – JB Nizet
    Nov 24 '18 at 9:25
















1















I am currently developing the user interface of a web application using angular6. I have a p-dataTable component (primeNG) with several rows, each row has as a first column a checkbox and as last one a disabled button by default.



The goal is opening a dialog if I select a row by clicking it's checkbox and then clicking it's dialog trigger button after unable that button (and just that one, not the rest of row buttons).



I thought I could get the row selected index and style the button via CSS with nth-child(index) but I read p-columns in a p-datatable doesn't create an index.



It looks like something trivial, but I'm not able to unable just the button of the selected row.



I'll appreciate your help.



That's a simplified markup example:



    <p-datatable [value]="data" scrollable="true" scrollHeight="350px [(selection)]="selectedItems" columnResizeMode="expand">
<p-header>
<div class="ui-helper-clearfix">Example Table</div>
</p-header>
<p-column class="checkbox" selectionMode="multiple"></p-column>
<p-column class="data-column" field="dataColumn1" header="Column1"></p-column>
<p-column class="data-column" field="dataColumn2"header="Column2"></p-column>
<p-column>
<ng-template>
<button type="button" class="btn" icon="fa fa-user" [disabled]="??????"></button>
</ng-template>
</p-column>
</p-datatable>


Thanks in advance and greetings!










share|improve this question

























  • You need to test if selectedItems includes the row. I've never used primeng, but the selection example of the datatable documentation shows that you can use let-rowData to access to the current row.

    – JB Nizet
    Nov 24 '18 at 9:25














1












1








1








I am currently developing the user interface of a web application using angular6. I have a p-dataTable component (primeNG) with several rows, each row has as a first column a checkbox and as last one a disabled button by default.



The goal is opening a dialog if I select a row by clicking it's checkbox and then clicking it's dialog trigger button after unable that button (and just that one, not the rest of row buttons).



I thought I could get the row selected index and style the button via CSS with nth-child(index) but I read p-columns in a p-datatable doesn't create an index.



It looks like something trivial, but I'm not able to unable just the button of the selected row.



I'll appreciate your help.



That's a simplified markup example:



    <p-datatable [value]="data" scrollable="true" scrollHeight="350px [(selection)]="selectedItems" columnResizeMode="expand">
<p-header>
<div class="ui-helper-clearfix">Example Table</div>
</p-header>
<p-column class="checkbox" selectionMode="multiple"></p-column>
<p-column class="data-column" field="dataColumn1" header="Column1"></p-column>
<p-column class="data-column" field="dataColumn2"header="Column2"></p-column>
<p-column>
<ng-template>
<button type="button" class="btn" icon="fa fa-user" [disabled]="??????"></button>
</ng-template>
</p-column>
</p-datatable>


Thanks in advance and greetings!










share|improve this question
















I am currently developing the user interface of a web application using angular6. I have a p-dataTable component (primeNG) with several rows, each row has as a first column a checkbox and as last one a disabled button by default.



The goal is opening a dialog if I select a row by clicking it's checkbox and then clicking it's dialog trigger button after unable that button (and just that one, not the rest of row buttons).



I thought I could get the row selected index and style the button via CSS with nth-child(index) but I read p-columns in a p-datatable doesn't create an index.



It looks like something trivial, but I'm not able to unable just the button of the selected row.



I'll appreciate your help.



That's a simplified markup example:



    <p-datatable [value]="data" scrollable="true" scrollHeight="350px [(selection)]="selectedItems" columnResizeMode="expand">
<p-header>
<div class="ui-helper-clearfix">Example Table</div>
</p-header>
<p-column class="checkbox" selectionMode="multiple"></p-column>
<p-column class="data-column" field="dataColumn1" header="Column1"></p-column>
<p-column class="data-column" field="dataColumn2"header="Column2"></p-column>
<p-column>
<ng-template>
<button type="button" class="btn" icon="fa fa-user" [disabled]="??????"></button>
</ng-template>
</p-column>
</p-datatable>


Thanks in advance and greetings!







angular primeng primeng-datatable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 8:58









Toni Michel Caubet

6,88345157304




6,88345157304










asked Nov 24 '18 at 9:15









rmi8092rmi8092

284




284













  • You need to test if selectedItems includes the row. I've never used primeng, but the selection example of the datatable documentation shows that you can use let-rowData to access to the current row.

    – JB Nizet
    Nov 24 '18 at 9:25



















  • You need to test if selectedItems includes the row. I've never used primeng, but the selection example of the datatable documentation shows that you can use let-rowData to access to the current row.

    – JB Nizet
    Nov 24 '18 at 9:25

















You need to test if selectedItems includes the row. I've never used primeng, but the selection example of the datatable documentation shows that you can use let-rowData to access to the current row.

– JB Nizet
Nov 24 '18 at 9:25





You need to test if selectedItems includes the row. I've never used primeng, but the selection example of the datatable documentation shows that you can use let-rowData to access to the current row.

– JB Nizet
Nov 24 '18 at 9:25












1 Answer
1






active

oldest

votes


















1














I've found a solution:



First of all I need a ng-template inside my checkbox p-column in order to have a rowIndex variable to be read in the class component through a function triggered in the event onChange of the checkbox.



There I save this value in a class variable called "indexSelected" that I'll be watching in the ng-template of the buttons p-column. I'll be comparing this rowIndex value with the "indexSelected" to keep disabled or not my buttons.



This is my code:



Template:



   <p-datatable [value]="data" scrollable="true" scrollHeight="350px [(selection)]="selectedItems" columnResizeMode="expand">
<p-header>
<div class="ui-helper-clearfix">Example Table</div>
</p-header>
<p-column selectionMode="multiple" [styleClass]="'colsmall'">
<ng-template let-riCheck="rowIndex" pTemplate="body">
<p-checkbox (onChange)="selectRow(riCheck); "></p-checkbox>
</ng-template>
</p-column>
<p-column class="data-column" field="dataColumn1" header="Column1"></p-column>
<p-column class="data-column" field="dataColumn2"header="Column2"></p-column>
<p-column header="Acciones" styleClass="colmedium" frozen="true">
<ng-template pTemplate="body" let-ri="rowIndex">
<button type="button" pButton icon="fa-user" (click)="goToEditContact();" [disabled]="indexSelected != ri"></button>
<button type="button" pButton icon="fa-ellipsis-h" class="ui-button-inverted" (click)="mostrarMasOpciones($event, op)" [disabled]="indexSelected != ri"></button>
</ng-template>
</p-column>
</p-datatable>


Class Component:



...

selectRow (index) {
this.indexSelected = index;
}

...





share|improve this answer























    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%2f53456762%2fhow-to-toggle-disabled-property-from-button-if-its-row-has-been-selected-by-che%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














    I've found a solution:



    First of all I need a ng-template inside my checkbox p-column in order to have a rowIndex variable to be read in the class component through a function triggered in the event onChange of the checkbox.



    There I save this value in a class variable called "indexSelected" that I'll be watching in the ng-template of the buttons p-column. I'll be comparing this rowIndex value with the "indexSelected" to keep disabled or not my buttons.



    This is my code:



    Template:



       <p-datatable [value]="data" scrollable="true" scrollHeight="350px [(selection)]="selectedItems" columnResizeMode="expand">
    <p-header>
    <div class="ui-helper-clearfix">Example Table</div>
    </p-header>
    <p-column selectionMode="multiple" [styleClass]="'colsmall'">
    <ng-template let-riCheck="rowIndex" pTemplate="body">
    <p-checkbox (onChange)="selectRow(riCheck); "></p-checkbox>
    </ng-template>
    </p-column>
    <p-column class="data-column" field="dataColumn1" header="Column1"></p-column>
    <p-column class="data-column" field="dataColumn2"header="Column2"></p-column>
    <p-column header="Acciones" styleClass="colmedium" frozen="true">
    <ng-template pTemplate="body" let-ri="rowIndex">
    <button type="button" pButton icon="fa-user" (click)="goToEditContact();" [disabled]="indexSelected != ri"></button>
    <button type="button" pButton icon="fa-ellipsis-h" class="ui-button-inverted" (click)="mostrarMasOpciones($event, op)" [disabled]="indexSelected != ri"></button>
    </ng-template>
    </p-column>
    </p-datatable>


    Class Component:



    ...

    selectRow (index) {
    this.indexSelected = index;
    }

    ...





    share|improve this answer




























      1














      I've found a solution:



      First of all I need a ng-template inside my checkbox p-column in order to have a rowIndex variable to be read in the class component through a function triggered in the event onChange of the checkbox.



      There I save this value in a class variable called "indexSelected" that I'll be watching in the ng-template of the buttons p-column. I'll be comparing this rowIndex value with the "indexSelected" to keep disabled or not my buttons.



      This is my code:



      Template:



         <p-datatable [value]="data" scrollable="true" scrollHeight="350px [(selection)]="selectedItems" columnResizeMode="expand">
      <p-header>
      <div class="ui-helper-clearfix">Example Table</div>
      </p-header>
      <p-column selectionMode="multiple" [styleClass]="'colsmall'">
      <ng-template let-riCheck="rowIndex" pTemplate="body">
      <p-checkbox (onChange)="selectRow(riCheck); "></p-checkbox>
      </ng-template>
      </p-column>
      <p-column class="data-column" field="dataColumn1" header="Column1"></p-column>
      <p-column class="data-column" field="dataColumn2"header="Column2"></p-column>
      <p-column header="Acciones" styleClass="colmedium" frozen="true">
      <ng-template pTemplate="body" let-ri="rowIndex">
      <button type="button" pButton icon="fa-user" (click)="goToEditContact();" [disabled]="indexSelected != ri"></button>
      <button type="button" pButton icon="fa-ellipsis-h" class="ui-button-inverted" (click)="mostrarMasOpciones($event, op)" [disabled]="indexSelected != ri"></button>
      </ng-template>
      </p-column>
      </p-datatable>


      Class Component:



      ...

      selectRow (index) {
      this.indexSelected = index;
      }

      ...





      share|improve this answer


























        1












        1








        1







        I've found a solution:



        First of all I need a ng-template inside my checkbox p-column in order to have a rowIndex variable to be read in the class component through a function triggered in the event onChange of the checkbox.



        There I save this value in a class variable called "indexSelected" that I'll be watching in the ng-template of the buttons p-column. I'll be comparing this rowIndex value with the "indexSelected" to keep disabled or not my buttons.



        This is my code:



        Template:



           <p-datatable [value]="data" scrollable="true" scrollHeight="350px [(selection)]="selectedItems" columnResizeMode="expand">
        <p-header>
        <div class="ui-helper-clearfix">Example Table</div>
        </p-header>
        <p-column selectionMode="multiple" [styleClass]="'colsmall'">
        <ng-template let-riCheck="rowIndex" pTemplate="body">
        <p-checkbox (onChange)="selectRow(riCheck); "></p-checkbox>
        </ng-template>
        </p-column>
        <p-column class="data-column" field="dataColumn1" header="Column1"></p-column>
        <p-column class="data-column" field="dataColumn2"header="Column2"></p-column>
        <p-column header="Acciones" styleClass="colmedium" frozen="true">
        <ng-template pTemplate="body" let-ri="rowIndex">
        <button type="button" pButton icon="fa-user" (click)="goToEditContact();" [disabled]="indexSelected != ri"></button>
        <button type="button" pButton icon="fa-ellipsis-h" class="ui-button-inverted" (click)="mostrarMasOpciones($event, op)" [disabled]="indexSelected != ri"></button>
        </ng-template>
        </p-column>
        </p-datatable>


        Class Component:



        ...

        selectRow (index) {
        this.indexSelected = index;
        }

        ...





        share|improve this answer













        I've found a solution:



        First of all I need a ng-template inside my checkbox p-column in order to have a rowIndex variable to be read in the class component through a function triggered in the event onChange of the checkbox.



        There I save this value in a class variable called "indexSelected" that I'll be watching in the ng-template of the buttons p-column. I'll be comparing this rowIndex value with the "indexSelected" to keep disabled or not my buttons.



        This is my code:



        Template:



           <p-datatable [value]="data" scrollable="true" scrollHeight="350px [(selection)]="selectedItems" columnResizeMode="expand">
        <p-header>
        <div class="ui-helper-clearfix">Example Table</div>
        </p-header>
        <p-column selectionMode="multiple" [styleClass]="'colsmall'">
        <ng-template let-riCheck="rowIndex" pTemplate="body">
        <p-checkbox (onChange)="selectRow(riCheck); "></p-checkbox>
        </ng-template>
        </p-column>
        <p-column class="data-column" field="dataColumn1" header="Column1"></p-column>
        <p-column class="data-column" field="dataColumn2"header="Column2"></p-column>
        <p-column header="Acciones" styleClass="colmedium" frozen="true">
        <ng-template pTemplate="body" let-ri="rowIndex">
        <button type="button" pButton icon="fa-user" (click)="goToEditContact();" [disabled]="indexSelected != ri"></button>
        <button type="button" pButton icon="fa-ellipsis-h" class="ui-button-inverted" (click)="mostrarMasOpciones($event, op)" [disabled]="indexSelected != ri"></button>
        </ng-template>
        </p-column>
        </p-datatable>


        Class Component:



        ...

        selectRow (index) {
        this.indexSelected = index;
        }

        ...






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 9:54









        rmi8092rmi8092

        284




        284






























            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%2f53456762%2fhow-to-toggle-disabled-property-from-button-if-its-row-has-been-selected-by-che%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

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

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