vue-easytable plugin conditionally show columns - vue.js

I have used vue-easytable plugin version 2.8.2 to display a table inside a Vue component. I have a problem of finding how to conditionally display a table column.
In a demo displayed in here when either of the "Row Radio" or "Row Checkbox" switches are switched on we can see a column is added to the demo table dynamically. So I think this feature/functionality should be there already in vue-easytable, but I couldn't find how to achieve this referring to the documentation.
Within my Vue component the columns array I pass to vue-easytable is as follows.
columns: [
{
field: "entity",
key: "c",
title: "Entity",
align: "left",
sortBy: "asc",
},
{
field: "version",
key: "d",
title: "Version",
align: "center",
},
{
field: "test_date",
key: "e",
title: "Test Date",
align: "center",
},
{
field: "score",
key: "f",
title: "Score",
align: "center",
},
{
field: "score_percentage",
key: "g",
title: "Score (%)",
align: "center",
},
{
field: "result",
key: "h",
title: "Result",
align: "center",
}
]
I want to show the "Entity" column when a condition is satisfied. What should I do to achieve that?
You can find the vue-easytable documentation here.

In the source code of the demo, the column is being added to the columns array like so:
if (this.enableRowCheckbox) {
columns.push({
field: "checkbox",
key: "checkbox",
title: "",
width: 100,
fixed: this.enableColumnFixed ? "left" : "",
type: "checkbox",
});
}
For your use case you might be better off setting the defaultHiddenColumnKeys option and/or the hiddenColumnsByKeys and showColumnsByKeys instance methods. See the link for examples.
Or you can use the cellStyleOption, like so:
cellStyleOption: {
headerCellClass: ({ column }) => {
if (column.field === "entity") {
return someCondition?'text-visible-class':'text-hidden-class';
}
},
bodyCellClass: ({ column }) => {
if (column.field === "entity") {
return someCondition?'text-visible-class':'text-hidden-class';
}
},
},

Related

In Vuetify, how can I put a sparkline component in a data table row?

I want to create a data table that has a column that contains a sparkline for each item in the data table. Is that possible?
Here's a mock-up of what I'm aiming for:
You have the option to use the item.name slot. Create a separate column for chart, then use its slot to put the sparkline:
headers: [
{
text: "Dessert (100g serving)",
align: "start",
sortable: false,
value: "name",
},
{ text: "Calories", value: "calories" },
{ text: "Fat (g)", value: "fat" },
{ text: "Carbs (g)", value: "carbs" },
{ text: "Protein (g)", value: "protein" },
{ text: "Iron (%)", value: "iron" },
{ text: "Chart", value: "chart" },
],
<template v-slot:item.chart={item}>
<v-sparkline
:value="value"
:gradient="gradient"
:smooth="radius || false"
:padding="padding"
:line-width="width"
:stroke-linecap="lineCap"
:gradient-direction="gradientDirection"
:fill="fill"
:type="type"
:auto-line-width="autoLineWidth"
auto-draw
></v-sparkline>
</template>
Here is the full example: https://codesandbox.io/s/white-hill-spwzs?file=/src/components/HelloWorld.vue:1174-1618

How to change the width of b-table th

I want to expand the rows of the table as in screenshot.How do I change the width of the th?
Is there any way how to add class to th
I want to
logTableField: [
{ key: "LogDate", label: "Tarih",thClass: 'bg-white text-dark' },
{ key: "LogUser", label: "Analist" },
{ key: "Description", label: " İşlem Açıklaması" },
{ key: "LogText", label: "Kullanıcı Yorumu" },
],
<b-tab title="Kayıt Logları" v-if="this.Logs != null">
<b-table
id="logTable"
striped
hover
:items="Logs"
:fields="logTableField"
per-page="10"
:current-page="currentPageLog"
>
<template slot="Description" slot-scope="row">
<div v-html="row.item.Description"></div>
</template>
</b-table>
<b-pagination
v-model="currentPageLog"
:total-rows="Logs.length"
per-page="10"
aria-controls="my-table"
></b-pagination>
</b-tab>
Check bootstrap-vue documentation for detailed styling on tables.
EDIT:
Please use lowercase variables.
Read the docs which are listed above
Why are your summed up widths not 100%?
If you in fact want to use classes, look up the answer from Stefanos_Apk which is perfect if there is more than one style attribute in my opinion
https://codesandbox.io/s/nervous-chandrasekhar-d5e2o?file=/src/components/Table.vue
logTableField: [
{
key: "logDate",
label: "Tarih",
thStyle: { width: "10%" },
},
{ key: "logUser", label: "Analist", thStyle: { width: "20%" } },
{
key: "description",
label: " İşlem Açıklaması",
thStyle: { width: "20%" },
},
{
key: "logText",
label: "Kullanıcı Yorumu",
thStyle: { width: "50%" },
},
],
You can set the thClass property inside of your field object. But tdClass just accepts a string or an array, not an object. So you can only reference to a css class.
logTableField: [
{ key: "LogDate", label: "Tarih", thClass: 'nameOfTheClass' },
{ key: "LogUser", label: "Analist", thClass: 'nameOfTheClas1' },
{ key: "Description", label: " İşlem Açıklaması", thClass: 'nameOfTheClass2' },
{ key: "LogText", label: "Kullanıcı Yorumu", thClass: 'nameOfTheClass3' },
]
and in your CSS:
.nameOfTheClass {
width: 10%
},
.nameOfTheClass1 {
width: 15%
}
.nameOfTheClass2 {
width: 15%
}
.nameOfTheClass3 {
width: 50%
}

Client side Sorting with server side Pagination in Quasar

I have a table displaying search results with server side Pagination but when I click the columns, there is no sorting taking place and the server displays the results again for any of the column arrow clicks. Is there any way I can limit the pagination to take place only for the page arrows in the footer and then sorting to take place for the displayed results when i click the columns?
<q-table
dense
:title="Patient result"
:data="searchResultList"
:columns="columns"
:pagination="serverPagination"
row-key="name"
:no-data-label="noDataMessage"
:loading="loading"
#request="request"
>
data() {
return {
filter: '',
columns: [
{
name: 'patientId',
required: true,
label: 'patientId',
align: 'left',
field: row => row.patientId,
format: val => `${val}`,
sortable: true,
}
{
name: 'lastname', align: 'center', label: 'lastname', field: 'lastname', sortable: true,
},
{
name: 'firstname', align: 'center', label: 'firstname', field: 'firstname', sortable: true,
},
{
name: 'dob', align: 'center', label: 'dob', field: 'dob', sortable: true,
},
],
page: 1,
rowsInPage: 25,
};
computed: {
serverPagination() {
return {
sortBy: this.paginationObject.sortBy,
descending: this.paginationObject.descending,
page: this.paginationObject.page,
rowsNumber: this.patientSearchResults.count,
rowsPerPage: this.paginationObject.rowsPerPage,
};
paginationObject: {
sortBy: 'name',
descending: false,
page: 1,
rowsPerPage: 25,
methods: {
request(props) {
this.$store.dispatch('patientSearch/setPaginationObject', props);
},
Use sort-method. You can write a custom function where you can pass the rows(Not all but according to pagination i.e. if the selected page is 1 the rows will be 0-5 from your array. Here I'm guessing the default visible rows will be 5.)
Check out this API reference - https://quasar.dev/vue-components/table
In QTable API, you will find the details about this method.

set column headers in knockout grid dynamically

I have a requirement where I need to set column headers in knockout grid dynamically. I tried using observable but didn't help as initially whatever value is assigned that only it is keeping .
self.ItemsGrid = new ko.grid.viewModel({
columns: [
{ field: "ItemCode", headerText: self.lblItemHeader, sort: self.GetItems },
{ field: "Mark", headerText: self.lblMark, sort: self.GetItems },
{ field: "Equipment", headerText: self.lblEquipment, sort: self.GetItems },
{ field: "RiskCategory", headerText: lblRiskCateg, sort: self.GetItems },
{ field: "Region", headerText: lblRegion, sort: self.GetItems },
{ field: "SerialNumber", headerText: lblSerialNo, sort: self.GetItems },
{ field: "RiskArea", headerText: lblRiskArea, sort: self.GetItems },
{ field: "RiskType", headerText: lblRiskType, sort: self.GetItems },
{ field: "RiskValue", headerText: lblRiskVal, sort: self.GetItems },
{ field: "", headerText: '' }
],
rowTemplate: "itemsTemplate",
paginate: self.GetItems,
pageSizeChange: self.GetItems,
pageSizeOptions: [10, 20, 30],
currentPageIndex: 1,
root: self
});
For first three columns I have used observables. These observables I am setting in a function and value is getting updated for these observables but still does not reflect in header.

Hide column in dgrid dynamically

How to hide complete column in dgrid (gridFromHtml) based on some run time parameter?
Lets say if the value of parameter is true I should be able to display some column and if the value is false then I should be able to hide that same column.
Use grid.styleColumn(columnId, css):
var grid = new Grid({
store: store,
columns: [
{ id: "artist", label: "Artist", field: "Artist"},
{ id: "name", label: "Song", field: "Name"},
{ id: "gerne", label: "Genre", field: "Genre"}
]
}, "grid-placeholder");
// to hide column with id="name"
grid.styleColumn("name", "display: none;");
// to show it
grid.styleColumn("name", "display: table-cell;");
There is a dgrid extension called ColumnHider which allows you to pass in a column with a "hidden" property.
require([
"dojo/_base/declare", "dgrid/OnDemandGrid", "dgrid/extensions/ColumnHider"
], function(declare, OnDemandGrid, ColumnHider) {
var grid = new(declare([OnDemandGrid, ColumnHider]))({
columns: {
col1: {
label: "Column 1",
hidden: true
},
col2: {
label: "Column 2",
unhidable: true
},
col3: "Column 3"
}
}, "grid");
// ...
});
This will also give the user the ability to hide their own columns. You can set some columns to be unhidable, like column 2 above
You need to use toggleColumnHiddenState:
require([
'dojo/_base/declare',
'dgrid/OnDemandGrid',
'dgrid/extensions/ColumnHider'
], function (declare, OnDemandGrid, ColumnHider) {
var grid = new (declare([ OnDemandGrid, ColumnHider ]))({
columns: {
'id': {label: '#'},
'name': {label: 'Название'}
}
}, 'grid');
grid.toggleColumnHiddenState('name', true); // hiding
grid.toggleColumnHiddenState('name', false); // showing
grid.toggleColumnHiddenState('name'); // toggling column
});
Should think this will work
var grid = new dojox.grid.DataGrid({
store: dataStore,
structure: [{
name: "ID",
field: "id",
width: "100px"
}, {
name: "Values",
field: "values",
width: "100px"
}]
}, "myGrid");
grid.startup();
function showOrHideColumn(show, widget, index) {
var d = show ? "" : "none"
dojo.query('td[idx="'+index+'"]', widget.viewsNode).style("display", d);
dojo.query('th[idx="'+index+'"]', widget.viewsHeaderNode).style("display", d);
}
showOrHideColumn(false,grid,0);