link822 link823 link824 link825 link826 link827 link828 link829 link830 link831 link832 link833 link834 link835 link836 link837 link838 link839 link840 link841 link842 link843 link844 link845 link846 link847 link848 link849 link850 link851 link852 link853 link854 link855 link856 link857 link858 link859 link860 link861 link862 link863 link864 link865 link866 link867 link868 link869 link870 link871 link872 link873 link874 link875 link876 link877 link878 link879 link880 link881 link882 link883 link884 link885 link886 link887 link888 link889 link890 link891 link892 link893 link894 link895 link896 link897 link898 link899 link900 link901 link902 link903 link904 link905 link906 link907 link908 link909 link910 link911 link912 link913 link914 link915 link916 link917 link918 link919 link920 link921 link922 link923 link924 link925 link926 link927 link928 link929 link930 link931 link932 link933 link934 link935 link936 link937 link938 link939 link940 link941 link942 link943 link944 link945 link946 link947 link948 link949 link950 link951 link952 link953 link954 link955 link956 link957 link958

[Vue.js] Color dots bases on their values - vue - javascriptPart of my vue code to display the value columnPart of my javascript codePart of my javascript code

there is created a table from a database. One of the columns called value.
Instead of displaying the value. when trying to display color dots based on a range.
For example, value is zero or 0.5 then the color of the dot is red. If the value is between 1 and 3 then the color is orange.
If the value is greater then 3 the dot is colored green.

At the moment I got the following error:
[vue.js warn]: Error in render: “TypeError: _vm.range is not a function”

It’s probably something about how if define my function. And the type of data, I gave to my function.
I don’t understand what the problem is. Could someone help me? Thanks a lot;

Part of my vue.js code to display the value column

<template slot=”value” slot-scope=”row”>
<span class=”green dot” v-if=”range(row.item.value) == 2”></span>
<span class=”orange dot” v-if=”range(row.item.value) == 1”></span>
<span class=”red dot” v-if=”range(row.item.value) == 1”></span>
</template>

Part of my javascript code

export default {
props: [‘dbdata’],
data() {
return {
dbs: this.dbdata,
dbSearch: ‘’,
totalRows: 1,
currentPage: 1,
perPage: 10,
pageOptions: [10, 20, 50,100],
sortBy: “id”,
sortDesc: false,
sortDirection: ‘asc’,
filter: null,
selectMode: ‘multi’,
selected: [],
fields: {
id: {
label: “Id”,
sortable: true
},
name: {
label: “Name”,
sortable: true
},
value: {
label: “value”,
sortable: true
}
}
}
},
computed:{
hits: function(){
var hits = this.dbs.length
return hits
},
range: function(x){
if (x < 1){
x = 0;
}if(x >= 1 && x <= 3 ){
x = 1;
}if(x >= 3 ){
x = 2;
}
return x
}

},

Solution :

the methods should be in the methods property:

computed: {
hits: function(){
var hits = this.dbs.length
return hits
},
}
methods: {
range: function(x){
if (x < 1){
x = 0;
}if(x >= 1 && x <= 3 ){
x = 1;
}if(x >= 3 ){
x = 2;
}
return x
}
}

https://vuejs.org/v2/guide/events.html#Method-Event-Handlers

Instead of using the v-if in this case, maybe you could use the css property to add the correct color. Try create a function that return the color as string and pass it to the :class property:

<template slot=”value” slot-scope=”row”>
<span class=”dot” :class=”color” />
</template>

https://vuejs.org/v2/guide/class-and-style.html#Object-Syntax

I hope that maked sense.

Solution 2:

You’re using a computed property, which doesn’t accept an argument.
What you have to use is a basic method like this :

methods: {
range(x) {
if (x < 1) {
x = 0;
} if(x >= 1 && x <= 3 ) {
x = 1;
} if(x >= 3 ) {
x = 2;
}
return x
}
}

Now you can use the method in the template.

Solution 3:

After the help I found the following.

The vue.js code

<template slot=”value” slot-scope=”row”>
<span class=’dot largerSize’ :class=”statusColor[range(row.item.value)]“></span>
</template>

Part of my javascript code

range: function(x){
if (x < this.min){
return 0;
}else if(x >= this.min && x <= this.max ){
return 1;
}else if(x >= this.max ){
return 2;
}
}