This is a migrated thread and some comments may be shown as answers.

Add data to grid by DataSource.add() cause an error 'field is not defined'.

10 Answers 796 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Worawat
Top achievements
Rank 1
Worawat asked on 19 Mar 2012, 09:18 AM
Hi,
I have this error for days, i'm find out how to fix this in this forum and still does not get it fix.
I have two grid in same page, each bind with datasouce and have a command button in the toolbar for add a record.

When i click at 'Add fee/cost' it work fine but then when i click at 'Add Loan' a error occured.
I dont know how to fix this, any one please me.

try
http://jsfiddle.net/mBYxS/7/ 

10 Answers, 1 is accepted

Sort by
0
Accepted
Alexander Valchev
Telerik team
answered on 20 Mar 2012, 04:01 PM
Hi Worawat,

Your code looks ok. I tested your example and the error appears in jsFiddle, but not when I ran the example locally. Could you please confirm that?
I believe the error in jsFiddle is caused by scope issues - if you define the schema inside the grid configuration everything works as expected. For example: http://jsfiddle.net/mBYxS/12/ 

All the best,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Worawat
Top achievements
Rank 1
answered on 20 Mar 2012, 05:10 PM
Hi Alexander,

Thanks, I thought that cause from I don't well understand about the Model and Datasource.

I have changed from http://jsfiddle.net/mBYxS/7/ to http://jsfiddle.net/mBYxS/14/ and every work fine.

If you have more detail about model and datasource in any document or demo it will be great.

Thanks.

0
Alexander Valchev
Telerik team
answered on 21 Mar 2012, 08:17 AM
Hello Worawat,

The dataSource's documentation could be found here, you may also check our code library projects as they give a brief idea how to set up dataSource binding from remote services.

Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
TrentCioran
Top achievements
Rank 1
answered on 27 Mar 2012, 03:54 PM
Hey Telerik Team,

Look like I'found a bug in the Q1 2012 release. This code
 based on the setup (just forked and modified, kendo version 2011.9.1129)  by Worawat shows the Parent column contents as supposed to, but this other that uses the latest kendo release (2012.1.322) does not show it.

Looks like a bug with the template processing.


-Regards,
Manuel
0
Alexander Valchev
Telerik team
answered on 28 Mar 2012, 11:08 AM
Hello Manuel,

In the first example I cannot see any field or column with the name "Parent", could you please tell me if I am missing something?

In the second example I noticed that the add new record functionality is not working. I believe that problem is caused by the fact that the field "HasPic" was not defined in the create function.
create: function () {
            $('#grid').data('kendoGrid').dataSource.add({
                Id: 'idString',
                Name: 'New',
                Description: 'Description',
                HasPic: false, //in the original code there is no HasPic field, which is causing error: Uncaught ReferenceError: HasPic is not defined
                Parent: {}
            });
        }

In addition please have in mind that the dataSource does not support hierarchical objects, that is why the Parent.Name field is not shown.

I have edited the jsFiddle example, please check it and let me know if I misunderstood you.

Greetings,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
TrentCioran
Top achievements
Rank 1
answered on 28 Mar 2012, 03:42 PM
Thanks Alexander,

After some investigation I realized that you need to set all fields defined in the schema when adding a new record. For the embedded object I use the workaraound proposed in tis posts http://www.kendoui.com/forums/framework/data-source/working-with-complex-json-objects.aspx but also used to add a new field during parsing ParentName that does what the template was supposed to do. Not as clean as I wish it could be but it does the work http://jsfiddle.net/cDFT8/3/

Regards,
0
Jesper
Top achievements
Rank 1
answered on 25 Jun 2012, 10:56 AM
This makes no sense. Omitting a column should lead to it being null if the field is nullable, else it should be set to a default value.
0
Devon
Top achievements
Rank 1
answered on 08 Aug 2012, 12:33 PM
I need to add objects to a datasource dynamically and I am having a similar issue I think.

Here's my code:

var packageDataSource = new kendo.data.DataSource({
                schema: {
                    model: {
                        id: "id",
                        fields: {
                            Name: { type: "string" },
                            Status: { type: "string" }
                        }
                    }
                }
            });
 
            for (var a = 0; a < packages.length; a++) {
                packageDataSource.add([{
                    ID: packages[a].ID,
                    Name: packages[a].Name,
                    Status: packages[a].Status
                }]);
            }

It is adding objects to the datasource but they seem to be one "level" too deep.

For example, when I try apply the datasource to a list using the following template I'm getting a Uncaught ReferenceError: Name is not defined error.

Template:

<script type="text/x-kendo-template" id="packagesTemplate">       
        <h3 class="item-title">${Name}</h3>
        <p class="item-info">Status: ${Status}</p>       
        <p>
            <select>
            <option value="Pending">Pending</option>
            <option value="Delivered">Delivered</option>
            <option value="Damaged">Damaged</option>
            <option value="Refused">Refused</option>
            </select>
        </p>       
    </script>

Here's part of the datasource log from Chrome's javascript console. Can anyone help me with this??

o.extend.init
  1. _aggregateundefined
  2. _aggregateResultObject
  3. _datao.extend.init[2]
    1. 0B.extend.f
      1. 0o.extend.init
        1. ID1
        2. Name"Package 1"
        3. Status"Pending"
        4. _eventsObject
        5. parentfunction (){return b}
        6. uid"da0754f0-6981-4025-bc43-a41dd4d666d1"
        7. __proto__B.extend.b
      2. _eventsObject
      3. dirtyfalse
      4. id""
      5. parentfunction (){return c}
      6. uid"70d7b698-91ed-49db-b1eb-a47238152127"
      7. __proto__B.extend.b
    1. 1B.extend.f
      1. 0o.extend.init
        1. ID2
        2. Name"Package 2"
        3. Status"Pending"
        4. _eventsObject
        5. parentfunction (){return b}
        6. uid"bbd8d94b-9333-41ef-8adf-748c56b01004"
        7. __proto__B.extend.b
      2. _eventsObject
      3. dirtyfalse
      4. id""
      5. parentfunction (){return c}
      6. uid"bc6af97e-922c-4168-af34-146cdf645aa2"
      7. __proto__B.extend.b
    2. _eventsObject
    3. length2
    4. parentfunction (){return b._parent()}
    5. typefunction (){e.apply(this,arguments)}
    6. __proto__B.extend.b
  1. _destroyedArray[0]
  2. _eventsObject
  3. _filterundefined
  4. _groupArray[0]
  5. _mapObject
  6. _pageundefined
  7. _pageSizeundefined
  8. _prefetchObject
  9. _pristineArray[0]
  10. _rangesArray[0]
  11. _skipundefined
  12. _sortundefined
  13. _takeundefined
  14. _total2
  15. _viewo.extend.init[2]
  16. datafunction (a){var c=this;if(a!==b)c._data=this._observe(a),c._total=c._data.length,c._process(c._data);else return c._data}
  17. optionsObject
  18. readerp.extend.init
  19. transportp.extend.init
  20. __proto__B.extend.b


0
Jesper
Top achievements
Rank 1
answered on 08 Aug 2012, 12:45 PM
The dataSource.add()-method does not expect an array as argument. You should pass it an object.
0
Devon
Top achievements
Rank 1
answered on 08 Aug 2012, 12:52 PM
Ah thanks man!! 

All is sorted now!!!
Tags
Data Source
Asked by
Worawat
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Worawat
Top achievements
Rank 1
TrentCioran
Top achievements
Rank 1
Jesper
Top achievements
Rank 1
Devon
Top achievements
Rank 1
Share this question
or