Repeat code on button click with AngularJS - html

I want to create a page where you can add from 1 to n products dinamically.
I've been working with AngularJS for a couple of months but it's kind of difficult for me to think this.
The html code I tought about it's something like this:
https://plnkr.co/edit/2MYbkYeAf4lZSRIh2c8l?p=preview
Here it is the entire template:
<div class="container text-center">
<h1>Make a sale</h1>
<br>
<form>
<div class = "divedp">
<label for="idNumeroFactura" class="col-form-label" >Invoice number: </label>
<input name="inpNumeroFactura" id="idNumeroFactura" maxlength= "8" type="text" ng-model="numeroFactura" class= "form-control"/>
</div>
<h6 class="small">products</h6>
<div class = "divedp">
<label for="idProducto">Product: </label>
<select name="sltProducto" ng-options ="producto.nombreProducto for producto in productos"
ng-model="producto" class="form-control input-sm" id="idProducto">
<option value="">-Choose the product-</option>
</select>
<br>
<label for= "idCantidad" >Quantity: </label>
<select ng-model="cantidad" id="idCantidad" class="form-control input-sm">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<br>
<button ng-click="showDiv=true; agregarProducto()" class="btn btn-default" ng-hide="showDiv"> Add another product</button>
</div>
<div ng-show="showDiv"> Here should appear the new product</div>
<button ng-click="guardarVenta()" class="btn btn-primary" type="submit">Save sale</button>
</form>
<span >{{mensaje}}</span> <br>
</div>
And this is the part I want to repeat:
<div class = "divedp">
<label for="idProducto">Product: </label>
<select name="sltProducto" ng-options ="producto.nombreProducto for producto in productos"
ng-model="producto" class="form-control input-sm" id="idProducto">
<option value="">-Choose the product-</option>
</select>
<br>
<label for= "idCantidad" >Quantity: </label>
<select ng-model="cantidad" id="idCantidad" class="form-control input-sm">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<br>
<button ng-click="showDiv=true; agregarProducto()" class="btn btn-default" ng-hide="showDiv"> Add another product</button>
</div>
So when I click add another product button, all div code part should repeat below the first div. This way, the user can choose all products he want with the quantity he want.
I was thinking on creating a template, but this is already a template... I'm using ui-router to do the routering part, but I never repeated a template inside a template, and I never used a template inside a template at all.
Besides, this approach gives me some doubts:
How can I concatenate an attribute on a Json array? (I use Json to communicate between frontend and backend)
How can I avoid users to repeat the same product item? should I take this problem to an AngularJS controller or can I handle it with directives?
Thanks for reading.

Since you didn't show any controller, I can provide only abstract idea of solution. Here goes anyway.
Repeating
Simple looping over collection of data is done with ngRepeat. In your controller, you need to manipulate the collection and angular will propagate the change for you
<div ng-repeat="productBlock in productBlocks track by productBlock.id">
<!-- your repeated code here -->
<button ng-click="removeProductBlock(productBlock.id)"> remove </button>
</div>
<button ng-click="addProductBlock()"> add </button>
ngRepeat documentation
Prohibiting user from selecting duplicate product
You can achieve this, again, with manipulating a collection in your controller. This time, the collection would hold products still available to choose. Angular will help you with propagating the change from controller to your view with the ngOptions directive
<select
ng-options="product as product.label for product in productsAvailable track by product.id"
ng-change="selectProduct(product.id)">
</select>
The selectProduct method will be responsible for removing the product from productsAvailable collection.
Keep in mind that it also has to "put back" any products not being used on update.
ngOptions documentation

Related

Problem inserting data in database from option menu using ASP.NET CORE

I am trying to set data in a database using dot net core, everything else inserts fine except when it is trying to pick up the data from an option menu.
Here is the code from the view(partially, because I don't think the whole code is necessary, if it is, please tell me I will upload it.)
What am I doing wrong?
Thanks in advance <3
` <label asp-for="Seizure">Seizure:</label>
<select asp-for="Seizure" name="seizures" id="seizures">
<option value="fixeddecorative">Fixed / decorative hatch</option>
<option value="functional">Functional hatch</option>
</select>
</div>
<div class="form-group">
<label asp-for="HatchTrap">Hatch trap:</label>
<select asp-for="HatchTrap" name="hatchtraps" id="hatchtraps">
<option value="nowindcatcher">No wind catcher</option>
<option value="modelA">Model A</option>
<option value="modelB">Model B</option>
<option value="modelC">Model C</option>
</select>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>`
And here is the code of my controller:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("OrderId,Model,Color,Size,Seizure,HatchTrap,User")] ShutterOrders order)
{
if (ModelState.IsValid)
{
_context.Add(order);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Create));
}
return View(order);
}
The picked up values in the object:
Model binding looks through the sources for the name pattern prefix.property_name. If nothing is found, it looks for just property_name without the prefix.In your code,the name of the selectlist could not match the property in ShutterOrders model.
And you need know that asp-for will generate name and id html element by default.As a sample,if you use the following code:
<select asp-for="HatchTrap">
</select>
It will generate the html in the browser:
<select id="HatchTrap" name="HatchTrap">
</select>
So,to meet your requirement,just remove the name and id(id is optional,you could remove or keep it):
#model ShutterOrders
<form asp-action="Create">
//other properties...
<div class="form-group">
<label asp-for="Seizure">Seizure:</label>
<select asp-for="Seizure">
<option value="fixeddecorative">Fixed / decorative hatch</option>
<option value="functional">Functional hatch</option>
</select>
</div>
<div class="form-group">
<label asp-for="HatchTrap">Hatch trap:</label>
<select asp-for="HatchTrap">
<option value="nowindcatcher">No wind catcher</option>
<option value="modelA">Model A</option>
<option value="modelB">Model B</option>
<option value="modelC">Model C</option>
</select>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
Result:

A HTML form seems correct but doesn't submit

I've recently extended a working form I had but now the form doesn't submit. I thought perhaps it was due to the addition of the Select field, but on removing that the form still doesn't submit.
I then thought perhaps the Divs causing issues, but can't seem to work out why they would?
Without any additional DIVS etc. the form works as expected.
I've also tried adding an id to the form then form attributes to each input/select field but still no success.
<form action="jobs.php" class="search-form" method="get">
<div class="form-container">
<div class="box-select">
<div class="select">
<input type="text" name="key" placeholder="Keywords" style="padding: 28px 15px;" />
</div>
<div class="select">
<input type="text" name="location" placeholder="Location" style="padding: 28px 15px;" />
</div>
<div class="select">
<select name="category">
<option value="" disabled selected>Select Category</option>
<option value="">Accounts and Finance</option>
<option value="">Actuarial / Statistics</option>
<option value="">Architect / Interior Design</option>
<option value="">Arts / Creative / Media</option>
<option value="">Cleaning / Security / Maintenance</option>
<option value="">Clerical / Administrative</option>
<option value="">Construction / Real Estate</option>
<option value="">Customer Service / Telesales</option>
<option value="">Education / Lecturing</option>
<option value="">Executive / Professional / Management </option>
<option value="">Hotel / Restaurant / Travel / Leisure</option>
<option value="">Human Resources</option>
<option value="">Information Technologies / Telecommunication</option>
<option value="">Logistics / Shipping / Warehouse</option>
<option value="">Manufacturing - Production </option>
<option value="">Others</option>
<option value="">Public Relations & Communication </option>
<option value="">Purchasing / Procurement</option>
<option value="">Retail / Showroom / Counter / Exhibition Sales</option>
<option value="">Sales & Marketing</option>
<option value="">Technical and Engineering</option>
<option value="">Training and Development</option>
<option value="">Wellness / Fitness </option>
</select>
</div>
<div class="select">
<button class="button" type="button">Search</button>
</div>
</div>
</div>
Can someone point me at what I'm doing wrong here please.
As said in the docs
<input> elements of type submit are rendered as buttons. When the
click event occurs (typically because the user clicked the button),
the user agent attempts to submit the form to the server.
The type of your button needs to be submit if the form is to be submitted on button click.
Thus, replace
<button class="button" type="button">Search</button>
with
<button class="button" type="submit">Search</button>

Bootstrap 4 form not sending contents of select box

I've written a simple bootstrap 4 form with a GET method, and it only sends the values in the Submit button, not in the tag above it.
This is eventually to be handled in a POST request, but I've changed it to GET to more easily see what data the form is sending.
Here is the form code:
<form action="/picknumbers" method="GET">
<div class="form-group">
<label for="numberselect">Select one or more numbers</label>
<select multiple class="form-control mb-3" id="numberselect" size="6">
<option>1</option>
<option selected>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
</div>
<button name="submission" value="numberspicked" type="submit" class="btn btn-primary btn-block">Choose numbers</button>
</form>
The output in the browser address bar is simply: /picknumbers?submission=numberspicked, without including the numbers picked by the user. How do I get the submitted form to include them?
You need to give a value on the options also you need to give a name to the select. To clarify, see the code below:
<select multiple class="form-control mb-3" name="select-name" id="numberselect" size="6">
<option value="1">1</option>
<option value="2" selected>2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
Your select options appear to be missing value attribute tags. It should look like the below:
<div class="form-group">
<label for="numberselect">Select one or more numbers</label>
<select multiple class="form-control mb-3" id="numberselect" size="6">
<option value="1">1</option>
<option value="2" selected>2</option>
...
</select>
</div>
<button name="submission" value="numberspicked" type="submit" class="btn btn-primary btn-block">Choose numbers</button>
</form>

Pass Multiple Query String in Python

So, I'm building a web app using flask/jinja and I have a webpage that displays a list of data that includes date, and I currently have two drop down menus, one that controls the date range of data viewed and the other that sorts the data based on various parameters.
<form method='get' action=''>
<button class='btn btn-default'>View data from:</button>
<select name = "time_length">
<option value="0">All</option>
<option value="7">Week</option>
<option value="30">Month</option>
</select>
</form>
</div>
<div>
<form method='get' action=''>
<button class='btn btn-default'>sort:</button>
<select name = "sortby">
<option value="user">Name</option>
<option value="group">Project</option>
<option value="date">Date</option>
</select>
</form>
Individually, this works fine because the it just passes along ?time_length=7 or whatever to the url, but I can't figure out how to pass along both parameters. One ends up replacing the other in the link.
You need to have them sent from the same <form>.
<form method='get' action=''>
<button class='btn btn-default'>submit</button>
<select name = "time_length">
<option value="0">All</option>
<option value="7">Week</option>
<option value="30">Month</option>
</select>
<select name = "sortby">
<option value="user">Name</option>
<option value="group">Project</option>
<option value="date">Date</option>
</select>
</form>

How do I handle multiple values from a single select field in Sinatra?

I have an HTML form with a element in a page that part of a Sinatra app, e.g.
<form action="/form" method="post">
<p>
<label for="text">Text</label>
<input type="text" name="text" id="text">
</p>
<p>
<label for="select">Selection</label>
<select name="select" id="select">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</p>
<p>
<label for="multi_select" multiple>MultipleSelection</label>
<select name="multi_select" id="multi_select" multiple>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</p>
<p>
<input type="submit" name="submit" value="Submit">
</p>
</form>
If the user selects A & C from the multi_select, the POST handler in Sinatra is supposed to return the selected values, but because params is a hash, it only returns the last selected value (so params[:multi_select] = "C").
For various reasons, I can't use Javascript or other front-end tricks to change how the value is sent. Is there a good way to handle this correctly server-side? I haven't worked much with Sinatra prior to this project.
This discussion implies that the name of the multiselect needs to look like an array for Sinatra to pick up all the values. Try changing it to:
<select name="multi_select[]" id="multi_select" multiple>