validating forms $refs returning undefined vuejs - forms

I am trying to validate my form but when I try to call the validation method it's returning the value undefined instead of Boolean. Can anyone help me fix the problem ? This below code is the one I am using to validate. By the way I am trying validate it in a child component. Which is only gonna render and show a dialogue when clicking a button.
computed:{
employeeFormValidation(){
this.$refs.employeeForm.validate((valid)=>{
return valid ? true : false
},
employeeAddressValidation(){
this.$refs.employeeAddress.validate((valid)=>{
return valid ? true : false
})
},
employeeDetailsValidation(){
this.$refs.employeeDetails.validate((valid)=>{
return valid ? true : false
})
},
bankDetailsValidation(){
this.$refs.bankDetails.validate((valid)=>{
return valid ? true : false
})
},

Vue is not design as everything is reactive, the docs says "$refs is also non-reactive"
One way to do is use methods instead of computed, But the downside is that this will be re-called even if the dependency has not changed.
However, according to Vue's design philosophy, it should use less refs, accept the values emited by the component, and store them in data.
<your-child-components
#validate="(ok) => (valiateEmployeeDetails = ok)"
>
<your-child-components
#validate="(ok) => (valiateEmployeeAddress = ok)"
>
{
data(){
return {
valiateEmployeeDetails: false,
valiateEmployeeAddress: false,
}
}
}
React has a doucment about "when to use refs", it's the same in Vue.

Related

Ag-grid valueFormatter and Column Filter

I am having problems using ag-grid valueFormatter and column filters (https://www.ag-grid.com/javascript-data-grid/filtering/).
I have a simple colDef:
{
headerName: 'My column',
field: 'myData',
hide: true,
valueFormatter: this.formatterBooleanToHuman,
},
the formatterBooleanToHuman is a simple code to change true to Yes and false to No.
It works as expected, the issue is that we are using column filters, and when I click on the filter I have true and false to select, if I select any of them, nothing returns from the filters because the value now is actually Yes and No.
I couldn't manage to have both of them working together. To have the column filter working properly I need to remove the valueFormatter, but I would like to have both working.
I tried to apply the valueFormatter function to filterParams.valueFormatter, it did change the values on the filter but something is failing, I am getting 2 No and 1 Yes, and none of them filter.
Any suggestions?
UPDATE:
So, I found a solution, but I am not convinced it is the right way to do it.
get getcolumnDef(): Array<ColDef> {
return [
{
headerName: 'Boolean Column',
field: 'booleanValue',
hide: true,
valueFormatter: this.formatterBooleanToHuman,
filterParams: {
valueGetter: (params) => this.filterBooleanValueGetter(params, 'booleanValue')
}
}
];
}
private filterBooleanValueGetter(params: ValueGetterParams, propertyName: string) {
let isDeleted = false;
const hasValue = !!params && !!params.data && params.data[propertyName];
if (hasValue) {
isDeleted = String(params.data[propertyName]) === 'true';
}
return isDeleted ? 'Yes' : 'No';
}
So, the valueGetter works as expected and makes my filter work, I just think it is a bit "dirty" to have it to work like that, I haven't found anything on the docs saying this is the way it needs to be done. So suggestions are more than welcome.
valueFormatter applies only to data in grid. However even if the filter shows true and false instead of formatted values, it should work correctly. If filtering does not work, it may indicate some other error in your code. Maybe you depend on this in formatterBooleanToHuman method?
Anyway to format values in filter, you should define filterParams.valueFormatter like this:
{
// ...
valueFormatter: this.formatterBooleanToHuman,
filterParams: {
valueFormatter: this.formatterBooleanToHuman
}
}
For some reason, the value given to filter formatter is string instead of boolean (bug in ag-grid?), you need to adjust that.
See complete example here: https://plnkr.co/edit/o3sN3GodqQumVe09

Pass Dynamic Variable to highlightResults in Autocomplete.js

I'm building a typeahead component for my Vue application that searches Algolia, which has several different indexes to search in different places, so I've created props to be passed in to set the input placeholder, search index, and displayKey.
All works well except my highlighting function for suggestions.
I'm sure this is something simple but I can't get the highlight return to pick up the dynamic prop passed in.
$('.typeahead').autocomplete({ hint: false }, [{
source: $.fn.autocomplete.sources.hits(this.client, { hitsPerPage: 5 }),
displayKey: this.display,
templates: {
suggestion: (suggestion) => {
return suggestion._highlightResult.{this.display goes here}.value;
}
}
}]).on('autocomplete:selected', (event, suggestion, dataset) => {
console.log(suggestion, dataset);
})
If I omit the highlighting all works perfectly.
I knew it was simple, call it via array key instead of dot notation.
return suggestion._highlightResult[this.display].value;

ag - grid + angular 4 custom text filter

I have a column called Account Verification which return values either true or false. I formatted the value on table to Yes and No, but when filtering the column, I still have to search true or false to be able to filter the column. I made a custom filter condition but it did not work. Anyone has solution?
columns = [
{
headerName: 'Account Verification', field: 'accountVerified', filter: 'agTextColumnFilter',
// Cell renderer
valueFormatter: (data) => {
if (data.value === true) return 'Yes';
else return 'No';
},
// Custom filter
filterParams: {
condition: (searchTerm, cellValue) => {
if (searchTerm === 'Yes' || 'yes') {
return cellValue === true;
} else if (searchTerm === 'No' || 'no') {
return cellValue === true;
} else return cellValue === null;
}
}
}
]
"ag-grid": "^18.0.1"
I think the best way to implement this - is to use built in methods of ag-grid, like:
onFilterModified
onFilterChanged
And there provide the logic, which would be transform the filter value to true/false.
Or, use javascript to add event listener on apply button of the filter (if you are using apply button to apply your filter), which would call fucntion for transform your value.
Have no example in the moment, but I develped solution for filter validation (custom) in the same way.
Try one of these options:
Similar to to valueFormatter in your column definition, use a valueGetter also. There is also this method available in column definition (filterValueGetter(params) - Function or expression. Gets the value for filtering purposes.)
You can add a new field in your data model that you initialize to Yes/No based on the values of the original field that has true/false. Then in the column definitions, you can use this new field to display in the grid.
Alternatively, you can write a custom filter component. See the custom Angular filter example on the ag-grid site.
You can use TextFormatter within the filterParams to modify the filter value during filtering:
filterParams: { textFormatter: (filterValue) => 'yourvalueformatter' }
https://www.ag-grid.com/javascript-grid-filter-text/#text-formatter
I had the same issue and went through all the inbuilt filters.
Finally I was able to fix the issue by following the below steps
Steps :
Install the ag-grid-enterprise package. (npm install ag-grid-enterprise)
Set the filter to agSetColumnFilter. (filter: 'agSetColumnFilter')
Add filterParams and specify the same valueFormatter which you used in the column definition
filterParams: {
   valueFormatter: (data) => {
       if (data.value === true) return 'Yes';
       else return 'No';
      }
}
You can try adding this to your column definition:
filterValueGetter: params => params.data.accountVerified ? "Yes" :
"No"

Suitescript 2.0 setting coupons and partner codes

We created a suitescript 2.0 script in our netsuite environment. We are using RESTlet to access it.
Our script creates a sales order with various fields. It works fine but we are unable to set a coupon code value or a partner code, we get the same error for both. We are using Internal ID and we tried coupon code itself as well.
Any idea?
Error:
{
"type":"error.SuiteScriptError",
"name":"INVALID_FLD_VALUE",
"message":"You have entered an Invalid Field Value 18 for the following field: couponcode",
"stack":[
"<anonymous>(N/record/recordService.js)",
"setSalesOrderData(adhoc$-1$debugger.user:71)",
"saveSaleOrder(adhoc$-1$debugger.user:17)",
"<anonymous>(adhoc$-1$debugger.user:107)",
"<anonymous>(adhoc$-1$debugger.user:6)"
],
"cause":{
"type":"internal error",
"code":"INVALID_FLD_VALUE",
"details":"You have entered an Invalid Field Value 18 for the following field: couponcode",
"userEvent":null,
"stackTrace":[
"<anonymous>(N/record/recordService.js)",
"setSalesOrderData(adhoc$-1$debugger.user:71)",
"saveSaleOrder(adhoc$-1$debugger.user:17)",
"<anonymous>(adhoc$-1$debugger.user:107)",
"<anonymous>(adhoc$-1$debugger.user:6)"
],
"notifyOff":false},"id":"","notifyOff":false
}
}
RESTlet code:
var objRecord = record.create({
type: record.Type.SALES_ORDER,
isDynamic: true
});
/* add other values.....*/
objRecord.setValue({ fieldId: 'couponcode', value: 538 });
var recordId = objRecord.save({
enableSourcing: false,
ignoreMandatoryFields: false
});
Are these coupon codes you are trying to set One-Time Use codes? Or are they linked to a Promotion?
Which internal ID are you using in the couponcode field?
Can you share the relevant parts of your RESTlet code as well?
I tested the following in the console (i.e. a Client Script) on a Sales Order, and it seems to set a Promotion and Coupon Code appropriately:
require(["N/currentRecord"], function(c) {
c.get().setValue({
"fieldId": "couponcode",
"value": 1
});
});
where 1 is the internal ID of the Promotion. If I use an internal ID not associated to a Promotion, I get no error, but nothing is populated in either field.
we finally got working code from Netsuite support, since there is such little help on this topic online, I am sharing it here. We grabbed what we needed into our own script, but this basic one works as well,
From netsuite support agent:
I created a simple SuiteScript 2.0 code for entering the values into Partner field (id: 'partner') and Coupon Code (id: 'couponcode'). Both fields are dropdown fields not multiselect fields.
The field Coupon Code depends on Promotion field that's why we should enter value in 'promocode' field instead of 'couponcode'.
/**
*#NApiVersion 2.x
*#NScriptType usereventscript
*/
define(['N/record'],
function(record) {
function AfterSubmit(context) {
var result = record.load({
type: 'salesorder',
id: 71040,
isDynamic: true
});
result.setValue ({
fieldId : 'partner',
value : 45140
});
result.setValue ({
fieldId : 'couponcode',
value : 'AMARILLO16'
});
result.save({
enableSourcing : false,
ignoreMandatoryFields : true
});
return true;
}
return {
afterSubmit: AfterSubmit
};
});
We had to do one modification for it to work for us:
result.setValue ({
fieldId : 'partner',
value : 45140
});
result.setText ({
fieldId : 'couponcode',
text : 'AMARILLO16'
});
result.save({
enableSourcing : false,
ignoreMandatoryFields : true
});

How to update jstree node values without reload

I have a jstree that I created with the following code:
$('#mytree').jstree({"core": { "data" : value
, "themes" : { "dots": false
, "icons": false }
}
}
);
I can rebuild it with new data by this code:
$('#mytree').jstree(true).settings.core.data = new_data;
$('#mytree').jstree(true).refresh();
but it can be expensive when you have a lot of nodes. What I would like to achieve is that I would like update the value of the elements (i.e. the node.text part) without rebuilding the whole tree. I get the new values via websocket in one message (the complete JSON string that will be the new_data) but the structure is not changing. How can I do that? Thank you!
What you need is not refresh() but redraw() thus the code is
$('#mytree').jstree(true).settings.core.data = new_data;
$('#mytree').jstree(true).redraw(true);
You can find the functions in the jstree API.
As per zmirc suggestion, in v3.1 use:
$('#mytree').jstree(true).settings.core.data = new_data;
$('#mytree').jstree(true).refresh();
for deleting the node and reload tree
$('#mytree').jstree(true).refresh();
for those who need to redraw without restart the tree use
jQuery('#data').jstree(true).refresh(true);
worked for me: $('#structureRows').jstree("destroy").empty();
function CreateStructureTree(jsonData)
{
$('#structureRows').jstree("destroy").empty();
$('#structureRows').jstree
({
'core' : {
'data':
[
jsonData,
{
'id' : 'node_2',
'text' : 'Root node with options',
'state' : { 'opened' : true, 'selected' : true },
'children' : [ { 'text' : 'Child 1' }, 'Child 2']
}
]
}
});
}
You can refresh node by this
$('#treeView').jstree(true).refresh_node("node_id_here")
$('#mytree').jstree(true).refresh();
is working, but in my case it causes thread leak.
every refresh adds one more thread
I load data via an url, so my refresh part looks like:
$('#groupTree').jstree(true).settings.core.data.url = get_group_url();
$('#YourJSTREE').jstree("destroy").empty();
Set new data
$('#YourJSTREE').jstree(true).refresh();