Binding values to panel title - extjs

I have a problem when I am binding values to a panel title.
My code basically looks like this :
Ext.define('serviceteamWorkflow.view.core.ServiceteamWorkflow', {
extend: 'Ext.Panel',
bind: {
title: 'First Name : {firstName} Last Name : {lastName}'
},
})
The problem is that nothing shows in the title when one of the bound values is null or undefined. Ie. If one of the bound values is invalid then the whole thing won't show.
I would like to just show nothing if the bound value is invalid. ie :
First Name : Last Name : Doe
Is there a way around this?

You could create a formula that would reference your bindings in your view model:
Ext.define('serviceteamWorkflow.view.core.ServiceteamWorkflow', {
extend: 'Ext.Panel',
bind: {
title: {showTitle}
},
})
Then inside your ServiceteamWorkflow view model:
requires: [
'Ext.app.bind.Formula'
],
data: {
firstName: '',
lastName: 'Johnson'
},
formulas: {
showTitle: function(get) {
var firstName = get('firstName'),
lastName = get('lastName');
return "First Name : " + firstName + " Last Name: " + lastName;
}
}

For those who are like me wondering how to set default values to viewmodel as suggested by Evan Trimboli in the first comment, it should be set in a view:
viewModel: {
data: {
firstName: '',
lastName: ''
}
}
If you set your data somewhere dynamically, for example:
this.getViewModel().set('fullName', data);
you need to set defaults like this:
viewModel: {
data: {
fullName: {
firstName: '',
lastName: ''
}
}
}

Related

update one element of array inside object and return immutable state - redux [duplicate]

In React's this.state I have a property called formErrors containing the following dynamic array of objects.
[
{fieldName: 'title', valid: false},
{fieldName: 'description', valid: true},
{fieldName: 'cityId', valid: false},
{fieldName: 'hostDescription', valid: false},
]
Let's say I would need to update state's object having the fieldName cityId to the valid value of true.
What's the easiest or most common way to solve this?
I'm OK to use any of the libraries immutability-helper, immutable-js etc or ES6. I've tried and googled this for over 4 hours, and still cannot wrap my head around it. Would be extremely grateful for some help.
You can use map to iterate the data and check for the fieldName, if fieldName is cityId then you need to change the value and return a new object otherwise just return the same object.
Write it like this:
var data = [
{fieldName: 'title', valid: false},
{fieldName: 'description', valid: true},
{fieldName: 'cityId', valid: false},
{fieldName: 'hostDescription', valid: false},
]
var newData = data.map(el => {
if(el.fieldName == 'cityId')
return Object.assign({}, el, {valid:true})
return el
});
this.setState({ data: newData });
Here is a sample example - ES6
The left is the code, and the right is the output
Here is the code below
const data = [
{ fieldName: 'title', valid: false },
{ fieldName: 'description', valid: true },
{ fieldName: 'cityId', valid: false }, // old data
{ fieldName: 'hostDescription', valid: false },
]
const newData = data.map(obj => {
if(obj.fieldName === 'cityId') // check if fieldName equals to cityId
return {
...obj,
valid: true,
description: 'You can also add more values here' // Example of data extra fields
}
return obj
});
const result = { data: newData };
console.log(result);
this.setState({ data: newData });
Hope this helps,
Happy Coding!
How about immutability-helper? Works very well. You're looking for the $merge command I think.
#FellowStranger: I have one (and only one) section of my redux state that is an array of objects. I use the index in the reducer to update the correct entry:
case EMIT_DATA_TYPE_SELECT_CHANGE:
return state.map( (sigmap, index) => {
if ( index !== action.payload.index ) {
return sigmap;
} else {
return update(sigmap, {$merge: {
data_type: action.payload.value
}})
}
})
Frankly, this is kind of greasy, and I intend to change that part of my state object, but it does work... It doesn't sound like you're using redux but the tactic should be similar.
Instead of storing your values in an array, I strongly suggest using an object instead so you can easily specify which element you want to update. In the example below the key is the fieldName but it can be any unique identifier:
var fields = {
title: {
valid: false
},
description: {
valid: true
}
}
then you can use immutability-helper's update function:
var newFields = update(fields, {title: {valid: {$set: true}}})

JS copy one object to another

I have the following object:
myModel =
{
Id: '',
Category :'',
Values: {
userName: '',
pasword: '',
address: ''
}
}
newModel may look like this:
{
Version : "12.1",
Values : {
"somenewProp" : "with a value"
}
I want myModel to look like this at the end of this merge:
myModel =
{
Id: '',
Category :'',
Version : "12.1",
Values: {
userName: '',
pasword: '',
address: '',
"somenewProp" : "with a value"
}
}
I have the exact same object format with values that I want to merge, I am doing the following:
this.myModel = Object.assign(this.myModel, ...newModel);
MyModel doesnt change, I need to be able to merge new properties and assign properties from the newModel into myModel
Object.assign is not recursive, nothing in the ECMAScript standard library offers recursive merging of two objects. You need to either :
Write your own recursive merge function
Use a third party library (eg. merge, lodash.merge, etc..)
Assume;
myModel = {
Id: '',
Category :'',
Values: {
userName: '',
pasword: '',
address: ''
}
}
subModel = {
Version : "12.1",
Values : {
"somenewProp" : "with a value"
}
}
Then newModel = {...myModel, Version: subModel.Version, Values: {...subModel.Values, ...myModel.Values}} will give you the result you want. Although, the readability of the code would be poor.

Ionic 2 / 3: Number Input from Alert

I'm using Ionic 3.x on macOS.
I have the following issue:
I have an array containing a number and an array of names.
table: { number: number, names: string[] } = {
number: 0,
names: ['']
};
I want to set the number of the array using an input for the user. I stumbled upon the AlertController.
I have written the following function thing to add a number:
addTable(){
let prompt = this.alertCtrl.create({
title: 'Add Table',
subTitle: 'Enter the table number',
inputs: [{
name: 'tableNumber',
placeholder: 'Number',
type: 'number'
}],
buttons: [
{
text: 'Cancel'
},
{
text: 'Add',
handler: data => {
//this.tables.push(data);
this.table.number = data;
}
}
]
});
prompt.present();
}
But this always sets table.number to object [object]. If I write it as this.table.number = +data; it has the value NaN. The push version also doesn't work.
How do I set table.number to a number that the user put in?
The name of the input
name: 'tableNumber'
gets added as a property name to the resulting object. You can access it like this:
handler: data => {
this.table.number = data.tableNumber;
}

Sencha Touch 2 Search List

am having issue designing and getting a search field to work, i don't know how to get this working, i can see any documentation or sample code on Sencha Touch 2. any help will be appreciated. this my current stage:
`Ext.define('ikhlas.view.SearchProfile', {
extend: 'Ext.Panel',
xtype: 'searchpanel',
config:{
title: 'Search',
iconCls: 'search',
scrollable: true,
styleHtmlContent: true,
items: [
{
xtype: 'fieldset',
title:'Search Profile',
iconCls:'add',
items: [
{
xtype: 'searchfield',
name:'searchfield',
placeHolder:'Search',
},
]
},
]
}
});`
And my controller looks like this (Noting has been done, i don't know how to start help pls):
Ext.define('ikhlas.controller.SearchField',{
extend: 'Ext.app.Controller',
config:{
refs:{
submitpanel:'loginpanel'
},
control:{
}
},
});
And here are the list of Data's i want to Auto search:
data: [
{firstName: 'Tommy', lastName: 'Maintz'},
{firstName: 'Rob', lastName: 'Dougan'},
{firstName: 'Ed', lastName: 'Spencer'},
{firstName: 'Jamie', lastName: 'Avins'},
{firstName: 'Aaron', lastName: 'Conran'},
{firstName: 'Dave', lastName: 'Kaneda'},
{firstName: 'Michael', lastName: 'Mullany'}
i want the search field to work in such a way that as the user is typing in character, it will auto pop suggestion just like: http://docs.sencha.com/touch/2-0/#!/example/search-list
In your controller you should listen for two events, clearicontap and keyup for the searchfield.
...
control: {
'searchfield': {
keyup: 'onSearchQueryChanged',
clearicontap: 'onSearchReset'
}
},
onSearchQueryChanged: function(field) {
// as in sample
//get the store and the value of the field
var value = field.getValue(),
store = this.getStore(); //you should actually point to the real store
//first clear any current filters on thes tore
store.clearFilter();
//check if a value is set first, as if it isnt we dont have to do anything
if (value) {
//the user could have entered spaces, so we must split them so we can loop through them all
var searches = value.split(' '),
regexps = [],
i;
//loop them all
for (i = 0; i < searches.length; i++) {
//if it is nothing, continue
if (!searches[i]) continue;
//if found, create a new regular expression which is case insenstive
regexps.push(new RegExp(searches[i], 'i'));
}
//now filter the store by passing a method
//the passed method will be called for each record in the store
store.filter(function(record) {
var matched = [];
//loop through each of the regular expressions
for (i = 0; i < regexps.length; i++) {
var search = regexps[i],
didMatch = record.get('firstName').match(search) || record.get('lastName').match(search);
//if it matched the first or last name, push it into the matches array
matched.push(didMatch);
}
//if nothing was found, return false (dont so in the store)
if (regexps.length > 1 && matched.indexOf(false) != -1) {
return false;
} else {
//else true true (show in the store)
return matched[0];
}
});
}
},
onSearchReset: function(field) {
this.getStore().clearFilter();
}
...
This example will emulate the same behavior as in ST2 SDK, that is filtering a store of Ext.List. Naturally, you will probably end up implementing your own logic for filtering.
Note that searchfield is nothing more but a styled textfield, usually with clear button to the right (depends on browser/os), as defined in HTML5.

How to add records in json-store

var store = new Ext.data.JsonStore({
id:'jfields',
totalProperty:'totalcount',
root:'rows',
url: 'data.php',
fields:[{ name:'jfields' },
{ name:'firstyear' , mapping :'firstyear' , type:'float' },
{ name:'secondyear', mapping :'secondyear', type:'float' },
{ name:'thirdyear' , mapping :'thirdyear' , type:'float' },
{ name:'fourthyear', mapping :'fourthyear', type:'float' },
{ name:'fifthyear' , mapping :'fifthyear' , type:'float' } ]
}
});
What I want is to add data at the end for this store , but I am totally confused , what I did is I add the following code to it but not working.
listeners: {
load : function(){
BG_store.add([{"jfields":"Monthly","firstyear":22.99,"secondyear":21.88,"thirdyear":21.88,"fourthyear":22.99,"fifthyear":21.88}]);
}
}
But I do not think my concept are cleared ,Please any body show some way how to do it .
You need to define a record type, create it and at it, e.g:
TaskLocation = Ext.data.Record.create([
{name: "id", type: "string"},
{name: "type", type: "string"},
{name: "type_data", type: "string"},
{name: "display_value", type: "string"}
]);
Then:
var record = new TaskLocation({
id: Ext.id(),
type: "city",
type_data: "",
display_value: "Brighton"
});
Then:
my_store.add(record);
my_store.commitChanges();
Remember by the time the data is in the store it's not in the same format as you sent it down but in Record objects instead.
See the recordType property in the JsonStore. It's a function that can be used as a record constructor for the store in question. Use it like this:
var newRecord = new myStore.recordType(recordData, recordId);
myStore.add(newRecord);
I have also figured out a simple solution to this:
listeners: {
load: function( xstore , record , option ) {
var u = new xstore.recordType({ jfields : 'monthly' });
xstore.insert(record.length, u);
}
}
Here what I have to add is this listeners as when the data loads it will create the record type and u can add fields as data as many as u want

Resources