How to set default values to a grid when adding new row

4 Answers 11297 Views
Grid
KobusVisagie
Top achievements
Rank 1
KobusVisagie asked on 28 Aug 2012, 07:37 AM
I'd like to set default values for a column when I add a new row to a grid.

How would I do that?

Thank you

4 Answers, 1 is accepted

Sort by
1
Rosen
Telerik team
answered on 28 Aug 2012, 07:54 AM
Hello Gregory,

A default value can be set through model's field definition. More information on the matter can be found here and here.

Greetings,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Richard
Top achievements
Rank 1
commented on 26 Jan 2013, 12:04 AM

Rosen, your link are great for static default values. How do you do this if the values are comming from a remote data source?
Rosen
Telerik team
commented on 29 Jan 2013, 07:11 AM

Hello Richard,

I'm afraid that you cannot change the default value declaration for given model dynamically. You should consider getting the values and assigning them before constructing the Model definition.

Regards,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Ross Micheals
Top achievements
Rank 1
commented on 28 Mar 2013, 07:16 PM

Rosen,

Can you provide an example on how you would do this? What do you mean specifically by "getting the values and assigning them before constructing the Model definition."

Thank you!

    
1
Rosen
Telerik team
answered on 29 Mar 2013, 09:11 AM
Hi Ross,

You should retrieve the values and then use them when constructing the Model definition. For example:

  var defaultValues = getDefaultValuesFromAService(); // returns a default values as an key value map { foo: "valueFoo", bar: "valueBar" }
   
  var model = new kendo.data.Model.define({
    id: "id",
    fields: {
      foo: {
        defaultValue: defaultValues["foo"]
      },
      bar: {
        defaultValue: defaultValues["bar"]
      }
    }
  });
  var dataSource = new kendo.data.DataSource({
      schema: {
          model: model
      }
      //....
  });

All the best,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Ross Micheals
Top achievements
Rank 1
commented on 29 Mar 2013, 01:23 PM

Will this work for dynamic values? That is, I'd like to change the default values of the new row, perhaps based on other values in the grid, or the state of another control on the page.
Rosen
Telerik team
commented on 01 Apr 2013, 07:08 AM

Hi Ross,

As I have already stated the default values cannot be changed dynamically.

However, you may use the Grid edit event to pre-populate the edit form:

edit: function(e) {
     if (e.model.isNew() && !e.model.dirty) {
       e.container
         .find("input[name=ProductName]") // get the input element for the field
         .val("MyCustomValue") // set the value
         .change(); // trigger change in order to notify the model binding
     }
   }

All the best,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Dilip
Top achievements
Rank 1
commented on 09 May 2013, 01:53 AM

Hi,

I'm using Kendo Grid Popup to add/edit data in MVC. When I add new record the ModelID field value remain 0 only. So when I try to edit the newly added record, instead of updating existing record it adds new record again. How to set ModelID field value when adding new record?

Also, I've drop downs on popup. I'm populating dropdowns from separate controller action. Could you please let me know how to populate dropdowns from List<T> property of same view model which is used to add/edit popup.

Regards,
Dilip
Rosen
Telerik team
commented on 09 May 2013, 06:08 AM

Hi Dilip,

As you may know the value of the id field determines if the record is new (when the value is set to the default) or it is already existing (if id value is different than the default one). Therefore, you should not modify the id manually before it is send to the server. 

As the thread is diverted from its initial subject, please consider starting a new thread if further question raised.

Greetings,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Scott
Top achievements
Rank 1
answered on 19 May 2016, 03:44 PM
I am using this method and the values are defaulting but when I click the update and it posts back to the server some values are null.  Mainly the ones I do not change manually.

arg.container.find("input[name='user_name']").val(customRowDataItem.user_name).change();
arg.container.find("input[name='group_name']").val(customRowDataItem.group_name).change();
arg.container.find("select[name='page_access_level_id']").val(customRowDataItem.page_access_level_id).change();
0
Scott
Top achievements
Rank 1
answered on 19 May 2016, 06:27 PM

I found the answer on StackOverflow:
arg.model.set('user_name', customRowDataItem.user_name);
arg.model.set('group_name', customRowDataItem.group_name);
arg.model.set('page_access_level_id', customRowDataItem.page_access_level_id);
arg.model.set('page_name_id', customRowDataItem.page_name_id);

 

I also had to set each filed in the schema to editable:true (That's what the article said to do).
http://stackoverflow.com/questions/30419736/dynamic-default-value-for-kendo-grid

 

Scott
Top achievements
Rank 1
commented on 19 May 2016, 06:28 PM

Just an FYI.  I left both version in the code it did not hurt so I left it.  I think one might set the GUI and the other the model.
Pooja
Top achievements
Rank 1
commented on 20 Dec 2017, 09:09 AM

I have a dropdownlist defined with two values. Grid data is populated based on the selection from the drop down. Now while adding new record to grid, i am unable to pass the value selected in the dropdown as default valueto one of the field in grid. Can someone help me to assign the dropdown value to field in grid using JSP?
Alex Hajigeorgieva
Telerik team
commented on 26 Dec 2017, 11:38 AM

Hi, Pooja,

The Kendo UI Model field default value can be set to a JavaScript function. In this function, you may obtain the Kendo UI DropDownList instance and its value() and return it.

https://docs.telerik.com/kendo-ui/api/javascript/data/model#methods-Model.define

Let me know if you need more help.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Matt
Top achievements
Rank 1
Veteran
commented on 13 May 2020, 06:30 PM

Could you guys either update all the links in your forum or fix the SSL cert on your website: https://docs.telerik.com/

the "here" and "here" links require bypassing the invalid SSLs throughout your support site.

 

Thanks.

Alex Hajigeorgieva
Telerik team
commented on 18 May 2020, 01:11 PM

Hi, Matt,

Thank you for bringing this to our attention.

It is the old domain of the Kendo UI documentation that has an expired certificate. I have addressed the reported behaviour with the team that is responsible for this as it should be redirected globally.

Meanwhile, I have fixed these particular links from this forum post.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
KobusVisagie
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Scott
Top achievements
Rank 1
Share this question
or