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

13 posts, 0 answers
  1. Gregory
    Gregory avatar
    47 posts
    Member since:
    Sep 2010

    Posted 28 Aug 2012 Link to this post

    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
  2. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 28 Aug 2012 Link to this post

    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!
  3. Kendo UI is VS 2017 Ready
  4. Richard
    Richard avatar
    6 posts
    Member since:
    Jan 2013

    Posted 25 Jan 2013 Link to this post

    Rosen, your link are great for static default values. How do you do this if the values are comming from a remote data source?
  5. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 29 Jan 2013 Link to this post

    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!
  6. Ross Micheals
    Ross Micheals avatar
    17 posts
    Member since:
    Oct 2006

    Posted 28 Mar 2013 Link to this post

    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!

        
  7. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 29 Mar 2013 Link to this post

    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!
  8. Ross Micheals
    Ross Micheals avatar
    17 posts
    Member since:
    Oct 2006

    Posted 29 Mar 2013 Link to this post

    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.
  9. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 01 Apr 2013 Link to this post

    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!
  10. Dilip
    Dilip avatar
    4 posts
    Member since:
    Apr 2013

    Posted 08 May 2013 Link to this post

    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
  11. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 09 May 2013 Link to this post

    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!
  12. Scott
    Scott avatar
    6 posts
    Member since:
    May 2015

    Posted 19 May in reply to Rosen Link to this post

    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();
  13. Scott
    Scott avatar
    6 posts
    Member since:
    May 2015

    Posted 19 May in reply to Scott Link to this post

    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

     

  14. Scott
    Scott avatar
    6 posts
    Member since:
    May 2015

    Posted 19 May in reply to Scott Link to this post

    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.
Back to Top
Kendo UI is VS 2017 Ready