PDA

View Full Version : [CLOSED] Combobox with AjaxProxy - bind data at initial load



mj.daly
Feb 04, 2013, 4:25 PM
I have a combobox that loads data through an AjaxProxy when the user types in some characters, this is working fine.

If the user is editing a record, when the page first loads, I want to have the combobox store the current value and show the correct DisplayText.





var companyName = @Html.X()
.ComboBox()
.ListConfig(new BoundList { ItemTpl = new XTemplate { Html = "<tpl for='.'><div class='list-item'>{CompanyName} ({InfusionSoftCompanyId})</div></tpl>" } })
.DisplayField("CompanyName")
.ValueField("InfusionSoftCompanyId")
.FieldLabel("Copy in data from InfusionSoft")
.HideBaseTrigger(true)
.EmptyText("Type part of the company name...")
.ID("InfusionSoftCompanyId")
.Triggers(t => t.Add(new FieldTrigger { Icon = TriggerIcon.Clear, Qtip = "Clear" }))
.Listeners(l => l.TriggerClick.Handler = "this.removeByValue(this.getValue());this.clearValu e();")
.MinChars(1)
.Store(s =>
{
var store = new Store { AutoLoad = false };

var proxy = new AjaxProxy { Url = "/webservices/InfusionSoft/FindCompany.ashx" };
var reader = new JsonReader { Root = "companies", TotalProperty = "total" };

proxy.ActionMethods.Read = HttpMethod.POST;
proxy.Reader.Add(reader);

var model = new Model();
typeof(Company).GetPropertyList().ForEach(p => model.Fields.Add(p));

store.Proxy.Add(proxy);
store.Model.Add(model);

if (Model.InfusionSoftCompanyId != null)
{
store.DataSource = new object[] {Model};
store.DataBind();
}

s.Add(store);
})
.QueryMode(DataLoadMode.Remote)
.QueryDelay(500)
.Listeners(l => { l.Select.Fn = "populateForm"; });

if (Model.InfusionSoftCompanyId != null)
{
companyName.Value(Model.InfusionSoftCompanyId);
}


Using the above code, the id is shown instead of the display text. I also tried using EmptyValue instead of setting a value, but this is not submitted in the POST.

Baidaly
Feb 05, 2013, 12:02 AM
Hello!

Sorry, but I don't quite understand your scenario. Can you provide simplified sample of your problem?

Daniil
Feb 05, 2013, 5:30 AM
Hi everybody,

Just a quick thought. I don't think you should mix up a store.DataBind call and using of an AjaxProxy.

mj.daly
Feb 05, 2013, 9:14 AM
Hi everybody,

Just a quick thought. I don't think you should mix up a store.DataBind call and using of an AjaxProxy.

Yeah, I was just hoping it would convey what I was trying to do, even though it doesn't work.

I've now updated the webservice/ webhandler called by the AjaxProxy to take another parameter, id. The id parameter will be set in the AutoLoadParameters.

The only problem (not really a problem, more a visual gripe) I see now is that when the page loads, the companyName control shows the id to the user, until the AjaxProxy has completed filling the store, then the proper DisplayText is shown.

If a combobox has an empty store, can I not set the value without showing that value to the user? Users freak out when they don't understand something, they won't understand why the companyName combobox shows a number (id) and then a second later (once the AjaxProxy fills the store) shows the correct text.




var companyName = @Html.X()
.ComboBox()
.ListConfig(new BoundList { ItemTpl = new XTemplate { Html = "<tpl for='.'><div class='list-item'>{CompanyName} ({InfusionSoftCompanyId})</div></tpl>" } })
.DisplayField("CompanyName")
.ValueField("InfusionSoftCompanyId")
.FieldLabel("Copy in data from InfusionSoft")
.HideBaseTrigger(true)
.EmptyText("Type part of the company name...")
.ID("InfusionSoftCompanyId")
.Triggers(t => t.Add(new FieldTrigger { Icon = TriggerIcon.Clear, Qtip = "Clear" }))
.Listeners(l => l.TriggerClick.Handler = "this.removeByValue(this.getValue());this.clearValu e();")
.MinChars(1)
.Store(s =>
{
var proxyLocation = isCreate ? "../" : "../../";


var store = new Store { AutoLoad = false };


var proxy = new AjaxProxy { Url = proxyLocation + "webservices/InfusionSoft/FindCompany.ashx" };
var reader = new JsonReader { Root = "companies", TotalProperty = "total" };


proxy.ActionMethods.Read = HttpMethod.POST;
proxy.Reader.Add(reader);


var model = new Model();
typeof(Company).GetPropertyList().ForEach(p => model.Fields.Add(p));


store.Proxy.Add(proxy);
store.Model.Add(model);


if (Model.InfusionSoftCompanyId != null)
{
store.AutoLoad = true;
store.AutoLoadParams.Add(new Parameter
{
Name = "id",
Value = Model.InfusionSoftCompanyId.ToString()
});
}


s.Add(store);
})
.QueryMode(DataLoadMode.Remote)
.QueryDelay(500)
.Listeners(l => { l.Select.Fn = "populateForm"; });


if (Model.InfusionSoftCompanyId != null)
{
companyName.Value(Model.InfusionSoftCompanyId);
}

Daniil
Feb 05, 2013, 12:24 PM
Seems the requirement is clear now.

Please try this:

if (Model.InfusionSoftCompanyId != null)
{
store.Data = new object[] {Model};
}
instead of

if (Model.InfusionSoftCompanyId != null)
{
store.DataSource = new object[] {Model};
store.DataBind();
}
in your initial sample.

mj.daly
Feb 06, 2013, 8:49 AM
Good man Daniil, that's exactly what I needed to know.