PDA

View Full Version : [CLOSED] (Razor) Trying to find and select a newly created record in a buffered and pre-filtered gridpanel



JakeM
Apr 16, 2014, 6:28 PM
I have a gridpanel with a filter on the proxy. I also have a formpanel that I am using so save a new record. The grid then reloads from the proxy with the filter. I am trying to select the newly created record in the grid after reload.

The view:


<script>
var saveClientX = function (btn) {
var grid = btn.up('window').down('grid');
var store = grid.store;
submit(btn.up('window').down('form'), '@Url.Action("SaveClientX")', store, grid);

};
function submit(form, url, store, grid) {
form.submit({
url: url,
waitMsg: "Saving Client...",
success: function (form, action) {
var result = action.result;
App.newClientForm.hide();
var formValues = App.newClientForm.getValues();
App.newClientForm.reset();
App.ClientStore.reload();
var clientName = formValues.clientName;
var record = App.ClientStore.findRecord("Name", clientName);
grid.getSelectionModel().select(record);
},
failure: function (form, action) {
if (action.failureType === Ext.form.action.Action.CONNECT_FAILURE) {
Ext.Msg.alert('Error',
'Status:' + action.response.status + ': ' +
action.response.statusText);
}
if (action.failureType === Ext.form.action.Action.SERVER_INVALID) {
}
}
})
};
var resetClientForm = function () {
App.newClientForm.reset();
};
var reloadGrid = function () {
App.ClientStore.reload();
}
var showNewClientForm = function () {
App.ClientGrid.hide();
var size = App.newClientWindow.getSize();
size.width = 360;
size.height = 390;
App.newClientWindow.setSize(size)
App.newClientForm.show();
};
var hideNewClientForm = function () {
App.newClientForm.reset();
App.newClientForm.hide();
var size = App.newClientWindow.getSize();
size.width = 710;
size.height = 460;
App.newClientWindow.setSize(size)
App.ClientGrid.show()
};
</script>
@(X.Window()
.ID("newClientWindow")
.Title("Link Account " + ViewBag.AccountToLink + " to Client")
.Layout(LayoutType.Column)
.Hidden(true)
.Width(710)
.Height(460)
.Icon(Icon.ArrowIn)
.Items(
X.FormPanel()
.ID("newClientForm")
.Hidden(true)
.Width(350)
.Frame(true)
.Items(
X.FormPanel()
.Layout(LayoutType.Form)
.Url("saveClientX")
.Items(items =>
{
//items.Add(X.Label("").ID("clientId").Hidden(true));
items.Add(X.TextField().FieldLabel("Client Name").ID("clientName").Flex(3));
items.Add(X.DateField().FieldLabel("IAA Date").ID("date").Flex(2));
items.Add(X.TextField().FieldLabel("Category").ID("category").Flex(1));
items.Add(X.Checkbox().FieldLabel("Active").ID("isActive").Flex(1));
items.Add(X.TextField().FieldLabel("State").ID("state").Flex(1));
items.Add(X.ComboBox()
.Editable(false)
.FieldLabel("Advising Person")
.ID("advisor")
.Items(ViewBag.ComboAdvisors).Flex(1));
}
)
.TopBar(
X.Toolbar()
.Items(
X.Button()
.Text("Save Client")
.Icon(Icon.Add)
.Icon(Icon.DatabaseSave)
.Listeners(ls =>
ls.Click.Fn = "saveClientX"
),//save client button listener
X.Button()
.Text("Reset")
.Icon(Icon.Exclamation)
.Listeners(ls =>
ls.Click.Fn = "resetClientForm"
),//reset button listener
X.Button()
.Text("Close")
.Icon(Icon.DoorOut)
.Listeners(ls =>
ls.Click.Fn = "hideNewClientForm"
)//close button listener
)//bottombar items
)//topbar
),//FormPanel Items
X.GridPanel()
.ID("ClientGrid")
.Width(700)
.Height(450)
.Store(X.Store()
.ID("ClientStore")
.Proxy(
X.AjaxProxy()
.Url(Url.Action("GetClients"))
.Reader(X.JsonReader().Root("data").TotalProperty("total"))
)//Proxy
.AutoLoad(true)
.AutoSync(true)
.PageSize(50)
.Model(X.Model()
.Fields(
new ModelField("Id", ModelFieldType.Int),
new ModelField("Name", ModelFieldType.String),
new ModelField("IAASigned", ModelFieldType.Date),
new ModelField("ClientCategory", ModelFieldType.String),
new ModelField("IsActive", ModelFieldType.Boolean),
new ModelField("State", ModelFieldType.String),
new ModelField("AdvisorId", ModelFieldType.String)
)//Fields
)//Model
.Sorters(
X.DataSorter().Property("Name").Direction(Ext.Net.SortDirection.ASC)
)//Sorters
)//Store
.Hidden(false)
.View(Html.X().GridView().TrackOver(false))
.ColumnModel(
X.RowNumbererColumn().Sortable(false).Width(35),
X.Column().Text("Id").DataIndex("Id").Hidden(true),
X.Column().Text("Client Name").DataIndex("Name").Flex(1),
X.DateColumn().Text("IAA Signed").DataIndex("IAASigned").Format("d").Width(100),
X.Column().Text("Category").DataIndex("ClientCategory").Width(75),
X.CheckColumn().Text("Active").DataIndex("IsActive").Width(50),
//X.Column().Text("Advisor").DataIndex("AdvisorId").Flex(1),
X.Column().Text("State").DataIndex("State").Width(50)
)//ColumnModel
.Features(X.GridFilters()
.Filters(
X.NumericFilter().DataIndex("Id"),
X.StringFilter().DataIndex("Name"),
X.DateFilter().DataIndex("IAASigned").DatePickerOptions(options => { options.TodayText = "Now";}),
X.StringFilter().DataIndex("ClientCategory"),
X.BooleanFilter().DataIndex("IsActive").Active(true).DefaultValue(true),
X.StringFilter().DataIndex("State")
)//Filters
)//Features
.SelectionModel(
X.RowSelectionModel()
.Mode(SelectionMode.Single)
.PruneRemoved(false))
.TopBar(
X.Toolbar()
.Items(
X.Button()
.Text("Create New Client")
.Icon(Icon.Add)
.Listeners(ls =>
ls.Click.Fn = "showNewClientForm"
),//button create new client
X.Button()
.Text("reload grid")
.Icon(Icon.ArrowRefresh)
.Listeners(ls =>
ls.Click.Fn = "reloadGrid"
)//Listeners and button reload grid
)//Toolbar Items
)//Topbar
)//Window newClientWindow Items


The Controller:


public ActionResult GetClients(StoreRequestParameters parameters)
{
AlbridgeImportData importedData = Session["AlbridgeImportedData"] as AlbridgeImportData;
importedData.GetClientList();

FilterConditions fc = parameters.GridFilters;
if (fc != null)
{
foreach (FilterCondition condition in fc.Conditions)
{
Comparison comparison = condition.Comparison;
string field = condition.Field;
FilterType type = condition.Type;

object value;
switch (condition.Type)
{
case FilterType.Boolean:
value = condition.Value<bool>();
break;
case FilterType.Date:
value = condition.Value<DateTime>();
break;
case FilterType.List:
value = condition.List;
break;
case FilterType.Numeric:
if (importedData.ClientList.Count > 0 && importedData.ClientList[0].GetType().GetProperty(field).PropertyType == typeof(int))
{
value = condition.Value<int>();
}
else
{
value = condition.Value<double>();
}
break;
case FilterType.String:
value = condition.Value<string>();
break;
default:
throw new ArgumentOutOfRangeException();
}//switch
importedData.ClientList.RemoveAll(
item =>
{
object oValue = item.GetType().GetProperty(field).GetValue(item, null);
IComparable cItem = oValue as IComparable;

switch (comparison)
{
case Comparison.Eq:

switch (type)
{
case FilterType.List:
return !(value as List<string>).Contains(oValue.ToString());
case FilterType.String:
return !oValue.ToString().StartsWith(value.ToString());
default:
return !cItem.Equals(value);
}//switch type

case Comparison.Gt:
return cItem.CompareTo(value) < 1;
case Comparison.Lt:
return cItem.CompareTo(value) > -1;
default:
throw new ArgumentOutOfRangeException();
}//switch comparison
}//item
);//removeall not of the filter
}//foreach condition filter
}//if filter not null
return this.Store(importedData.ClientList, importedData.ClientList.Count);
}
public ActionResult SaveClientX(FormCollection values)
{
int clientId = 0;
string clientStringId = "0";
string clientNameLabel = values.AllKeys.Where(k => k.StartsWith("clientName")).FirstOrDefault();
string dateLabel = values.AllKeys.Where(k => k.StartsWith("date")).FirstOrDefault();
string categoryLabel = values.AllKeys.Where(k => k.StartsWith("category")).FirstOrDefault();
string activeLabel = values.AllKeys.Where(k => k.StartsWith("isActive")).FirstOrDefault();
string stateLabel = values.AllKeys.Where(k => k.StartsWith("state")).FirstOrDefault();
string advisorLabel = values.AllKeys.Where(k => k.StartsWith("advisor")).FirstOrDefault();
FieldErrors errors = new FieldErrors();
foreach (string key in values.Keys)
{
if (key == activeLabel)
continue;
if(values[key] == "")
errors.Add(new FieldError(key.ToString(), "Test " + key.ToString() + ""));
}
if (errors.Count > 0)
return this.FormPanel("Errors:", errors);

string clientName = values[clientNameLabel];
DateTime date = Convert.ToDateTime(values[dateLabel]);
string category = values[categoryLabel];
bool active = new bool();
if (values[activeLabel] == null)
active = false;
else
active = true;
string state = values[stateLabel];
int advisor = Convert.ToInt16(values[advisorLabel]);

Client myNewClient = new Client();
myNewClient.Name = clientName;
myNewClient.AdvisorId = advisor;
myNewClient.IsActive = active;
myNewClient.IAASigned = date;
myNewClient.ClientCategory = category;
myNewClient.State = state;
try
{
db.Clients.Add(myNewClient);
db.SaveChanges();
clientId = myNewClient.Id;
clientStringId = clientId.ToString();
X.Msg.Alert("Client Saved", "Successful.").Show(); //, new JFunction { Fn = "newClientLinkAccount" }
MessageBus.Default.Publish("clientId " + clientId);
return this.FormPanel(true);
}
catch (Exception ex)
{
return this.FormPanel(false);
}
}

I have looked at the non-buffered findRecord http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Store-method-findRecord
but that won't work for me. Any help or advice on the code would be greatly appreciated.

Daniil
Apr 16, 2014, 11:20 PM
Hi @JakeM,

Hard to say. If you can provide a simplified runnable example to reproduce the problem, I can investigate.

JakeM
Apr 17, 2014, 5:15 PM
It isn't really a problem with my code, it is more a question of how to do something. As far as a runnable example, that is actually what I am looking for. The closest to what I am looking for is a combination the remote paging and sorting example: http://mvc.ext.net/#/GridPanel_Paging_and_Sorting/Remote/

with the ability in this case to save a new plant to the model, update and sort the grid store, then select the newly created record.

The question outside of all of that is simply, how do I select a record by value in a grid/store that is sorted remotely. I know how to filter and sort, but not how to select with a "find".

The store "find" "findBy" "findExact" and "findRecord" : http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Store-method-find will not work on buffered stores.

Daniil
Apr 17, 2014, 6:40 PM
It isn't really a problem with my code, it is more a question of how to do something.

Yes, you are right. But I don't request your code, I request something to test with.

So, there is a Store with the Buffered="true" setting, isn't?