How correctly in vuejs using if v-select check if the element is selected
having in options array with code/label array?
I tried as :
<v-select
v-model="selection_filter_priority"
label="label"
:options="taskPriorityLabels"
id="filter_priority"
name="filter_priority"
class="form-control editable_field"
placeholder="Select all"
></v-select>
console.log('-11 typeof this.selection_filter_priority::')
console.log(typeof this.selection_filter_priority)
console.log(this.selection_filter_priority)
if (typeof this.selection_filter_priority == 'object' && typeof this.selection_filter_priority != null) {
filter_priority = this.selection_filter_priority.code // But if option is not selected(null) I got error here:
}
Which is the valid way?
"vue": "^2.6.10",
"vue-select": "^3.2.0",
Your code didn't properly test if this.selection_filter_priority is null. To check if an object is null, use if (this.selection_filter_priority === null) instead.
See the demonstration below:
var nullValue = null;
var objectValue = {};
var numberValue = 1;
console.log('null');
check(nullValue);
console.log('\n');
console.log('object');
check(objectValue);
console.log('\n');
console.log('number');
check(numberValue);
console.log('\n');
function check(x) {
if (x === null) {
console.log('is null');
}
if (typeof x == 'object' && typeof x != null) {
// is same as if (typeof x == 'object')
console.log('null or object');
}
if (typeof x != null) {
console.log('always true');
}
}
Related
I use this API :
https://geo.api.gouv.fr/decoupage-administratif/communes
It works really good but the problem is that if I'm searching a city (commune) like : "Saint-Étienne" I have to write exactly "Saint-Étienne". I would like to find this city for example without space : "saint étienne" or accent : "saint etienne".
In the documentation it's good.
But in my project in Vue.Js with Axios I can't find solutions. The v-select needs the exact writing.
Here is the input :
<v-autocomplete
v-else
#input="inputCity('departureCity', 'departureDepartment', selectedArrayCityDeparture)"
:loading="loading"
:items="citiesDeparture"
:search-input.sync="searchCitiesDeparture"
v-model="selectedArrayCityDeparture"
:rules="[
(value) => searchCitiesDeparture !== null || $t('form.validation.theDepartureCity') + ' ' + $t('isRequired')
]"
text
hide-no-data’
required
:label="$t('form.departureCity')"
:no-data-text="$t('noDataAvailable')"
/>
methods :
inputCity(cityType, departmentType, index) {
if (index !== null && this.responseCities[index] !== undefined) {
this.form[cityType] = this.responseCities[index].nom;
this.form[departmentType] = this.responseCities[index].codeDepartement;
}
},
querySelections(q, cities) {
this.$axios.get(this.apiRoutes.gouv.cities(q)).then(
response => {
this.loading = false;
this[cities] = _.map(response.data, function (item, i) {
return ({ text: item.nom + ' (' + item.codeDepartement + ')' , value: i });
});
this.responseCities = response.data;
}
)
},
watch :
search (val) {
val && val !== this.select && this.querySelections(val)
},
searchCitiesArrival(val) {
if (val !== null && val !== undefined && val.length > 1 && !(this.selectedArrayCityArrival !== null && this.citiesArrival[this.selectedArrayCityArrival] !== undefined && this.citiesArrival[this.selectedArrayCityArrival].text === val)) {
this.querySelections(val, "citiesArrival");
}
},
searchCitiesDeparture(val) {
if (val !== null && val !== undefined && val.length > 1 && !(this.selectedArrayCityDeparture !== null && this.citiesDeparture[this.selectedArrayCityDeparture] !== undefined && this.citiesDeparture[this.selectedArrayCityDeparture].text === val)) {
this.querySelections(val, "citiesDeparture");
}
}
data :
citiesDeparture: [],
citiesArrival: [],
responseCities: [],
call Api :
gouv: {
cities: function (name) {
return ('https://geo.api.gouv.fr/communes?nom=' + name + '&fields=&format=json&geometry=centre"');
}
},
How can I find the city with spaces or accents like in the documentation ?
I don't understand why I can't do
openAndFillModalSoin(soin)
{
this.show = true,
this.vDate = soin.date,
this.vCategorie = soin.categoriesoin.name,
//This can be null
if(soin.rabaisraion){
this.vReasonReduction = soin.rabaisraison.id;
}
this.vPaiement = soin.moyendepaiement.nam,
this.vRefer = soin.referedBy,
//This can be null aswell
this.vGiftCard = soin.boncadeau.id,
this.vVoucher = soin.bonreduction.id;
this.vID = soin.id;
},
The "if" parts doesn't work, it asks for an expression.
You have commas instead of semicolons ending the preceding line.
if(soin.rabaisraion){
this.vReasonReduction = soin.rabaisraison.id;
}
This this code will run when the following is NOT TRUE
soin.rabaisraion is the number 0, false, null, undefined, or an empty string.
To reiterate, the string 'false', the string '0' and and an array (empty or not) are all true.
Also, if soin is null or undefined, that will be a runtime error.
Perhaps you want this:
if(soin && soin.rabaisraion){
this.vReasonReduction = soin.rabaisraison.id;
}
Regardless, add a log before to see what's going on:
console.log('checking soin', soin)
console.log('checking boolean soin', !!soin)
if(soin && soin.rabaisraion){
this.vReasonReduction = soin.rabaisraison.id;
}
The '!!' will force the value to boolean.
//check undefined Array
if (typeof myArray === "undefined") {
alert("myArray is undefined");
}
// check undefined object
if (typeof myObj === "undefined") {
alert("myObj is undefined");
}
//check object property
if (typeof myObj.some_property === "undefined") {
alert("some_property is undefined");
}
I want to inherit function add_product of models.Order.
Here is the original function
/point_of_sale/static/src/js/models.js
add_product: function(product, options){
if(this._printed){
this.destroy();
return this.pos.get_order().add_product(product, options);
}
this.assert_editable();
options = options || {};
var attr = JSON.parse(JSON.stringify(product));
attr.pos = this.pos;
attr.order = this;
var line = new exports.Orderline({}, {pos: this.pos, order: this, product: product});
if(options.quantity !== undefined){
line.set_quantity(options.quantity);
}
if(options.price !== undefined){
line.set_unit_price(options.price);
}
//To substract from the unit price the included taxes mapped by the fiscal position
this.fix_tax_included_price(line);
if(options.discount !== undefined){
line.set_discount(options.discount);
}
if(options.extras !== undefined){
for (var prop in options.extras) {
line[prop] = options.extras[prop];
}
}
var to_merge_orderline;
for (var i = 0; i < this.orderlines.length; i++) {
if(this.orderlines.at(i).can_be_merged_with(line) && options.merge !== false){
to_merge_orderline = this.orderlines.at(i);
}
}
if (to_merge_orderline){
to_merge_orderline.merge(line);
} else {
this.orderlines.add(line);
}
this.select_orderline(this.get_last_orderline());
if(line.has_product_lot){
this.display_lot_popup();
}
},
I need to add one more condidtion like,
if(options.is_promo !== undefined){
line.set_promo(options.is_promo);
}
},
So in my custom module, I tried this,
var _super_order = models.Order.prototype;
models.Order = models.Order.extend({
add_product: function(product, options){
if(options.is_promo !== undefined){
line.set_promo(options.is_promo);
}
_super_order.add_product.apply(this,arguments);
},
But it throws an error, line is not defined.
How can i do it?
I'm trying to set an error message to appear when doing some file upload in Aurelia.
The HTML is:
<div if.bind="fileErrorMessage !== null" class="text-center">
<span class="validation-error">${fileErrorMessage}</span>
</div>
The code for the error message is:
folderError(type) {
if (type === "folders") {
console.log("folders");
this.fileErrorMessage = "ABC";
console.log(this.fileErrorMessage);
} else if (type === "file") {
console.log("files");
this.fileErrorMessage =
"DEF";
console.log(this.fileErrorMessage);
} else if (type === "files_already") {
console.log("files already");
this.fileErrorMessage =
"GHI";
console.log(this.fileErrorMessage);
} else if (type === "folders_already") {
console.log("folders already");
this.fileErrorMessage =
"JKL";
console.log(this.fileErrorMessage);
}
}
For some reason, all console logs fire correctly but if I access the folderError function from outside the module that contains it, the appropriate console.logs fire (including the one that correctly states "this.fileErrorMessage") but the error message does not appear on the rendering.
I was able to fix it by converting the function into an arrow function.
folderError = (type) => {
if (type === "folders") {
console.log("folders");
this.fileErrorMessage = "ABC";
console.log(this.fileErrorMessage);
} else if (type === "file") {
console.log("files");
this.fileErrorMessage =
"DEF";
console.log(this.fileErrorMessage);
} else if (type === "files_already") {
console.log("files already");
this.fileErrorMessage =
"GHI";
console.log(this.fileErrorMessage);
} else if (type === "folders_already") {
console.log("folders already");
this.fileErrorMessage =
"JKL";
console.log(this.fileErrorMessage);
}
}
I'm currently using a custom sort function on a dgrid (pasted below). It doesn't change sorting drastically, just sorts one particular column uniquely and sorts the others case-insensitive. I'd like to add a secondary sort by a column named "scheduled" to be added to the sort when any other column is sorted. I'm just not sure how to go about it. I've seen how to override the sort to sort by two columns, but not when a custom sort is in play. The secondary sort would always be there, not matter what other column is clicked.
For reference I'm running dojo 1.10 and dgrid 1.0. Data is coming from a RequestMemory DStore and I'd really rather this sort happen on the grid rather than back at the store level. Any help would be appreciated.
currGrid.on('dgrid-sort', function (event) {
event.preventDefault();
var sort = event.sort[0];
currGrid.set('sort', function (a, b) {
if (sort.property == "thisField") {
//special sort for thisField
if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
var colorA = a[sort.property].split("|");
var aValue = colorA[0].toLowerCase();
}
if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
var colorB = b[sort.property].split("|");
var bValue = colorB[0].toLowerCase();
}
if (String(aValue) == String(bValue)) {
var result = 0;
} else if (dojo.string.trim(aValue) == "") {
var result = true ? 1 : -1;
} else if (dojo.string.trim(bValue) == "") {
var result = true ? -1 : 1;
} else {
var result = aValue > bValue ? 1 : -1;
}
return result * (sort.descending ? -1 : 1);
} else {
//Sort for all other fields same as always (except toLowerCase)
if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
var aValue = a[sort.property].toLowerCase();
} else {
var aValue = "";
}
if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
var bValue = b[sort.property].toLowerCase();
} else {
var bValue = "";
}
var result = aValue > bValue ? 1 : -1;
return result * (sort.descending ? -1 : 1);
}
});
currGrid.updateSortArrow(event.sort, true);
});
currGrid.startup();
You could do something like below.
currGrid.on('dgrid-sort', function (event) {
event.preventDefault();
var sortSet = [];
sortSet.push(event.sort[0]);
sortSet.push({property: "scheduled"});
currGrid.set('sort', function (a, b) {
var aValue, bValue, result = 0;
for(var i = 0; i < sortSet.length; i++){
var sort = sortSet[i];
if (sort.property == "thisField") {
//special sort for thisField
if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
var colorA = a[sort.property].split("|");
aValue = colorA[0].toLowerCase();
}
if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
var colorB = b[sort.property].split("|");
bValue = colorB[0].toLowerCase();
}
if (String(aValue) == String(bValue)) {
result = 0;
} else if (dojo.string.trim(aValue) == "") {
result = true ? 1 : -1;
} else if (dojo.string.trim(bValue) == "") {
result = true ? -1 : 1;
} else {
result = aValue > bValue ? 1 : -1;
}
return result * (sort.descending ? -1 : 1);
} else {
//Sort for all other fields same as always (except toLowerCase)
if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
aValue = a[sort.property].toLowerCase();
} else {
aValue = "";
}
if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
bValue = b[sort.property].toLowerCase();
} else {
bValue = "";
}
//You need this check here
if(aValue != bValue){
result = aValue > bValue ? 1 : -1;
return result * (sort.descending ? -1 : 1);
}
}
}
return 0;
});
currGrid.updateSortArrow(event.sort, true);
});
currGrid.startup();
I have some concerns about your code, the variables result, aValue and bValue are all local within the if statement and yet they are being used outside the statement. It could result in wrong results if some other variables are defined with the same name in global space. So I have modified them.
So the second section you needed to check if aValue == bValue to return 0.