I'm building an app that shows company "x" pay rates for different departments within the company. The problem I'm running into is being able to create a ForEach that will iterate over all the pay rate array data. Currently, when you click on "Development", "Training" or "Marketing" the pay rates that display are for "Development." The higher pay rates for "Training" and "Marketing" do not display. My model data is organized to be an array ([Dept]) of arrays ([rates]). I have not been able to find any documentation or tutorials that explain how to iterate over an array within an array.
Here is my PayRateDetailView:
struct RateDetailView: View {
var dept: Dept
var body: some View {
List {
ForEach(dept.rates) { line in
RateCardView(dept: dept)
}
}
.navigationTitle(dept.deptName)
}
}
Here is my RateCardView:
struct RateCardView: View {
var dept: Dept
var body: some View {
ForEach(dept.rates) { rate in
HStack {
Text("Year \(rate.year)")
Spacer()
Text("$ \(rate.shiftWorkerRate, specifier: "%.2f")")
Spacer()
Text("$ \(rate.managerRate, specifier: "%.2f")")
}
}
}
}
Here is my PayRateView:
struct PayRateView: View {
var depts: [Dept]
var dept: Dept
var body: some View {
List {
ForEach(depts, id: \.deptName) { name in
NavigationLink(destination:
RateCardView(dept: dept)) {
Label(name.deptName, systemImage: "person")
}
}
}
}
}
I'm thinking the answer to my problem may be in finding a better way to display my pay rate model data, but this is the structure that seems best for what I'm trying to display:
struct Dept: Identifiable {
let id: UUID
var deptName: String
var rates: [HourlyRates]
init(id: UUID = UUID(), deptName: String, rates: [HourlyRates]) {
self.id = id
self.deptName = deptName
self.rates = rates
}
}
extension Dept {
static let deptList: [Dept] = [
Dept(deptName: "Development", rates: [
HourlyRates(year: 1, shiftWorkerRate: 10, managerRate: 20),
HourlyRates(year: 2, shiftWorkerRate: 11, managerRate: 21),
HourlyRates(year: 3, shiftWorkerRate: 12, managerRate: 22),
HourlyRates(year: 4, shiftWorkerRate: 13, managerRate: 23),
HourlyRates(year: 5, shiftWorkerRate: 14, managerRate: 24),
HourlyRates(year: 6, shiftWorkerRate: 15, managerRate: 25),
HourlyRates(year: 7, shiftWorkerRate: 16, managerRate: 26)]),
Dept(deptName: "Training", rates: [
HourlyRates(year: 1, shiftWorkerRate: 12, managerRate: 22),
HourlyRates(year: 2, shiftWorkerRate: 13, managerRate: 23),
HourlyRates(year: 3, shiftWorkerRate: 14, managerRate: 24),
HourlyRates(year: 4, shiftWorkerRate: 15, managerRate: 25),
HourlyRates(year: 5, shiftWorkerRate: 16, managerRate: 26),
HourlyRates(year: 6, shiftWorkerRate: 17, managerRate: 27),
HourlyRates(year: 7, shiftWorkerRate: 18, managerRate: 28)]),
Dept(deptName: "Marketing", rates: [
HourlyRates(year: 1, shiftWorkerRate: 12, managerRate: 22),
HourlyRates(year: 2, shiftWorkerRate: 13, managerRate: 23),
HourlyRates(year: 3, shiftWorkerRate: 14, managerRate: 24),
HourlyRates(year: 4, shiftWorkerRate: 15, managerRate: 25),
HourlyRates(year: 5, shiftWorkerRate: 16, managerRate: 26),
HourlyRates(year: 6, shiftWorkerRate: 17, managerRate: 27),
HourlyRates(year: 7, shiftWorkerRate: 18, managerRate: 28)])
]
}
struct HourlyRates: Identifiable {
let id: UUID
var year: Int
var shiftWorkerRate: Double
var managerRate: Double
init(id: UUID = UUID(), year: Int, shiftWorkerRate: Double, managerRate: Double) {
self.id = id
self.year = year
self.shiftWorkerRate = shiftWorkerRate
self.managerRate = managerRate
}
}
All works well for me, without using the RateDetailView that you call PayRateDetailView,
and minor mods in PayRateView.
Here is my test code, that shows the "Development", "Training" and "Marketing" pay rates are displayed as expected.
struct ContentView: View {
var body: some View {
NavigationStack { // <-- here
PayRateView()
}
}
}
struct RateCardView: View {
var dept: Dept
var body: some View {
ForEach(dept.rates) { rate in
HStack {
Text("Year \(rate.year)")
Spacer()
Text("$ \(rate.shiftWorkerRate, specifier: "%.2f")")
Spacer()
Text("$ \(rate.managerRate, specifier: "%.2f")")
}
}
}
}
struct PayRateView: View {
var depts: [Dept] = Dept.deptList
var dept: Dept = Dept(deptName: "", rates: [])
var body: some View {
List {
ForEach(depts, id: \.deptName) { dept in // <-- here
NavigationLink(destination: RateCardView(dept: dept)) {
Label(dept.deptName, systemImage: "person") // <-- here
}
}
}
}
}
struct Dept: Identifiable {
let id: UUID
var deptName: String
var rates: [HourlyRates]
init(id: UUID = UUID(), deptName: String, rates: [HourlyRates]) {
self.id = id
self.deptName = deptName
self.rates = rates
}
}
extension Dept {
// here for testing
static let deptList: [Dept] = [
Dept(deptName: "Development", rates: [
HourlyRates(year: 1, shiftWorkerRate: 0, managerRate: 20),
HourlyRates(year: 2, shiftWorkerRate: 0, managerRate: 21),
HourlyRates(year: 3, shiftWorkerRate: 0, managerRate: 22),
HourlyRates(year: 4, shiftWorkerRate: 0, managerRate: 23),
HourlyRates(year: 5, shiftWorkerRate: 0, managerRate: 24),
HourlyRates(year: 6, shiftWorkerRate: 0, managerRate: 25),
HourlyRates(year: 7, shiftWorkerRate: 0, managerRate: 26)]),
Dept(deptName: "Training", rates: [
HourlyRates(year: 1, shiftWorkerRate: 1, managerRate: 22),
HourlyRates(year: 2, shiftWorkerRate: 1, managerRate: 23),
HourlyRates(year: 3, shiftWorkerRate: 1, managerRate: 24),
HourlyRates(year: 4, shiftWorkerRate: 1, managerRate: 25),
HourlyRates(year: 5, shiftWorkerRate: 1, managerRate: 26),
HourlyRates(year: 6, shiftWorkerRate: 1, managerRate: 27),
HourlyRates(year: 7, shiftWorkerRate: 1, managerRate: 28)]),
Dept(deptName: "Marketing", rates: [
HourlyRates(year: 1, shiftWorkerRate: 2, managerRate: 22),
HourlyRates(year: 2, shiftWorkerRate: 2, managerRate: 23),
HourlyRates(year: 3, shiftWorkerRate: 2, managerRate: 24),
HourlyRates(year: 4, shiftWorkerRate: 2, managerRate: 25),
HourlyRates(year: 5, shiftWorkerRate: 2, managerRate: 26),
HourlyRates(year: 6, shiftWorkerRate: 2, managerRate: 27),
HourlyRates(year: 7, shiftWorkerRate: 2, managerRate: 28)])
]
}
struct HourlyRates: Identifiable {
let id: UUID
var year: Int
var shiftWorkerRate: Double
var managerRate: Double
init(id: UUID = UUID(), year: Int, shiftWorkerRate: Double, managerRate: Double) {
self.id = id
self.year = year
self.shiftWorkerRate = shiftWorkerRate
self.managerRate = managerRate
}
}
Related
I'm looking for a way to swap 2 bits at a given position but counting starts from MSB(most significant bit) to LSB (least significant bit).
Lets say i have position p1 = 0 and p2 = 2 and my number is 1000 = 8 . The result should be
0010 .
I tried this piece of code but it swaps the bits starting from LSB to MSB . How do I "reverse" the process?
unsigned int bit1 = (num >> p1) & 1;
unsigned int bit2 = (num >> p2) & 1;
unsigned int x = (bit1 ^ bit2);
x = (x << p1) | (x << p2);
result = num ^ x;
By your example, I will assume you mean to start numbering bits from the MSB of the number value. That is, given:
00010100
↑
msb == most significant bit in the number
In that case, you need a way to count the bit index of the MSB. There are actually processor instructions you can use to do that, and both MSVC and GCC (and Clang) provide special functions to access that functionality...
...but you don’t need that. Instead, just write yourself a function:
int index_of_msb( unsigned long value )
{
...
}
Remember that you can shift a number down by one bit at a time. As long as the number is not zero, you have at least one set bit left.
1 0 1 0 0 0
→ 1 0 1 0 0 (1 shift)
→ 1 0 1 0 (2 shifts)
→ 1 0 1 (3 shifts)
→ 1 0 (4 shifts)
→ 1 (5 shifts)
→ 0
Five shifts → bit 5 is MSB.
Now you can convert your bit positions to the standard shift offsets.
p1 = p_msb - p1;
p2 = p_msb - p2;
What remains to do is use your standard bit operators to swap the two bit values.
Four bits is small enough that you can use a 256-byte table:
unsigned char SwapBits(unsigned char number, int p0, int p1)
{
static const unsigned char Table[16][4][4] = {
{ { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0} },
{ { 1, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0} },
{ { 2, 1, 0, 0}, { 1, 2, 2, 2}, { 0, 2, 2, 2}, { 0, 2, 2, 2} },
{ { 3, 3, 1, 1}, { 3, 3, 2, 2}, { 1, 2, 3, 3}, { 1, 2, 3, 3} },
{ { 4, 2, 1, 0}, { 2, 4, 4, 4}, { 1, 4, 4, 4}, { 0, 4, 4, 4} },
{ { 5, 3, 5, 1}, { 3, 5, 6, 5}, { 5, 6, 5, 4}, { 1, 5, 4, 5} },
{ { 6, 6, 3, 2}, { 6, 6, 5, 4}, { 3, 5, 6, 6}, { 2, 4, 6, 6} },
{ { 7, 7, 7, 3}, { 7, 7, 7, 5}, { 7, 7, 7, 6}, { 3, 5, 6, 7} },
{ { 8, 4, 2, 1}, { 4, 8, 8, 8}, { 2, 8, 8, 8}, { 1, 8, 8, 8} },
{ { 9, 5, 3, 9}, { 5, 9, 9, 12}, { 3, 9, 9, 10}, { 9, 12, 10, 9} },
{ {10, 6, 10, 3}, { 6, 10, 12, 10}, {10, 12, 10, 9}, { 3, 10, 9, 10} },
{ {11, 7, 11, 11}, { 7, 11, 13, 14}, {11, 13, 11, 11}, {11, 14, 11, 11} },
{ {12, 12, 6, 5}, {12, 12, 10, 9}, { 6, 10, 12, 12}, { 5, 9, 12, 12} },
{ {13, 13, 7, 13}, {13, 13, 11, 13}, { 7, 11, 13, 14}, {13, 13, 14, 13} },
{ {14, 14, 14, 7}, {14, 14, 14, 11}, {14, 14, 14, 13}, { 7, 11, 13, 14} },
{ {15, 15, 15, 15}, {15, 15, 15, 15}, {15, 15, 15, 15}, {15, 15, 15, 15} },
};
return Table[number][p0][p1];
}
I want to change it with as little code as possible.
The code below can change the color, but these are long-winded.
import { createTheme } from "#mui/material/styles";
const shadowKeyUmbraOpacity = 0.05;
const shadowKeyPenumbraOpacity = 0.035;
const shadowAmbientShadowOpacity = 0.03;
function createShadow(...px: number[]) {
return [
`${px[0]}px ${px[1]}px ${px[2]}px ${px[3]}px rgba(0,0,0,${shadowKeyUmbraOpacity})`,
`${px[4]}px ${px[5]}px ${px[6]}px ${px[7]}px rgba(0,0,0,${shadowKeyPenumbraOpacity})`,
`${px[8]}px ${px[9]}px ${px[10]}px ${px[11]}px rgba(0,0,0,${shadowAmbientShadowOpacity})`,
].join(',');
}
export const theme = createTheme({
// Values from https://github.com/material-components/material-components-web/blob/be8747f94574669cb5e7add1a7c54fa41a89cec7/packages/mdc-elevation/_variables.scss
shadows: [
'none',
createShadow(0, 2, 1, -1, 0, 1, 1, 0, 0, 1, 3, 0),
createShadow(0, 3, 1, -2, 0, 2, 2, 0, 0, 1, 5, 0),
createShadow(0, 3, 3, -2, 0, 3, 4, 0, 0, 1, 8, 0),
createShadow(0, 2, 4, -1, 0, 4, 5, 0, 0, 1, 10, 0),
createShadow(0, 3, 5, -1, 0, 5, 8, 0, 0, 1, 14, 0),
createShadow(0, 3, 5, -1, 0, 6, 10, 0, 0, 1, 18, 0),
createShadow(0, 4, 5, -2, 0, 7, 10, 1, 0, 2, 16, 1),
createShadow(0, 5, 5, -3, 0, 8, 10, 1, 0, 3, 14, 2),
createShadow(0, 5, 6, -3, 0, 9, 12, 1, 0, 3, 16, 2),
createShadow(0, 6, 6, -3, 0, 10, 14, 1, 0, 4, 18, 3),
createShadow(0, 6, 7, -4, 0, 11, 15, 1, 0, 4, 20, 3),
createShadow(0, 7, 8, -4, 0, 12, 17, 2, 0, 5, 22, 4),
createShadow(0, 7, 8, -4, 0, 13, 19, 2, 0, 5, 24, 4),
createShadow(0, 7, 9, -4, 0, 14, 21, 2, 0, 5, 26, 4),
createShadow(0, 8, 9, -5, 0, 15, 22, 2, 0, 6, 28, 5),
createShadow(0, 8, 10, -5, 0, 16, 24, 2, 0, 6, 30, 5),
createShadow(0, 8, 11, -5, 0, 17, 26, 2, 0, 6, 32, 5),
createShadow(0, 9, 11, -5, 0, 18, 28, 2, 0, 7, 34, 6),
createShadow(0, 9, 12, -6, 0, 19, 29, 2, 0, 7, 36, 6),
createShadow(0, 10, 13, -6, 0, 20, 31, 3, 0, 8, 38, 7),
createShadow(0, 10, 13, -6, 0, 21, 33, 3, 0, 8, 40, 7),
createShadow(0, 10, 14, -6, 0, 22, 35, 3, 0, 8, 42, 7),
createShadow(0, 11, 14, -7, 0, 23, 36, 3, 0, 9, 44, 8),
createShadow(0, 11, 15, -7, 0, 24, 38, 3, 0, 9, 46, 8),
],
});
createShadow function and others referred to the link below.
https://github.com/mui/material-ui/blob/master/packages/mui-material/src/styles/shadows.js
So, I want to know how to change it more wisely.
I am trying to add custom data for month, week and day view. I created custom event file where I separate date and date time for week's event.
I am able to display data if I export event file like this
export default [
{
progress: '80',
title: 'streak minus one',
end: new Date(2022, 2, 21, 10, 8, 0),
start: new Date(2022, 2, 21, 10, 0, 0),
resource: {
month: {
status: true,
progress: 80
}
}
},
{
progress: '80',
title: 'streak minus one',
end: new Date(2022, 2, 21, 10, 19, 0),
start: new Date(2022, 2, 21, 10, 9, 0),
resource: {
month: {
status: true,
progress: 80
}
}
}
]
But not like this
export default [
{
title: 'streak minus one',
end: new Date(2022, 2, 21),
start: new Date(2022, 2, 21),
resource: {
month: {
progress: 70
},
week: [
{
endDateTime: new Date(2022, 2, 21, 10, 8, 0),
startDateTime: new Date(2022, 2, 21, 10, 0, 0)
},
{
endDateTime: new Date(2022, 2, 21, 10, 19, 0),
startDateTime: new Date(2022, 2, 21, 10, 9, 0)
},
{
endDateTime: new Date(2022, 2, 21, 10, 26, 0),
startDateTime: new Date(2022, 2, 21, 10, 19, 0)
},
{
endDateTime: new Date(2022, 2, 21, 10, 33, 0),
startDateTime: new Date(2022, 2, 21, 10, 26, 0)
}
]
}
},
{
title: 'second event',
end: new Date(2022, 2, 23),
start: new Date(2022, 2, 23),
resource: {
month: {
status: true,
progress: 45
},
week: [
{
endDateTime: new Date(2022, 2, 22, 10, 15, 0),
startDateTime: new Date(2022, 2, 22, 10, 0, 0)
}
]
}
}
]
How to display events by fetching it from resource on Week View in React-big-calendar
React-Big-Calendar expects the events individual 'event' data structures to be consistent, and uses the same data 'accessors' (startAccessor, endAccessor, titleAccessor) in all views. You can use onRangeChange to reload your events, but it would still expect the data structures to be the same.
I am fetching data in React using axios inside a useEffect hook. When I console log the data from inside the useEffect hook everything is fine, but when I attempt to access it in the return statement, it returns Cannot read property 'Con' of undefined.
Here is my code:
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState([]);
useEffect(() => {
axios.get('/api/comRes', {headers:{"Access-Control-Allow-Origin": "http://localhost:3000/"}})
.then(function (data) {
setData(data.data);
console.log(data.data.Con[0])
})}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data.data.Con[0]}</div>
</div>
);
this is the console.log of of the whole datset:
{_id: "5e85c1241c9d440000e730c7", Con: Array(30), Lab: Array(30), LibDem: Array(30), Other: Array(30), …}
_id: "5e85c1241c9d440000e730c7"
Con: (30) [47, 50, 44, 33, 35, 41, 43, 54, 63, 34, 42, 59, 49, 48, 52, 39, 39, 45, 32, 39, 49, 43, 37, 47, 50, 46, 57, 45, 57, 56]
Lab: (30) [31, 28, 34, 47, 42, 33, 35, 23, 19, 44, 34, 21, 28, 29, 34, 36, 39, 31, 11, 29, 33, 45, 52, 41, 30, 38, 28, 29, 25, 22]
LibDem: (30) [9, 9, 10, 9, 11, 11, 8, 8, 9, 10, 9, 9, 14, 9, 4, 8, 8, 10, 6, 5, 10, 8, 5, 7, 11, 7, 9, 14, 11, 14]
Other: (30) [0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0]
Brexit: (30) [3, 3, 2, 0, 3, 2, 4, 3, 1, 2, 3, 2, 1, 2, 4, 4, 2, 4, 1, 4, 3, 3, 2, 2, 5, 3, 2, 4, 1, 3]
Green: (30) [4, 4, 4, 6, 3, 5, 5, 5, 2, 4, 5, 3, 4, 4, 3, 4, 4, 5, 3, 1, 4, 2, 3, 3, 4, 5, 4, 8, 4, 5]
SNP: (30) [4, 5, 4, 4, 4, 8, 3, 6, 3, 4, 5, 4, 3, 4, 4, 6, 6, 3, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Plaid: (30) [1, 1, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 0, 2, 0, 1, 2, 1, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Date: "20/02/2020"
__proto__: Object
This is the eror report when I try to access {Data.Con[0]} from the return statement:
TypeError: Cannot read property '0' of undefined
First time when your component renders, the data might not be there yet.
My suggestion would be to set the data to null by default, so change this line
const [data, setData] = useState([]);
to
const [data, setData] = useState(null);
Then, before trying to rendering something, check it is there
<div className="dashboard">{data ? data.Con[0] : null }</div>
If you will still have issues, you might want to post the axios's response data structure.
Update #1
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState();
useEffect(() => {
axios.get('/api/comRes', { headers:
{ "Access-Control-Allow-Origin": "http://localhost:3000/" }
}).then(function (data) {
setData(data.data);
})
}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data ? data.Con[0] : null}</div>
</div>
);
}
It should just be {data.Con[0]}
The angularjs-slider should display values without commans, and only on the min max values display comma values.
Example: [6.5, 7, 8, .... , 31, 31.5]
If I set
$scope.slider = {
value: 31.5,
options: {
floor: 6.5,
ceil: 31.5,
step: 1,
precision: 1
}
};
the values are [6.5, 7.5, 8.5 ... 31.5]
How can I set this?
Found it: i have to set the stepsArray option, with the values I want to select.
Example:
var myApp = angular.module('myapp', ['rzModule']);
myApp.controller('TestController', TestController);
function TestController() {
var vm = this;
vm.priceSlider = {
value: 31.5,
options: {
floor: 6.5,
ceil: 31.5,
step: 1,
precision: 1,
stepsArray: [6.5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 31.5]
}
}
}
Fiddler: http://jsfiddle.net/w3ve98eL/1/