So, I have some data coming in and rendering a table with an indeterminate number of columns. I would like to count those columns to correctly display a colspan td in the footer. I initialize a variable called footerCallspan with a value of 2...
var app = new Vue({
el: '#order-products',
data: {
footerColspan: 2, // default to 2 for title and quantity
},
//...
My footer table cell looks like ...
<td v-bind:colspan="footerColspan" class="summary">some text</td>
Each of the columns in the main table check a function to determine whether or not they should display ...
<th v-if="checkShowField(null, 'field_name')"scope="col">Name</th>
Finally, that function looks like ...
checkShowField(p, field) {
let pTest = p;
if (p == null) {
pTest = this.products[Object.keys(this.products)[0]];
}
if (pTest.hasOwnProperty(field)){
if (p == null) { // only increment on header row
// let span = this.footerColspan + 1;
// alert(span);
// this.footerColspan = span;
// alert(this.footerColspan);
}
return true;
}
return false;
},
No matter how I try to increment footerColspan inside that function, it seems to create an infinite loop.
I'm new to Vue, so I'm not sure if trying to increment that variable causes the table to re-render therefore triggering more calls to that function or what.
Anyone have any guidance here? If not, any other suggestions for counting the number of columns for my purpose?
Thanks!
colspanvalue, ie the maximum number of columns you expect. If you just want to span all columns, thecolspanvalue can be equal to or larger than the number of columnsthis.productsobj looks like? You shouldn't increment the footerSpan inside the checkShowField function. That can be determined using a computed field instead.this.productsis a collection of products keyed by their id and each having several properties (most strings, but a couple arrays). But some of the properties are for display and some are for calculations. So, I can't just count the number of properties in it and use that.