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

In Custom popup editor, Update button always trigger 'create' event

11 Answers 1321 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paddy
Top achievements
Rank 1
Paddy asked on 23 Nov 2015, 09:23 AM
The attached picture1 is what I want to achieve. In the grid there is a list for students and their favorite fruits. The favorite fruits list only for text, in database they are saved as comma seperated numbers such as 1,2,3, and in the data source schema, there is a column for it named fids. And for the fruits, there is a table in database, and an additional odata source for it.
 In the popup editor, I only want to change their favorites not others, and the popup editor is implemented as checkboxes list, just like attachment picture2. The checkboxes list is initialized in the edit event of the gird. In the grid edit event, I have the following code:


  edit: function (e) {
        $("#fruitLists").css({ 'overflow-y': 'scroll', 'width': '280px', 'height': '170px' });

        var checkboxes = '';
        var fruitArray = e.model.fids.split(",");
        var data = that.fruitDataSource.data();
        for (var i = 0; i < data.length; i++) { //generate checkboxes list
            var checked = fruitArray.indexOf(data[i].id + '') > -1 ? "checked" : "";
            checkboxes += "<input type=\"checkbox\" name=\"fruitIdent\" value=\"" + data[i].id + "\" " + checked + " />" + data[i].name + "<br />\n";
        }
        $("#fruitLists").html(checkboxes);

        e.container.find(".k-button.k-button-icontext.k-primary.k-grid-update").click(function () {
            var checkedItems = [];
            $("input[name='fruitIdent']:checked").each(function () { checkedItems.push($(this).val()); });
            e.model.fids = checkedItems.join();

            //update favorite fruits...

            $("#grid").data("kendoGrid").saveRow();           
        });        
    },

My template for popup editor:
<div class="k-edit-form-container" id="popEditor">
    <script id="popup_editor" type="text/x-kendo-template">
        <div id="fruitLists" style="margin-left:35px">

        </div>
</script>
</div>


And the schema for students list:
    model: {
        id: "id",
        fields: {
            id: { type: "number", nullable: false, editable: false },   //headmap
            name: { type: "string", nullable: true },
            favorits: { type: "string", nullable: true },
            fids: { type: "string", nullable: true },
        },
    },

The curious is that, each time I click the Update button, it always trigger data source - transport's create event, not update event. Why? I searched this forum, many guys says that it is because the schema has no id or the popup UI has no id, but even I added the id field, only create event is fired with a null model. 

Why my approach does not work? Is there any solution? Thansk.


11 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 25 Nov 2015, 08:14 AM
Hello Paddy,

The create transport of the DataSource will trigger when the saved record does not have an ID. I see that you have defined the ID in the schema configuration, but could you please confirm that each of the dataItems in the dataSource has an "id" field with unique value different from null or undefined?

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Paddy
Top achievements
Rank 1
answered on 25 Nov 2015, 08:33 AM
Hi Alexander, thank you for your reply. You say "please confirm that each of the dataItems in the dataSource has an "id" field with unique value different from null or undefined", what do you mean the dataitems? The data listed in the grid? I'm very sure that each row does contain a unique id.Or how can I find there are invalid IDs? In the edit event of grid, I can see the id of the current row to be edited and it is a valid one. What to check next? Is there a way to change the event? Or change the model before saving? I'm looking forward to your reply... Thanks!
0
Paddy
Top achievements
Rank 1
answered on 25 Nov 2015, 08:41 AM
Hi Alexander, if the row being edited does contain a valid id, then how could the id get lost while saving? Is it a reason that my popup editor does not contain it. You can see from the attachment that my popup does not need ids and other fields to be shown, what I need is only a checkbox list. Is that a reason causing the model to lose id? Can I preserve id in the popup without showing them? 
0
Paddy
Top achievements
Rank 1
answered on 25 Nov 2015, 08:58 AM

What I'm confused is that, even after I added the id field in the popup template:

        <div class="k-edit-label">
            <label for="id">id</label>
        </div>
        <div class="k-edit-field">
            <input type="text" class="k-input k-textbox" name="id" data-bind="value:id" />
        </div>

When I click the Update button and it only trigger create event. 

0
Alexander Valchev
Telerik team
answered on 27 Nov 2015, 09:32 AM
Hi Paddy,

If the create transport fires that means that according to the DataSource the record is considered as newly created. This will happen when the record does not have a unique ID.

According to your reply each record has unique ID. We can verify this by hooking up to the save event of the Grid and execute the following code:

editable: "popup",
save: function(e) {
      console.log(e.model.isNew());
}

What is the console.log output in your case?
Looking forward to your reply.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Paddy
Top achievements
Rank 1
answered on 30 Nov 2015, 12:59 AM

Hi Alexander,

The output for my case is false.  Is that weird?

 

0
Alexander Valchev
Telerik team
answered on 02 Dec 2015, 08:28 AM
Hello Paddy,

Yes this is unusual. Is it possible for you to provide a Kendo Dojo or sample project with mock data that isolates the issue? In this way I will be able to reproduce it locally and examine what exactly is going wrong.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Lorenzo
Top achievements
Rank 1
answered on 21 Apr 2016, 07:01 PM

Hi, I'm experiencing the exact same situation. Do you have any update on this ?

Lorenzo

0
Lorenzo
Top achievements
Rank 1
answered on 21 Apr 2016, 07:07 PM

Hi, after some try I kind of figured out a workaround. The problem in my case seems the fact that the ID i a number. If i replace the key with a dummy ToString() of my Int32 key it works.

 

0
Lorenzo
Top achievements
Rank 1
answered on 21 Apr 2016, 07:13 PM

UPDATE

I figured out that if the ID is a number (in my case lineNum) and it is defined both as ID and in the fields the strange behavior occurs:for each update also a create is triggered.

If the ID is a string putting it both in ID and in the fields doesn't make any difference.

Now lineNum is only the ID and it works.

Lorenzo

 

0
Alexander Valchev
Telerik team
answered on 26 Apr 2016, 07:37 AM
Hello Lorenzo,

Please provide a sample project where we can observe the behaviour. In this way we will be able to examine it in details and assist you further.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Paddy
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Paddy
Top achievements
Rank 1
Lorenzo
Top achievements
Rank 1
Share this question
or