Select all option in materialize multi select - multi-select

Bootstrap multisleect has option for select all (for example here). Is this possible in materialize multi select?

Here is a smaller version: https://codepen.io/souvik1809/pen/rvNMyO
HTML
<div class = "row">
<label>Materialize Multi Select</label>
<select multiple class="select_all">
<option value="" disabled selected>Choose your option</option>
<option value = "1">Mango</option>
<option value = "24">Orange</option>
<option value = "3">Apple</option>
<option value = "4">Cucumber</option>
<option value = "5">Litchi</option>
</select>
</div>
JavaScript
$(document).ready(function() {
// $('select').val([1]);
$('select').formSelect();
$('select.select_all').siblings('ul').prepend('<li id=sm_select_all><span>Select All</span></li>');
$('li#sm_select_all').on('click', function () {
var jq_elem = $(this),
jq_elem_span = jq_elem.find('span'),
select_all = jq_elem_span.text() == 'Select All',
set_text = select_all ? 'Select None' : 'Select All';
jq_elem_span.text(set_text);
jq_elem.siblings('li').filter(function() {
return $(this).find('input').prop('checked') != select_all;
}).click();
});
});

You can add them like this:
https://codepen.io/anon/pen/XgEbRL?editors=1010
HTML:
<div class="input-field col s12">
<select multiple>
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label>Materialize Multiple Select</label>
</div>
<a class="btn" onClick="javascript:selectAll()">Select all</a>
<a class="btn" onClick="javascript:selectNone()">Select none</a>
JavaScript:
var activateOption = function(collection, newOption) {
if (newOption) {
collection.find('li.selected').removeClass('selected');
var option = $(newOption);
option.addClass('selected');
}
};
$(document).ready(function() {
$('select').material_select();
$('.test-input > .select-wrapper > .select-dropdown').prepend('<li class="toggle selectall"><span><label></label>Select all</span></li>');
$('.test-input > .select-wrapper > .select-dropdown').prepend('<li style="display:none" class="toggle selectnone"><span><label></label>Select none</span></li>');
$('.test-input > .select-wrapper > .select-dropdown .selectall').on('click', function() {
selectAll();
$('.test-input > .select-wrapper > .select-dropdown .toggle').toggle();
});
$('.test-input > .select-wrapper > .select-dropdown .selectnone').on('click', function() {
selectNone();
$('.test-input > .select-wrapper > .select-dropdown .toggle').toggle();
});
});
function selectNone() {
$('select option:selected').not(':disabled').prop('selected', false);
$('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:checked').not(':disabled').prop('checked', '');
//$('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:checked').not(':disabled').trigger('click');
var values = $('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:disabled').parent().text();
$('input.select-dropdown').val(values);
console.log($('select').val());
};
function selectAll() {
$('select option:not(:disabled)').not(':selected').prop('selected', true);
$('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:not(:checked)').not(':disabled').prop('checked', 'checked');
//$('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:not(:checked)').not(':disabled').trigger('click');
var values = $('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:checked').not(':disabled').parent().map(function() {
return $(this).text();
}).get();
$('input.select-dropdown').val(values.join(', '));
console.log($('select').val());
};

Related

filter array doesn't work in React component

I wanna sort the sizes of my clothes but when I click to elect the size, no matter what I choose, it stays "All"...Is there anything wrong with my function filterProducts?
function Filter(props) {
return (
<div className="filter">
<div className="filter-result">{props.count} Products</div>
<div className="filter-sort">
Order
<select value={props.sort} onChange={props.sortProducts}>
<option value="latest">Latest</option>
<option value="lowest">Lowest</option>
<option value="highest">Highest</option>
</select>
</div>
<div className="filter-size">
Filter
<select value={props.size} onChange={props.filterProducts}>
<option value="">All</option>
<option value="">XS</option>
<option value="">S</option>
<option value="">M</option>
<option value="">L</option>
<option value="">XL</option>
<option value="">XXL</option>
</select>
</div>
</div>
);
}
My codesand link:https://codesandbox.io/s/redux-shop-cart-forked-tlpek?file=/src/App.js
All the size options were missing unique value props, they were all value="".
Update the value props to match your data:
Filter
<select value={props.size} onChange={props.filterProducts}>
<option value="">All</option>
<option value="XS">XS</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
<option value="XXL">XXL</option>
</select>
You also had a typo in filterProducts in App where you reset the state to a product property instead of products. This wasn't allowing the "All" filter value to work.
filterProducts = (event) => {
if (event.target.value === "") { // all
this.setState({
size: event.target.value,
products: data.products // <-- reset products!!
});
} else {
this.setState({
size: event.target.value,
products: data.products.filter(
(product) => product.availableSizes.indexOf(event.target.value) >= 0
)
});
}
};

Angular js order by date dd-mm-yyyy

I want to order date. But doesn't work correctly. Date format is dd-mm-yyyy. Just order dd format doesn't check mm and yyyy. How can I solve?
JS
$scope.sortOptions = [
{
name:'Date desc',
sortCategory:'-anndate'
},
{
name:'Date asc',
sortCategory:'anndate'
}
];
HTML
<select ng-model="selectedSortType" class="custom-select">
<option value="" selected="">Select type</option>
<option value='{{opt.sortCategory}}' ng-repeat="opt in sortOptions track by $index">{{opt.name}}</option>
</select>
<div ng-repeat="i in res| orderBy:selectedSortType">
<div>{{i.anndate}}</div>
</div>
var app = angular.module('app', [])
.controller('appController', appController);
appController.$inject = ['$scope', '$window'];
function appController($scope, $window) {
$scope.title = "date sorting example";
$scope.sortOptions = [{
name: 'Date desc',
sortCategory: '2018-07-15 '
},
{
name: 'Date desc',
sortCategory: '2018-07-12 '
},
{
name: 'Date asc',
sortCategory: '2018-07-13'
}
];
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="appController">
<p>Ascending Order</p>
<div ng-repeat="i in sortOptions | orderBy:'sortCategory'">
<p>{{i.sortCategory}}</p>
</div>
<select ng-model="selectedSortType" class="custom-select">
<option value="" selected="">Select type</option>
<option value='{{opt.sortCategory}}' ng-repeat="opt in sortOptions | orderBy:'sortCategory'">{{opt.sortCategory}}</option>
</select>
<p>Descending Order</p>
<div ng-repeat="i in sortOptions | orderBy:'-sortCategory'">
<p>{{i.sortCategory}}</p>
</div>
<select ng-model="selectedSortType" class="custom-select">
<option value="" selected="">Select type</option>
<option value='{{opt.sortCategory}}' ng-repeat="opt in sortOptions | orderBy:'-sortCategory'">{{opt.sortCategory}}</option>
</select>
</div>
Try this..
Since the date in res object is in string format the filter is applying on dd value.
For entire date filter you need to convert the anndate to Date() object
Place the following code in the line below assigning res value in controller
angular.forEach($scope.res,function(item){ item.anndate = new Date(item.anndate); });
Then in html replace with below line
<select ng-model="selectedSortType" class="custom-select">
<option value="" selected="">Select type</option>
<option value='{{opt.sortCategory}}' ng-repeat="opt in sortOptions track by $index">{{opt.name}}</option>
</select>
<div ng-repeat="i in res| orderBy:selectedSortType">
<div>{{i.anndate| date : 'dd/MM/yyyy'}}</div>
</div>

ng-change does not work when selected items are changed

I'm new to angular, I was trying to display cascading dropdown lists. Here is my html code
<div class="row" ng-controller="createCabsTicketController as tktCreatecabsCntrl">
<select id="LocalCompanyIDForSearch" ng-change="getIXs()" ng-model="createCabsTicket.cabsCompanies.selected" name="LocalCompanyIDForSearch" class="form-control" style="width:100%;">
<option value=" "> </option>
<option ng-repeat="localCompany in createCabsTicket.cabsCompanies"
value="{{ localCompany.CompanyID }}">
{{ localCompany.LongDescription }}
</option>
</select>
<select id="IXCname" name="IXCname" class="form-control" style="width:100%;">
<option value=" "> </option>
<option ng-repeat="ix in createCabsTicket.IXCarrierNames"
value="{{ ix.Interexchange }}">
{{ ix.Interexchange }}
</option>
</select>
And here is my js file contents
app.controller('createCabsTicketController',['$scope','$http',function($scope,$http){
$scope.createCabsTicket = {
cabsCompanies: [],
IXCarrierNames: [],
IXCarrierIds:[]
};
$http.get('http://localhost:52377/api/reference/companies/active', { cache: true }).success(function (data) {
$scope.createCabsTicket.cabsCompanies = data;
});
$scope.getIXs = function (selectedItem) {
var postData = { ActiveOnly: true, CompanyId: selectedItem, CarrierId: "", CarrierName: "", CarrierType: "" };
$http.post('http://localhost:52377/api/reference/interexchanges', postData, { cache: true }).success(function (data) {
$scope.createCabsTicket.IXCarrierNames = data;
});
}
}]);
The web services work and the parent list gets populated just fine. But the ng-change event doesn't get fired. I am not sure what did I do wrong. I have found similar posts but they were not quite helpful to solve my problem. Please help!!

Getting selected item of ng repeat on a datalist

I'm using a array iteration to know which is the selected option. I'm insterested on id atribute of result. is there other way?
<input type="text" list="products" ng-model="query" />
<datalist id="products">
<option value="{{result.name}}" ng-repeat="result in results" >
</datalist>
...
$scope.search = function (query) {
$scope.results.forEach(function (result) {
if (result.name === query) {
// code here
}
}
}
...
Try this:
<datalist id="products">
<option value="{{result.name}}" ng-selected="query.id == result.id" ng-repeat="result in results" >
</datalist>

Add space to <select> with ng-options

How do I add leading spaces into the <select> options using Angular ng-options?
<div ng-controller="MyCntrl">
Static values (works)
<select>
<option value="black">black</option>
<option value="white"> white</option>
<option value="red"> red</option>
<option value="blue"> blue</option>
<option value="yellow"> yellow</option>
</select>
Values from JS using angular (only the first-default works)
<select ng-model="color" ng-options="c.name for c in colors">
<option value=""> default</option>
</select>
</div>
JS:
angular.module("myApp", []).
controller("MyCntrl", ['$scope', function ($scope) {
$scope.colors = [{
name: 'black',
shade: 'dark'
}, {
name: ' white',
shade: 'light'
}, {
name: ' red',
shade: 'dark'
}, {
name: ' blue',
shade: 'dark'
}, {
name: ' yellow', // spaces here
shade: 'light'
}];
}]);
you can format the option text:
<select ng-model="color" ng-options="(' '+c.name) for c in colors">
<option value=""> default</option>
</select>
EDIT
If you want to generate the dynamically it is a little bit more complicated. Suppose you have a function in your controller that knows how many you want to add, then you can write this function like this:
$scope.addSpaces= function(color){
var count = 1;// put your logic here
var result = "";
for(var i=0; i<count; i++){
result+= String.fromCharCode(160);
}
return result;
};
in your html it will be:
<select ng-model="color" ng-options="(addSpaces(c)+c.name) for c in colors">
<option value=""> default</option>
</select>
This works because we concatenate the unicode character for (e.g.  ) in our string and not the entity, so there is nothing to escape for the element.text() function.

Resources