PDA

View Full Version : [CLOSED] Adding extra listitem at top combobox



CarWise
Jun 28, 2012, 2:26 PM
Hi,

Trying to add an extra listitem in your Ajax Linked Combos example.
How to Achieve. I've tried combo.Items.Insert, but not succeeding. Where do I go wrong ?



<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Xml" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET Examples</title>
<script runat="server">
protected void CitiesRefresh(object sender, StoreReadDataEventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(HttpContext.Current.Server.MapPath("Cities.xml"));
List<object> data = new List<object>();
foreach (XmlNode cityNode in xmlDoc.SelectNodes(string.Concat("countries/country[@code='", this.ComboBox1.SelectedItem.Value, "']/city")))
{
string id = cityNode.SelectSingleNode("id").InnerText;
string name = cityNode.SelectSingleNode("name").InnerText;
data.Add(new { Id = id, Name = name });
}

this.CitiesStore.DataSource = data;
this.CitiesStore.DataBind();
// -- CHECK THIS --
Ext.Net.ListItem li = new Ext.Net.ListItem("New","0");
ComboBox2.Items.Insert( 0 , li );
}
</script>
<link href="/resources/css/examples.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />

<ext:ComboBox
ID="ComboBox1"
runat="server"
Editable="false"
TypeAhead="true"
QueryMode="Local"
ForceSelection="true"
TriggerAction="All"
SelectOnFocus="true"
EmptyText="Select a country">
<Listeners>
<Select Handler="#{ComboBox2}.clearValue(); #{CitiesStore}.reload();" />
</Listeners>
<Items>
<ext:ListItem Text="Belgium" Value="BE" />
<ext:ListItem Text="Brazil" Value="BR" />
<ext:ListItem Text="Bulgaria" Value="BG" />
<ext:ListItem Text="Canada" Value="CA" />
<ext:ListItem Text="Chile" Value="CL" />
<ext:ListItem Text="Cyprus" Value="CY" />
<ext:ListItem Text="Finland" Value="FI" />
<ext:ListItem Text="France" Value="FR" />
<ext:ListItem Text="Germany" Value="DE" />
<ext:ListItem Text="Hungary" Value="HU" />
<ext:ListItem Text="Ireland" Value="IE" />
<ext:ListItem Text="Israel" Value="IL" />
<ext:ListItem Text="Italy" Value="IT" />
<ext:ListItem Text="Lithuania" Value="LT" />
<ext:ListItem Text="Mexico" Value="MX" />
<ext:ListItem Text="Netherlands" Value="NL" />
<ext:ListItem Text="New Zealand" Value="NZ" />
<ext:ListItem Text="Norway" Value="NO" />
<ext:ListItem Text="Pakistan" Value="PK" />
<ext:ListItem Text="Poland" Value="PL" />
<ext:ListItem Text="Romania" Value="RO" />
<ext:ListItem Text="Slovakia" Value="SK" />
<ext:ListItem Text="Slovenia" Value="SI" />
<ext:ListItem Text="Spain" Value="ES" />
<ext:ListItem Text="Sweden" Value="SE" />
<ext:ListItem Text="Switzerland" Value="CH" />
<ext:ListItem Text="United Kingdom" Value="GB" />
</Items>
</ext:ComboBox>

<ext:ComboBox
ID="ComboBox2"
runat="server"
TypeAhead="true"
QueryMode="Local"
ForceSelection="true"
TriggerAction="All"
DisplayField="name"
ValueField="id"
EmptyText="Loading..."
ValueNotFoundText="Loading...">
<Store>
<ext:Store
runat="server"
ID="CitiesStore"
AutoLoad="false"
OnReadData="CitiesRefresh">
<Model>
<ext:Model runat="server" IDProperty="Id">
<Fields>
<ext:ModelField Name="id" Type="String" Mapping="Id" />
<ext:ModelField Name="name" Type="String" Mapping="Name" />
</Fields>
</ext:Model>
</Model>
<Listeners>
<Load Handler="#{ComboBox2}.setValue(#{ComboBox2}.store.getAt(0). get('id'));" />
</Listeners>
</ext:Store>
</Store>
</ext:ComboBox>
</form>
</body>
</html>

Daniil
Jun 28, 2012, 4:51 PM
Hi,

Please do not mix the ComboBox Store and Items. They should not be used at the same time.

I would add the new item into the "data" before it is applying to the Store DataSource.

CarWise
Jun 29, 2012, 6:51 AM
Hi,

Please do not mix the ComboBox Store and Items. They should not be used at the same time.

I would add the new item into the "data" before it is applying to the Store DataSource.

Ok..

Will do that. Thanks..

Martin

Vladimir
Jun 29, 2012, 9:23 AM
Combobox can use store data and Items at one time but Items must be defined initialy (before rendering)
For example, move ListItem to Items collection initially and 'New' item will be appended after each store reloading


<Items>
<ext:ListItem Text="New" Value="0" />
</Items>

Daniil
Jun 29, 2012, 10:57 AM
I have to clarify the following.


Please do not mix the ComboBox Store and Items. They should not be used at the same time.

It regards to DirectEvent/DirectMethod. Though, personally, I would avoid to use Store and Items at the same time for initial page load as well.