assign an ID in each object of array - vue.js

I have a use case whereby sequential numbers need to be displayed in a table of data in the UI of an app, beginning at 1. The app is built using Vue and Buefy for the UI component library. Here is a screenshot of some sample data in such a table:
This needs to have a third column added and should display 1 in the first row and 2 in the second row.
I looked in the Buefy docs to see if their table component has built-in capability to do this but didn't see anything that fit. If that is the case, the data passed to the table component will need to provide it.
Since the format of the data passed to the table component is an array of objects I was thinking there might be a way to use each object's index, incremented by 1, for this purpose. But I'm not sure how to get this:
const data = [
{ color: 'blue', size: 'large', height: 'tall' },
{ color: 'green', size: 'medium', height: 'short' },
{ color: 'purple', size: 'small', height: 'average' }
];
to be this:
const data = [
{ id: '1', color: 'blue', size: 'large', height: 'tall' },
{ id: '2', color: 'green', size: 'medium', height: 'short' },
{ id: '3', color: 'purple', size: 'small', height: 'average' }
];
I tried the following but it does not give the desired outcome:
console.log([...data, ...Object.keys(data)];
How can the desired end result be achieved? I'm looking for a simple approach, if possible.
Also, I was concerned about what happens if an element gets removed from the original array of objects...would that mess up the sequential numbering that's based on the index number? I tested that situation by executing data.splice(0,1); but examining the results in the console, the objects appear to get re-indexed and therefore it shouldn't be an issue. Unless someone knows otherwise.

If I understood you correctly try with index:
new Vue({
el: '#demo',
data() {
return {
items: [
{ color: 'blue', size: 'large', height: 'tall' },
{ color: 'green', size: 'medium', height: 'short' },
{ color: 'purple', size: 'small', height: 'average' }
]
}
},
methods: {
del(i) {
this.items.splice(i, 1)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<div v-for="(item, i) in items" :key="i">
<div>{{ i+1 }} - {{ item.color }}</div>
<button #click="del(i)">delete</button>
</div>
</div>

Related

QRCodeVue3 - Load image into center of QR Code

Trying to load an image in the center of a QR Code on a Vue3 project. Been reading through the API documentation online and it appears I'm supposed to load it as a string through the "image" parameter, but every time I try to load the image by QR Code disappears entirely.
The page I'm specifically working through is here: https://www.npmjs.com/package/qrcode-vue3
Below is the current working of the code:
<QRCodeVue3
:width="200"
:height="200"
value="HelloWorld"
:qrOptions="{ typeNumber: 0, mode: 'Byte', errorCorrectionLevel: 'H' }"
image="https://cryptologos.cc/logos/ravencoin-rvn-logo.png"
:imageOptions="{ hideBackgroundDots: true, imageSize: 0.4, margin: 0 }"
backgroundColor="white"
:dotsOptions="{
type: 'dots',
color: '#26249a',
gradient: {
type: 'linear',
rotation: 0,
colorStops: [
{ offset: 0, color: '#26249a' },
{ offset: 1, color: '#26249a' },
],
},
}"
:backgroundOptions="{ color: '#ffffff' }"
:cornersSquareOptions="{ type: 'dot', color: '#000000' }"
:cornersDotOptions="{ type: undefined, color: '#000000' }"
/>
End goal I would prefer to load the image from my assets folder but I can't even get a simple image that works to load. I know it's possible because I see the examples QR codes on the npm page but no example code to demonstrate it.
Realized I needed to add the crossOrigin = 'Anonymous' as well as I implemented a v-bind to a url of my local image. Final working solution is as follows:
New component:
<QRCodeVue3
:width="200"
:height="200"
value="HelloWorld"
:qrOptions="{ typeNumber: 0, mode: 'Byte', errorCorrectionLevel: 'H' }"
v-bind:image="iconUrl"
:imageOptions="{ hideBackgroundDots: true, imageSize: 0.4, margin: 3, crossOrigin: 'Anonymous' }"
backgroundColor="white"
:dotsOptions="{
type: 'dots',
color: '#26249a',
gradient: {
type: 'linear',
rotation: 0,
colorStops: [
{ offset: 0, color: '#26249a' },
{ offset: 1, color: '#26249a' },
],
},
}"
:backgroundOptions="{ color: '#ffffff' }"
:cornersSquareOptions="{ type: 'dot', color: '#000000' }"
:cornersDotOptions="{ type: undefined, color: '#000000' }"
/>
Loading image:
export default {
name: "HomeView",
components: {
QRCodeVue3,
},
data() {
return {
iconUrl: require('../assets/ravencoin-rvn-logo.png')
};
},

How to set dynamic styles in Vue.js component from Vuex store

I'm trying to set an array of components styles directly from the store, so that when the store changes, the design of each component changes too.
I store a set of links in my Vuex store like this:
links: [
{id: 1, text: 'Banana is a test', design: {color: 'red', 'background-color': 'blue', padding: '51px', margin: '5px', 'border-width' : '10px', 'border-color': 'blue', 'font-weight' : 600, font: 'Arial'}},
{id: 2, text: 'This is a test', design: {color: 'red', 'background-color': 'blue', padding: '20px', margin: '10px', 'border-width' : '10px', 'border-color': 'green', 'font-weight' : 600, font: 'Arial'}},
{id: 3, text: 'Monkey is a test', design: {color: 'red', 'background-color': 'blue', padding: '5px', margin: '5px', 'border-width' : '10px', 'border-color': 'green', 'font-weight' : 600, font: 'Arial'}},
]
and this is how I try to render them
<a v-for="link in links" :key=link.id :href="link.destination" :id=link.id :style="link.design">
{{link.text}}
</a>
the trouble is, when the design objects changes in the Vuex store, the links styles are not subsequently updated as I would expect.
In my Vue component I've tried getting the links in different ways, assuming this would change the reactivity. Currently I get them using a computed method like so:
computed: {
getLinks: function() {
return this.$store.state.links
}
},
but whenever I change the value of a background-color, say from 'blue' to 'red', I have to reload the page to see the change. Do I need to force a rerender of the page everytime I call my mutation?
This is my mutation for reference:
setSelectedItemDesign (state, payload ) {
state.selectedItem.design[Object.keys(payload)[0]] = Object.values(payload)[0]
}
and I'd call it from my component like this:
this.$store.commit('setSelectedItemDesign', {'background-color' : this.rgbaValue})
Thats because you read value from store directly here return this.$store.state.links. This way it is not reactive and also anitpattern, because you should not access store like that.
You should create getter to get value and then it should be ok.
The problem was that I was not updating the array in my store in a way that Vue observes to trigger a view update, as nada correctly pointed out.
You can see here for more details: vuejs.org/v2/guide/list.html#Array-Change-Detection

ECharts: Display Legend for Treemap?

The treemap below has two levels. I want to display a legend for the top level nodes (Node A and Node B). With other types of charts I've used, the legend can be auto-generated or I can define it explicitly. With the treemap, it appears one is not auto-generated, and if I define one explicitly, it is never displayed. Is it possible to display a legend for a treemap?
<style>
#chart {
position: absolute;
border: 1px solid;
top: 100px;
left: 100px;
right: 100px;
bottom: 100px;
border: 1px solid black;
}
</style>
<div id="chart"></div>
<script src="http://echarts.baidu.com/dist/echarts.min.js"></script>
<script>
var options = {
series: [{
type: 'treemap',
data: [{
name: 'Node A',
value: 20,
children: [{
name: 'Node A1',
value: 12
}, {
name: 'Node A2',
value: 8
}]
}, {
name: 'Node B',
value: 20,
children: [{
name: 'Node B1',
value: 20
}]
}]
}]
};
var chart = echarts.init(document.getElementById("chart"));
chart.setOption(options);
</script>
The legend object of ECharts is constructed from the series object by default. This means that nested datas in treemap series aren't part of the legend. You need to make two entries in your series: one for Node A, one for Node B.
So you can first use the code below, and you'd see that you run into an UI-related issue.
{
legend: {
data: ['Node A', 'Node B'],
top: 55,
itemGap: 5,
backgroundColor: 'rgb(243,243,243)',
borderRadius: 5
},
series: [
{
type: 'treemap',
name: 'Node A',
data: [{
name: 'Node A1',
value: 12,
}, {
name: 'Node A2',
value: 8,
}]
}, {
type: 'treemap',
name: 'Node B',
data: [{
name: 'Node B1',
value: 20,
}]
}
]
}
This code will run, but the legend <-> chart sync will not work properly since ECharts doesn't support multiselect mode legend for the treemap object (it's a bit technical). Basically, you can only use single-selection mode as of the current ECharts version.
To get rid of the weird UI issue, you will either have to remove the legends (since the name already describes each block in the map so you might not need any legend), or add the following line inside the legend object:
selectedMode: 'single'
This will allow you to have a properly working legend, but this will not allow you to display two series at the same time. At least, you will be able to switch between your entries in your series array.
Here is a demo screenshot on the official editor:
Echarts demo

Can Office Fabric DetailsList column headers be styled?

I was looking through the office fabric documentation, there seems to be clear information on how to style the items/content inside the DetailsList (https://developer.microsoft.com/en-us/fabric#/components/detailslist/customitemcolumns has an example) but no information on how to style the column headers (or if it's possible).
It seems like a pretty common use case (I'm trying to center my column headers instead of having them left aligned and make them larger), so not sure if I'm just missing something?
One option to customize column headers would be to override the rendering of headers via onRenderDetailsHeader event and then render header tooltip with a custom styling as demonstrated below
<DetailsList
items={sortedItems as any[]}
setKey="set"
columns={columns}
onRenderDetailsHeader={this.renderDetailsHeader}
/>
private renderDetailsHeader(detailsHeaderProps: IDetailsHeaderProps) {
return (
<DetailsHeader
{...detailsHeaderProps}
onRenderColumnHeaderTooltip={this.renderCustomHeaderTooltip}
/>
);
}
private renderCustomHeaderTooltip(tooltipHostProps: ITooltipHostProps) {
return (
<span
style={{
display: "flex",
fontFamily: "Tahoma",
fontSize: "14px",
justifyContent: "center",
}}
>
{tooltipHostProps.children}
</span>
);
}
Demo
You can style the columns headers with the IDetailsColumnStyles interface.
Example:
...
const headerStyle: Partial<IDetailsColumnStyles> = {
cellTitle: {
color: "#c00"
}
}
const columns: IColumn[] = [
{ styles: headerStyle, key: 'name', name: 'Name', fieldName: 'name', minWidth: 120 },
...
Look at the definition of IDetailsColumnStyles to see what can be styled.
The IColumn interface has a property named headerClassName which can be used to style the column header. Example:
/* CSS */
.headerClass > span {
/* right aligned header should have padding */
padding-right: 15px;
}
.headerClass span {
/* bolder font */
font-weight: 900;
/* Right Align the column header */
justify-content: flex-end;
text-align: right;
/* green color */
color: green;
/* background color */
background: pink;
}
//JSX
const columns = [
{
key: 'column1',
name: 'Name',
fieldName: 'name',
minWidth: 100,
maxWidth: 200,
isResizable: true,
heaerClassName: 'headerClass',
},
{
key: 'column2',
name: 'Value',
fieldName: 'value',
minWidth: 100,
maxWidth: 200,
isResizable: true,
},
];
<DetailsList
items={items}
columns={columns}
setKey='set'
/>
Demo

dojox.Grid row background color

I have a Grid and I would like to change the background color, do you know how to do it ?
Thanks for your help, have a nice day.
Here is my code :
var jsonStore = new dojo.data.ItemFileReadStore({url: "..." ?>"});
var model = new dojox.grid.data.DojoData(null,jsonStore,{jsId: 'model', rowsPerPage: 20, query: { date: '*' }});
var view1 = {
cells: [[
{name: 'x', width: "80px", field: "date"},
{name: 'y', width: "50px", field: "weight"},
{name: 'z', width: "100px", field: "nurse"}
]]
};
var layout = [ view1 ];
<div id="gridWeight" dojoType="dojox.Grid" model="model" structure="layout" autoWidth="true" style="height: 150px"></div>
You can either use the onStyleRow event or adapt the CSS directly - in your case:
.tundra #gridWeight .dojoxGridRow,
.tundra #gridWeight .dojoxGridRowOdd {
background: red;
}