Svelte input bind JSON array - arrays

I followed this svelte tutorial and I wonder if I could do this group binding to a JSON array instead of simple array, for example I would like to do a planets selector:
<script>
let planets = [{
name: 'Jupiter',
enable: false
},
{
name: 'Saturn',
enable: false
},
{
name: 'Uran',
enable: false
},
{
name: 'Neptun',
enable: false
},
{
name: 'Pluto',
enable: true
},
];
$: planets, console.log(planets)
</script>
{#each planets as planet}
<label>
<input type=checkbox bind:group={planets} name={planet.name} value={planet}>
{planet.name}
</label>
{/each}
This is a REPL. I wonder if there is a way how to properly bind JSON array (enable array field with inputs value) in svelte each loop. Now it pops out with each item as you click as you can see in REPL's console.log and I would like just to uncheck it.
With bind:checked property displays proper way
<input type=checkbox bind:checked={planet.enable} value={planet}>
But this does not change array.enable value on click. Can I achieve responsibility of planets array here?

I think the problem is that you set the value to planet. When you remove this and only use the checked property, then the array updates with the proper value.
<script>
let planets = [{
name: 'Jupiter',
enable: false,
},
{
name: 'Saturn',
enable: false
},
{
name: 'Uran',
enable: false
},
{
name: 'Neptun',
enable: false
},
{
name: 'Pluto',
enable: true
},
];
$: planets, console.log(planets)
</script>
{#each planets as planet}
<label>
<input type=checkbox bind:checked={planet.enable}>
{planet.name}
</label>
{/each}
You can checkout my REPL.
Here is a screenshot from the update:

Related

Looping same field in the same form (display as list) then all of the results get pushed into an array when submitted - mongodb, express, ejs, node js

I am really stuck with getting the issue resolved. I am very new to coding and hasn't been able to find any solution online.
These are the fields that I wish to be an array. I have created multiple inputs such as PIC#2 etc.
I am linking this model to another model schema so I would like that when I have selected the company, their corresponding PICs will be in another dropdown list. At the moment, it is showing all PIC1 which includes other companies' staffs. Please help out if you can make the coding simpler! Thank you in advance!
html
<div class="mb-3 col">
<label class="form-label" for="pic"><b>Person In Charge (PIC) #1</b></label>
<input class="form-control"type="text" id="pic" name="company[pic]" required>
</div>
<div class="row">
<div class="mb-3 col">
<label class="form-label" for="mobile"><b>Mobile</b></label>
<input class="form-control"type="text" id="mobile" name="company[mobile]" required>
</div>
<div class="mb-3 col">
<label class="form-label" for="email"><b>Email</b></label>
<input class="form-control"type="email" id="email" name="company[email]" required>
</div>
</div>
model schema
const companySchema = new Schema({
name: [{
type: String,
uppercase: true,
}],
code: String,
pic: [{
type: String,
uppercase: true,
}],
mobile: String,
email: [{
type: String,
lowercase: true,
}],
pic1: [{
type: String,
uppercase: true,
}],
pic2: [{
type: String,
uppercase: true,
}],
pic3: [{
type: String,
uppercase: true,
}],
pic4: [{
type: String,
uppercase: true,
}],
pic5: [{
type: String,
uppercase: true,
}],
pic6: [{
type: String,
uppercase: true,
}],
pic7: [{
type: String,
uppercase: true,
}],
pic8: [{
type: String,
uppercase: true,
}],
pic9: [{
type: String,
uppercase: true,
}],
pic10: [{
type: String,
uppercase: true,
}],
mobile1: String,
mobile2: String,
mobile3: String,
mobile4: String,
mobile5: String,
mobile6: String,
mobile7: String,
mobile8: String,
mobile9: String,
mobile10: String,
email1: [{
type: String,
lowercase: true,
}],
email2: [{
type: String,
lowercase: true,
}],
email3: [{
type: String,
lowercase: true,
}],
email4: [{
type: String,
lowercase: true,
}],
email5: [{
type: String,
lowercase: true,
}],
email6: [{
type: String,
lowercase: true,
}],
email7: [{
type: String,
lowercase: true,
}],
email8: [{
type: String,
lowercase: true,
}],
email9: [{
type: String,
lowercase: true,
}],
email10: [{
type: String,
lowercase: true,
}],
remarks: String,
status: String,
upload: String,
companyimage: String
});
route
app.get("/companies/new", (req,res) => {
res.render("companies/new")
});
app.post("/companies",async(req,res) => {
// res.send(req.body)
const company = new Company(req.body.company);
await company.save();
res.redirect(`/companies/${company._id}`)
});
Since I don't have much context on what you're trying to achieve, I'll go through all I can think of to best answer this thread
| NodeJS is not a front-end framework
NodeJS is a JavaScript runtime environment, which roughly means it reads and executes JS code you're giving it. A front-end framework is a set of tools and method that greatly helps you to code faster. It generally gives you higher level components so that you don't have to deal with low-level JS code yourself. Some examples are ReactJS, VueJS, Angular, etc...
I strongly advise you to take a look at these if you're going to build a web-app.
| You might want to rework your database model
I won't tell you to rework your entire base code because that can be tiring and that's not the point of the question.
I'd put all PICs in a single array instead of having 10 fields, same thing goes for emails and mobiles. We can chat about it later on if you want to.
| Answering the question
Given that I don't have much context about the stack used regarding the front-end, I sticked to plain JS and HTML. Here's one method (among many others):
index.js (to put front-end side)
//initializing the company object
const addedData = {
id: 1,
company: 'companyname',
pic: [],
mobile: [],
email: []
}
//getting the buttons elements
const addPIC = document.getElementById('addpic');
const form = document.getElementById('sendform');
//listening on click event on the form
form.addEventListener('click', (e) => {
// do what you have to here, e.g sending form
const res = fetch("http://localhost:3000/companies", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(picArray)
}).then(data => {
console.log(data);
});
});
// listening on click event when adding data
addPIC.addEventListener('click', (e) => {
//getting input tags
const pic = document.getElementById('pic');
const mobile = document.getElementById('mobile');
const email = document.getElementById('email');
//pushing into respective arrays
addedData.pic.push(pic.value);
addedData.mobile.push(mobile.value);
addedData.email.push(email.value);
//resetting values to ease the process of mass adding PICs
pic.value = '';
mobile.value = '';
email.value = '';
//console.log(addedData);
});
and your html file (took your original file and just added 2 buttons, that's why it's not formatted properly):
<div class="mb-3 col">
<label class="form-label" for="pic"><b>Person In Charge (PIC) #1</b></label>
<input class="form-control"type="text" id="pic" name="company[pic]" required>
</div>
<div class="row">
<div class="mb-3 col">
<label class="form-label" for="mobile"><b>Mobile</b></label>
<input class="form-control"type="text" id="mobile" name="company[mobile]" required>
</div>
<div class="mb-3 col">
<label class="form-label" for="email"><b>Email</b></label>
<input class="form-control"type="email" id="email" name="company[email]" required>
</div>
</div>
<button id="addpic">Add PIC</button>
<button id="sendform">Send form</button>
</div>
Now your back-end will receive the datas, but you'll have to process it once again to match your database model, which is not optimal.

How pass data to polymer from server side

in polymer can I do that. How I can read val in my polymer. I create c# project I need write polymer values from server and I not need write my values inside polymer attrs I have complex text in values it may be break polymer attrs so I not need do this way.
<ctr-textbox name="Name">
<val>joman mahmoud</val>
</ctr-textbox>
my polymer is
<template>
<input type="text" dir="auto" name="{{name}}" value="{{value}}" class$="{{fnJoin('form-control ', class)}}" />
</template>
<script>
Polymer({
is: "ctr-textbox",
properties: {
type: {
type: String,
value: "ctr",
reflectToAttribute: true
},
name: {
type: String,
value: "",
reflectToAttribute: true
},
class: {
type: String,
value: '',
reflectToAttribute: true
},
value: {
type: String,
value: ''
}
},
ready: function () {
}
});
</script>

How can i bind dynamic array data to multiselect?

I am using a bootstrap multiselect plugin by david stutz.
Here is my html
<select class="multiselect" name="multiselect[]" id="multiselect-employee" ng-model="taskData.AssignedId" ng-options="emp.Name for emp in employeelistoptions" multiple="multiple" multiselect-dropdown></select>
here is my script
$(document).ready(function () {
$('#add-new-task').on('click', function () {
$('#multiselect-employee').multiselect({
nonSelectedText: 'Select an employee!',
includeSelectAllOption: true,
enableFiltering: true
});
clearValues(scope);
});
});
employeelist options is a dynamic array which stores data
$scope.employeelistoptions =[ {
ProfileId:0 ,
Name: ""
}];
Now the multiselect doesn't works if employeelistoptions is used. but it works when some array like employeeList(Which is not dynamic) is used.
$scope.employeeList = [{
ProfileId: 1,
Name: "Antony"
}, {
ProfileId: 2,
Name: "Amal"
}, {
ProfileId: 3,
Name: "Sachin"
}];
How can i bind dynamic array data to multiselect?

Validate that all of the checkboxes are checked in ionic

I have a form with list of checkboxes, as shown here:
$scope.deviceList = [
{ text: "Dev 0", checked: false },
{ text: "Dev 1", checked: false },
{ text: "Dev 2", checked: false },
{ text: "Dev 3", checked: false },
{ text: "Dev 4", checked: false }
];
<form>
<ion-checkbox class="checkbox-balanced"
ng-repeat="item in deviceList"
ng-model="item.checked"
ng-required="true">
{{ item.text }}
</ion-checkbox>
</form>
Of course that I have more elements. but just for this case I show the relavent code.
Now, I would like to have a validation that the form cannot be sent until all checkboxes are checked.
Any suggestions of an elegant solution for that?
Thanks in advance
Maybe a function with something like the following would do the trick:
$scope.validate = function(){
var numChecked = $filter($scope.deviceList, function(device) {
return device.checked
}).length;
return $scope.deviceList.length == numChecked;
}
And don't forget to inject $filter service in your controller or it won't work

Exclude the hidden elements from limitTo in ng-repeat

Need Help.Here is the solution I have been looking for..
I am trying to show 6 elements from an array excluding the hidden elements. ie: If there are two hidden elements technically it will have to show 8 elements. I dont want to increment the scope object bound to limitTo as it is doing lot of complication. Is there any way that I can use a filter to keep the limitTo to a constant and bypassing limitTo filter if the element is hidden. I have been looking for the solution for a couple of days. Nothing worked out yet. I would really appreciate if someone can help on this.
<div data-ng-repeat="user in vm.userList | limitTo: '6" ng-hide="user.checked">
<input type="checkbox" ng-model="user.checked"> {{user.name}}</input>
</div>
You need to change a bit approach. Instead of ng-hide use filter please see sample below
var app = angular.module('app', []);
app.controller('fCtrl', function($scope) {
var vm = this;
vm.userList = [
{
name: 1,
checked: true
}, {
name: 2,
checked: false
}, {
name: 3,
checked: true
}, {
name: 4,
checked: false
}, {
name: 5,
checked: false
}, {
name: 6,
checked: false
}, {
name: 7,
checked: false
}, {
name: 8,
checked: false
}, {
name: 9,
checked: false
}, {
name: 10,
checked: false
}, {
name: 11,
checked: false
},
];
return vm;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="fCtrl as vm">
<div data-ng-repeat="user in vm.userList | filter: {checked:false}| limitTo: 6">
{{$index+1}})
<input type="checkbox" ng-model="user.checked">{{user.name}}</input>
</div>
</div>
</div>

Resources