Skip to content

Instantly share code, notes, and snippets.

@minhhungit
Created October 17, 2019 02:14
Show Gist options
  • Save minhhungit/fb60bfc0c4bbbe82bca8518e27f2c2db to your computer and use it in GitHub Desktop.
Save minhhungit/fb60bfc0c4bbbe82bca8518e27f2c2db to your computer and use it in GitHub Desktop.
lookupeditor: pass parameters and change lookupkey
constructor() {
super();
this.form.MyOption.change(e => {
let currentValue = Serenity.EditorUtils.getValue(this.form.MyOption);
//let currentText = Serenity.EnumFormatter.format(MyEnum, Q.toId(currentValue));
//Q.notifySuccess(`You selected ${currentText}, lookup items will be reloaded`);
// clear old value
this.form.TestLookup.value = null;
// pass value into lookup editor and update items
this.form.TestLookup.myId = currentValue;
this.form.TestLookup.updateItems();
// select first lookup item after changing
if (this.form.TestLookup.items && this.form.TestLookup.items.length > 0) {
Serenity.EditorUtils.setValue(this.form.TestLookup, this.form.TestLookup.items[0].id);
}
});
}
[FormScript("YOUR_FORM_SCRIPT")]
[BasedOnRow(typeof(Entities.YOUR_ROW), CheckNames = true)]
public class YOUR_FORM
{
public Int32 MyOption { get; set; }
[DisplayName("Test Lookup")]
public Int32 TestLookup { get; set; }
}
[EnumKey("Default.MyEnum")]
public enum MyEnum
{
County = 100,
State = 200
}
namespace [YOUR_NAMESPACE] {
@Serenity.Decorators.registerEditor()
export class MyLookupEditor extends
Serenity.LookupEditorBase<Serenity.LookupEditorOptions, any> {
public myId: number;
constructor(container: JQuery, opt: Serenity.LookupEditorOptions) {
super(container, opt);
}
protected getLookupKey() {
return this.buildLookupKey(this.myId);
}
protected getItems(lookup: Q.Lookup<any>) {
var customLookup = Q.getLookup(this.buildLookupKey(this.myId));
let items: any = super.getItems(customLookup);
// this is demo about filtering lookup items
// only take item that has Id % 5 = 0 (5, 10, 15, 20...)
items = items.filter(x => Q.toId(x.Id) % 5 == 0); // here 'Id' field is hardcode for demo
// just take maximum first 5 items
if (items.length >= 5) {
return items.slice(0, 5);
}
else {
return items;
}
}
private buildLookupKey(id?: number): string {
//demo switch lookup key by id
if (id == 100) {
return "Default.County";
} else {
if (id == 200) {
return "Default.State";
}
else {
return "Supplier.Supplier"; // default
}
}
}
}
}
[RadioButtonEditor(EnumKey = "Default.MyEnum")]
public MyEnum? MyOption
{
get { return (MyEnum)Fields.MyOption[this]; }
set { Fields.MyOption[this] = (Int32)value; }
}
[MyLookupEditor]
public Int32? TestLookup
{
get { return Fields.TestLookup[this]; }
set { Fields.TestLookup[this] = value; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment