This is my category list
$scope.categories= [{"category_id":"1","category_name":"sports"},{"category_id":"2","category_name":"casual"},{"category_id"
:"3","category_name":"formal"},{"category_id":"4","category_name":"party wear"},{"category_id":"5","category_name"
:"winter"},{"category_id":"9","category_name":"summer"}]
The product can have multiple categories.
$scope.product_categories=[{"category_id":"3"},{"category_id":"4"},{"category_id":"5"}]
I have these two array first array categories holds all the category.
and second array holds the category which that product has.
I have select tag where all the categories are listed at the time of adding the product.user can select multiple product.
{{category.category_name}}
Now suppose I have added one product with 3 category 3,4,5 respectively.
When I trying to edit that product these 3,4,5 category must be selected because this product is related with these category. So this is my code which is not working.
<select multiple="" class="form-control" name="category_list[]" ng-model="categories" >
<option ng-selected="{{category.category_id == product_categories}}"
ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
I am confused here how to do the multiple selection when I have array with array.Categories 3,4,5 must be selected among the category.if id do
ng-selected="{{category.category_id ==5}}" like this only 5 category is get selected.how to do the multiple selection with array or multiple values?
I have a solution, use $scope.$watch
$scope.$watch('product_categories', function (nowSelected) {
$scope.selectedValues = [];
if (!nowSelected) {
// here we've initialized selected already
// but sometimes that's not the case
// then we get null or undefined
return;
}
angular.forEach(nowSelected, function (val) {
$scope.selectedValues.push(val.category_id.toString());
});
});
And apply selectedValues via following:
<select multiple ng-model="selectedValues" style="width: 50%;" size="7">
<option ng-repeat="category in categories" value="{{category.category_id}}" ng-selected="{{selectedValues.indexOf(category.category_id.toString())!=-1}}">{{category.category_name}}</option>
</select>
==> Full code at Plunker multi selection ng-selected
Hope it helps!
you forgot to write the ng-app="" in your code ...
something like this !
<div ng-app="">
<select multiple="" class="form-control" name="category_list[]" ng-model="categories" >
<option ng-selected="{{category.category_id == product_categories}}"
ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
</div>
I have added
$watch for the multiple selection.
angular.module("myApp.controllers",[])
.controller("product",function($scope){
$scope.categories= [{"category_id":"1","category_name":"sports"},{"category_id":"2","category_name":"casual"},{"category_id"
:"3","category_name":"formal"},{"category_id":"4","category_name":"party wear"},{"category_id":"5","category_name"
:"winter"},{"category_id":"9","category_name":"summer"}]
$scope.product_categories=[{"category_id":"3"},{"category_id":"4"},{"category_id":"5"}]
$scope.$watch('product_categories', function(nowSelected){
$scope.selectedValues = [];
if( ! nowSelected ){
return;
}
angular.forEach(nowSelected, function(val){
$scope.selectedValues.push( val.category_id.toString() );
});
});
});
});
and I made some changes in my view part
<select class="form-control" name="category_list[]" multiple ng-model="selectedValues" >
<option ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
</select>
I put all the categories in selectedValues and assigned to select tag as ng model with multiple selection. After this change It works for me.
Related
I am trying to update a database field based on the option that is selected in the View.
<select name="unit_type[]" id="unit_type" class="form-control" style="width:150px;">
<option value="none" selected="" disabled="">Choose</option>
<option value="Unit">Unit</option>
<option value="Sub-Unit">Sub-Unit</option>
</select>
So in this case if Unit is selected a number should be added to a particular field and if Sub-Unit is selected the number should be added to another field.
Model
public function restocks_update(){
$item_id=$this->input->post('item_id');
$qty=$this->input->post('qty');
$unit_type=$this->input->post('unit_type');
for($i=0; $i<count($qty); $i++){
if($unit_type=="Unit"){
$this->db->query("UPDATE `stock` SET `quantity_available`=`quantity_available`+'$qty[$i]' WHERE `id`='$item_id[$i]'");
}else if($unit_type=="Sub-Unit"){
$this->db->query("UPDATE `stock` SET `convert_qty_available`=`convert_qty_available`+'$qty[$i]' WHERE `id`='$item_id[$i]'");
}
}
}
The if statement is in a loop because the select is also generated dynamically via jquery. The issue at the moment is that the fields are not updating, if i remove the else if it updates only one particular field even if it doesn't satisfy the condition.
try unit_type instead of unit_type[]
My page has two columns. Column1 for player1, column2 for player2.
For each column I have a dropdown to choose one of the players.
With some ajax en php codes I return the folowing info of each player. For example:
Col1: Tom, Football: 8 points Basketball: 5 points
Col2: Jonathan, Football: 4 points Basketball: 9 points
Now I want to let the users sort the results: So I add another dropdown in each column:
PHP
echo "<select id='filter_class' name=\"filter\"
data-userid=\"".$user_id."\" onchange=\"getFilter(this.value)\">
<option value=\"all\">All</option>
<option value=\"asc\">Asc</option>
<option value=\"desc\">Desc</option>
</select>";
AJAX
function getFilter(filter)
{
var user_id = document.getElementById('filter_class').getAttribute('data-userid');
console.log(user_id);
$.ajax({
type: "GET",
url: 'http://website.com',
data: {action: filter, user_id: user_id},
success: function (result) {
$("#Target").html(result);
}
});
}
As you can see, the dropdown use the user_id and the option value to sort the results.
this code works only for 1 column, because he keeps (Ofcourse) using the first user_id. For example when I try to sort the second results, he sort the results of the first user_id (Normal).
I want that the SORT DROPDOWN use the user_id of the player is selected in the same column.
IMPORTANT
I want to use the sort option after showing both results on the page.
You need to create your SORT dropdowns with different ids. Let's say you have the id's id='filter_class1' and id='filter_class2'.
Now you need to pass the id of the calling dropdown to the getFilter() function like this.
onchange=\"getFilter(this.value, this.id)\"
First few lines of your getFilter() function should look like this:
function getFilter(filter, eleId)
{
var user_id = document.getElementById(eleId).getAttribute('data-userid');
console.log(user_id);
...
There can only be one element with one id.
You might wanna use the class attribute alongside the data attribute.
Using jquery's class selector.
Take a look at this example.
<select class='filter_class' name="filter"
data-userid="29" >
<option value="all">All</option>
<option value="asc">Asc</option>
<option value="desc">Desc</option>
</select>
<select class='filter_class' name="filter"
data-userid="30" >
<option value="all">All</option>
<option value="asc">Asc</option>
<option value="desc">Desc</option>
</select>
<script>
$(".filter_class").change(function(){
var user_id = $(this).data('userid');
alert(user_id);
})
</script>
I am building a simple angular blog app in WordPress using the REST api, and so far have a select dropdown to filter posts by category, but I need to setup a select displaying archives by month.
I am unsure how to create the ng-repeat to loop over the months in which there are posts, and then how to filter the posts by the month selected.
This is how I am doing it for categories, first - within the controller:
$http.get('/wp-json/taxonomies/category/terms').success(function(data){
$scope.categories = data;
});
Then in the view:
<label for="categories">Categories:</label>
<select name="categories" ng-model="filterCat">
<option value="">Select month</option>
<option ng-repeat="category in categories" ng-bind-html="category.name" value="{{category.name}}"></option>
</select>
What I need to do is query the JSON (date) and retrieve only the month (and perhaps the year) for the select dropdown, and then filter those posts.
Thanks so much in advance.
I think it works now! This is based from a Plunkr somebody else posted on filtering by date with angular:
$scope.selectedMonthFilter = function(element) {
var d = new Date(element.date)
if (!$scope.selectedMonth) return true;
return d.getMonth() == $scope.selectedMonth;
}
The rest is identical to the linked Plunkr.
I want to create a product ordering page that has tons of options. Each option changes the price around a little bit, and I want the total price auto-update whenever any option changes.
All options are Drop-down menus so the prices would need to change when they select an option from a drop down.
I'm open to everything, but simple code on one page would be best. I want to be able to edit the prices as a part of the rest of the product page as there is only one product.
Any advice would be appreciated.
Try binding a jQuery/javascript function that calculates the sum of all price input fields and prints it at the bottom to the dropdowns' onchange events like so. The HTML I'm giving you is just a mockup, feel free to change it and the jQuery references to it as you wish.
HTML:
<p>Computer base price: $<span id="baseCost"></span></p>
<p>Select your upgrades:</p>
<form id="options">
<select name="processor" onchange="calculateTotals()">
<option value="Base_0">Base processor ($0)</option>
<option value="Processor1_100">Processor 1 ($100)</option>
<option value="Processor2_500">Processor 2 ($500)</option>
</select>
<select name="hard-drive" onchange="calculateTotals()">
<option value="Base_0">Base hard-drive ($0)</option>
<option value="7200rpm_250">7200rpm hard-drive ($250)</option>
</select>
</form>
<p>Upgrade total: $<span id="upgradeCost">0</span></p><br>
<p>Final total: $<span id="sumTotal"></span></p>
Javascript:
$(document).ready(function(){
var basePrice = 2000;
$("#baseCost").text(basePrice);
$("#sumTotal").text(basePrice);
});
function calculateTotals(){
var basePrice = parseInt($("#baseCost").text(), 10);
var upgradePrice = 0;
$("#options select").each(function(){
var optionVal = $(this).val();
upgradePrice += parseInt(optionVal.substr(optionVal.indexOf("_") + 1, optionVal.length - 1), 10);
});
$("#upgradeCost").text(upgradePrice);
$("#sumTotal").text(basePrice + upgradePrice);
}
WORKING DEMO
As you can see, the option value attributes include both the option ID and its price, separated by an underscore. This allows the Javascript and your form handling code to get both the selected options and their prices.
Need to have a dynamic set of values in an select attibute, depending upon another select attribute.
e.g. there will be two dropdown attributes 1. parent dropdown, 2. child dropdown
if "A" is selected in parent dropdown then "Air","Apple","Ant" will be shown in dropdown.
if "B" is selected in parent attribute then "Ball", "Box", "Base" will be shown.
So basically values of child dropdown will be depended upon the selected value of parent dropdown.
I want to make it dynamic as options can be saved under attributes and those values are to shown in Catalog Product Edit page.
Thanks in advance.
try the below code if you have data inside select box in object or array in JS then you can filter it out easily and append it to select box
Here's Demo DEMO
var data = {
"A": ["Air", "Apple", "Ant"],
"B": ["Water", "Mango", "Fly"]
}
jQuery('#parent').on('change', function() {
var tempData = data[this.value];
var selectChild = jQuery('#child');
jQuery('option', selectChild).remove();
for (var i = 0; i < tempData.length; i++) {
var option = new Option(tempData[i], tempData[i]);
selectChild.append(jQuery(option));
}
});
<select id="parent">
<option value="">Select Parent</option>
<option value="A">A</option>
<option value="B">B</option>
</select>
<select id="child">
<option value="">Select Child</option>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>