atatanasov / gijgo

Gijgo - Free Javascript Controls
http://gijgo.com
MIT License
475 stars 187 forks source link

grid - conditional tmpl rendering #697

Open edchelstephens opened 2 years ago

edchelstephens commented 2 years ago

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!