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

grid - conditional tmpl rendering #697

Open
edchelstephens opened this issue Jun 1, 2022 · 0 comments
Open

grid - conditional tmpl rendering #697

edchelstephens opened this issue Jun 1, 2022 · 0 comments

Comments

@edchelstephens
Copy link

Question:

How to conditionally render a different tmpl based on a property on a row on the data source, i.e. a property of the object?

With code:

let editButton = '<a class="btn btn-primary"  href="#" onclick="editTrainingSession({id})">Edit</a>'
let cancelButton = '<button class="btn btn-secondary" onclick="cancelTrainingSession({id})">Cancel</button>'
let deleteButton = '<button class="btn btn-danger" onclick="deleteTrainingSession({id})">Delete</button>'

How can I do this:

{ title:'Actions', tmpl: object.is_cancelled ? editButton: actionButtons}

That is, I want to hide the Cancel button if the session is already cancelled with the is_cancelled property being true

If the objet is cancelled, tmpl should be just editButton only, if not cancelled then all actionButtons

UI

image

Code

Grid:


let editButton = '<a class="btn btn-primary"  href="#" onclick="editTrainingSession({id})">Edit</a>'
let cancelButton = '<button class="btn btn-secondary" onclick="cancelTrainingSession({id})">Cancel</button>'
let deleteButton = '<button class="btn btn-danger" onclick="deleteTrainingSession({id})">Delete</button>'
 
 let actionButtons = `${editButton} ${cancelButton} ${deleteButton}`
 
var teamTrainingsGrid = $('#teamTrainingsGrid').grid({
      primaryKey: 'id',
      uiLibrary: 'bootstrap4',
      dataSource: 'training_sessions/',
      columns: [
        { title: 'Date', field: 'date' , sortable:false},
        { title: 'Time',  field: 'time' , sortable:true  },
        {title:"Duration", field:"duration", sortable:true},
         { title: 'Name',  field: 'name' , sortable:true  },
        {  title: 'Venue', field: 'venue', renderer:(value) =>value.name, sortable: true },
        {  title: 'Status', field: 'status' , sortable: true },
        {  title: 'Notes', field: 'notes', sortable: true },
        { title:'Actions', tmpl:actionButtons}
      ],
      pager: { limit: 20, sizes: [10, 20, 50, 100] }
    })

Returned data from endpoint:

{
    "total": 5,
    "records": [
        {
            "id": 133,
            "is_cancelled": true,
            "name": "Training Camp",
            "description": "Training Camp",
            "venue": {
                "id": 16,
                "name": "Christopher Camp"
            },
            "time": "03:10 PM - 04:10 PM",
            "date": "June 01, 2022",
            "duration": "1 hour",
            "status": "Cancelled",
            "notes": ""
        },
        {
            "id": 134,
            "is_cancelled": false,
            "name": "Training Camp",
            "description": "Training Camp",
            "venue": {
                "id": 16,
                "name": "Christopher Camp"
            },
            "time": "03:10 PM - 04:10 PM",
            "date": "June 03, 2022",
            "duration": "1 hour",
            "status": "Scheduled",
            "notes": ""
        },
      
      
    ]
}

Sofware versions:

Browser:

Google Chrome - Version 102.0.5005.61 (Official Build) (arm64)

Environment

Mac M1 Monterey

Any help is much appreciated,

Thanks!

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

No branches or pull requests

1 participant