No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Q-table with colorful q-badge

    Help
    2
    3
    518
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      Kate last edited by

      Hey there, I want to make a column with colored q-badge items. For example: warning, error, info and etc. I tried with if:

            <template v-slot:body-cell-name="props">
              <div class="table-trace-row">
                {{ props.row.trace }}
              </div>
            </template>
            <template v-slot:body-cell-logLabel="props">
              <div class="flex flex-center">
                <q-tr :props="props">
                  <q-badge :class="myTrClass(props)">
                    {{ props.row.logLabel }}
                  </q-badge>
                </q-tr>
              </div>
            </template>
          ...
      
         methods: {
            myTrClass(props) {
              if (props.row.logLabel === 'TRACE') {
                return 'bg-grey-3'
              }
              if (props.row.logLabel === 'WARNING') {
                return 'bg-warning'
              }
              if (props.row.logLabel === 'ERROR') {
                return 'bg-red'
              }
              if (props.row.logLabel === 'DEBUG') {
                return 'bg-green'
              }
              if (props.row.logLabel === 'INFO') {
                return 'bg-purple'
              }
            }
          }
      

      Yes, it is working but I would like to go through the cycle and write a better option, more concise, shorter. Tell me please, what are the more convenient versions of this task?

      metalsadman 1 Reply Last reply Reply Quote 0
      • metalsadman
        metalsadman @Kate last edited by metalsadman

        @Kate use an object literal. ie.

        ...
          <q-badge :color="myTrClass(props.row.logLabel)">
             {{ props.row.logLabel }}
           </q-badge>
         ...
        
        const color = col => ({
                  'TRACE': 'grey-3',
                  'WARNING': 'warning',
                  'ERROR': 'red',
                  'DEBUG': 'green',
                  'INFO': 'purple'
                }[col])
        ...
        myTrClass(logLabel) {
          return color(logLabel)
        }
        
        1 Reply Last reply Reply Quote 0
        • K
          Kate last edited by

          @metalsadman Thank you very much! It is working.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post