Types.Relationship filter on the fly in AdminUI

495 views
Skip to first unread message

heru joko

unread,
Jul 14, 2014, 9:26:33 PM7/14/14
to keyst...@googlegroups.com
Hi,
I had 3 models (Country , State , Address) which Country has relationship to State and Address had both Country and State. I've been looking this group for solution and found similar problem. By using this reference I got it work great. The State drop down relationship could populate related State from selected Country but when I change the Country I need to save the data first then the State drop down can populate their related item to selected Country. My question is there any solution to repopulate the State drop down based on the selected Country without saving data first in AdminUI? . Here is my code :

/* /model/Country.js */

var keystone = require('keystone'),
Types = keystone.Field.Types;

/**
 * Country Model
 * ==================
 */

var Country = new keystone.List('Country');

Country.add({
name: { type: Types.Text, required: true },
code : { type: Types.Text}
});

Country.relationship({ ref: 'Address', path: 'country' , refpath: 'name' });
Country.relationship({ ref: 'State', path: 'country' });

Country.register();


/* model/State.js */

var keystone = require('keystone'),
Types = keystone.Field.Types;

/**
 * State Model
 * ==================
 */

var State = new keystone.List('State' , function(){
drilldown : 'country'
});

State.add({
name: { type: Types.Text, required: true } ,
country : { type: Types.Relationship, ref:'Country' , initial:true}
});

State.relationship({ ref: 'Address', path: 'state' });
State.register();

/* /model/Address.js */

var keystone = require('keystone'),
Types = keystone.Field.Types;
/**
 * Address Model
 * ==================
 */

var Address = new keystone.List('Address', {
autokey: { from: 'name', path: 'key', unique: true },
});

Address.add({
name: { type: Types.Text, required: true , initial:true },
country: { type: Types.Relationship, ref: 'Country' , initial:true, note:'save first to populate state'},
        state: { type: Types.Relationship, ref:'State' , filters: { country:':country' }}
});

console.log(this.country);
Address.defaultColumns = 'name, country, state';
Address.register();

/* END */

the line I made bold is the code where I filter the State relationship based on Selected Country using filters option. It has been great but it would be better if i can repopulate the State relationship based o selected Country on the fly.

李萌

unread,
Oct 16, 2018, 11:08:41 AM10/16/18
to Keystone JS
Hello , did you have already solved it ? I have the same questions.
Reply all
Reply to author
Forward
0 new messages