SmartGWT - Filter Listgrid data depending on entered value in another cell - smartgwt

I have a listgrid which has 4 columns. Code,Name,Qty and Price. the name cell is a Combo Box which loads the names via datasource. So far all is fine. now i want to be able to enter a code in the Code col cell and then i want the Combo box to display the name in the Name cell and the corresponding Price in the Price cell. Can somebody help me to achieve this.I have attached a screenshot to make things more clear.
cheers
Zolf

You can add ChangedHandler/BlurHandler on Code field which will be invoked when user has entered the Code and try to navigate to the next field.
Within the event handler, you need to wirte your logic to set Name and Price based on Code value.
listGrid.getField("Code").addChangedHandler(new ChangedHandler() {
#Override
public void onChanged(ChangedEvent event) {
Record r=listGrid.getRecord(event.getRowNum());
String code=(String)event.getValue();
//add your logic to get Name and price based on Code value here
r.setAttribute("Name", name);
r.setAttribute("Price", price);
}
});

Related

Is there a way to enable row editing on only new added rows within a Kendo grid?

So I have a standard Kendo grid in MVC with 2 non-editable columns. What I would like to achieve is to enable editing of all columns only within the newly added rows.
Example: There's 4 rows already in the grid, first 2 columns non-editable. User clicks "Add new", a new record appears in the grid with everything editable.
Try adding an editable function handler to your column definition, something like this:
{
field: "salary",
editable: function (dataItem) {
return dataItem.isNew();
}
}
Please note: you will need to have specified an id column in your model definition for this to work, for details see isNew documentation:
Checks if the Model is new or not. The id field is used to determine if a model instance is new or existing one. If the value of the field specified is equal to the default value (specified through the fields configuration) the model is considered as new.

How to set default value for any field of a ViewModel using Kendo UI Grid and ASP.NET MVC?

When we click on the Add New or Edit button using Kendo UI Grid, how do we set the default value for any model field?
For example I have two view models City and State. City view model contains the StateID as the foreign key to the City model.
Let's assume that I have a view page where I select from a State dropdownlist to get a Kendo Grid populated with the list of cities for that state. Now I want to create a new city within that state so I need to set the StateID value to the selected value from the dropdownlist.
How do I achieve this functionality?
Well I spent quite some time to figure out the above issue till I stumbled upon this link. And the solution was pretty easy.
All I need to do is add "Edit" event and specify the javascript function which will be called on edit for the Kendo UI grid. Then we can set the default value on the javascript function.
#(Html.Kendo().Grid()
.Events( e => e.Edit("onEdit") )
)
<script type="text/javascript">
function onEdit(e) {
var stateID = $("#hfStateID).val(); // I have set the dropdownlist selected value to //the hidden field
//check if record is new
if (e.model.isNew()) {
//set the default value for StateID
e.model.set("StateID", stateID );
}
}
</script>
Thought it might help someone.
Thanks.
Agree with the ajexpess.
I also had to remove the Required DataAnotation.
For example Remove: [Required(ErrorMessage = "Value should not be empty")] from the property you are trying to set.

SelectItem with property multiple select

I´m using the SelectItem component with configuration:
private SelectItem nElementsCombo;
nElementsCombo = new SelectItem();
nElementsCombo.setMultiple(true);
nElementsCombo.setMultipleValueSeparator("|");
In the combo the elements selected are shown item_selected_1|item_selected_2|item_selected_3
but when I do:
nElementsCombo.getValueAsString()
Return item_selected_1,item_selected_2,item_selected_3 and I´d like item_selected_1|item_selected_2|item_selected_3
How can I solve this?
from the javadoc :If this item is displaying multiple values, this property will be the string that separates those values for display purposes. Display purpose
I don't catch it, can you replace in your returned string the comma by the pipe ..... !!!
As per Alain's answer, MultipleValueSeparator is only for display purpose.
Means, when you select multiple values from picklist & then when the picklist is hidden on blur (focus lost) of multi select item, the selected values are displayed as a string separated by comma(default). This display can be changed by MultipleValueSeparator. But not the one you get by multiSelectItem.getValueAsString().
Also I don't think, there's any provision in SmartGWT API which fulfills your requirement, as of now.

How to get the selected text from a system.web.mvc.selectlist

I have a system.web.mvc.selectlist when I use .selectedvalue it gives me the value as expected however I use an int ID as the value and would like to get the display text instead.
Update
I've created a selectlist and I'd like to retrieve the selected text on the next line of code. I.e.
SelectList sl = new SelectList(items, "id", "name", 10);
String txt= sl.selectedvalue.text;
That last line is where I am stuck. I'm looking to get the name field for the item with id 10. Ideally without looking up in the db as I want a generic function I can use on all select lists.
I don't think this is possible since the text isn't passed back to the server in a post, only the value is. I can think of two ways to get it though:
Query the database with the value to get the text.
Set the text in a hidden field on the client side before posting the
form. You can do this with jQuery for example.

Sending custom values on save and update using Telerik Grid

I have a Telerik grid in my asp.net mvc application that looks something like:
Now it lists all the regions in a zone selected from the list placed just above the grid. zoneid is foreign key in the grid. Now I want that when I add new region in the grid the zoneID should be taken from the list instead of what is present in zone column of the grid because that value is just to display the zone and that can also be removed from the grid as it as apparent from the list which zone the listed regions belong to.
I understand that I could have used editor templates to show this list inside the grid on edit but I prefer it to be outside the grid and provide some filtering on basis of the zone as we are likely to have many regions per zone. so my concern here, now, is how can I set ZoneID of a region (edited or newly added) equal to selected value of list that shows just above the grid control.
When you click on the AddNewRecord button, why don't you set the Value of your zone equals to the zoneId selected in the combobox value ?
I've done something a little similar, but I had to get the value from a Treeview.
private void btnAddContact_Click(object sender, EventArgs e)
{
Int64 companyId = Int64.Parse(treeCompany.SelectedNode.Name);
dsSociete.ContactRow newContact = dsSociete.Contact.NewContactRow();
newContact.SocieteId = societeId;
dsSociete.Contact.AddContactRow(newContact);
}
And once i add a new Contact, it gets automatically its Company (Societe is Company in French) set.
I did it in Winform, but I guess you can do the same in Web?
I solved this problem by hooking the onSave event of Telerik grid like
<%
Html.Telerkik.Grid<xyz.company>()
.Name("name")
.// other properties
.Events(even=>even.onSave("onSave")
.Render();%>
Inside onSave event handler in JS I have written something like
function onSave(e)
{
var data = e.values;
data["companyID"] = $("#CompanySelectList").val();
e.values = data;
return true;
}
onSave event adds the companyID at companyID index of json that will be submitted to the server and modelbinder will bind it with concerning property name of model.

Resources