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

Call js when call CREATE method

10 Answers 701 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gusev
Top achievements
Rank 1
Gusev asked on 04 Apr 2013, 01:26 PM
Hello!
I want create KEY(first object in my model), before call method Create!
I dont want create KEY, If I edit object, so I can't  write this:
Events(e => e.Edit("getKey()"))

So,probably,I need this:
.ToolBar(commands =>
        {
            commands.Create().HtmlAttributes(new { onclick = "getKey()" });
        })
How I can this do?
Because it's doesn't work :)

Please help!
And thanks for help)

10 Answers, 1 is accepted

Sort by
0
Dimiter Madjarov
Telerik team
answered on 04 Apr 2013, 02:36 PM
Hello Grigoriy,


To achieve this, you could bind to the edit event of the Grid and check if the current operation is Create with the isNew() method of the model.
E.g.
.Events(e => e.Edit("edit"))

function edit(e) {
  if (e.model.isNew()) {
      //custom logic
  }
}

 

Greetings,
Dimiter Madjarov
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Gusev
Top achievements
Rank 1
answered on 05 Apr 2013, 10:56 AM
Hello!
Thanks for answer!

Can I ask you?
I need create Key = GUID when I click button "Add new object"
I use this
.Event(e => e.....)
in Grid
&
js:
function edit(e) {
        if (e.model.isNew()) {
            e.model.Key = guid();
        }
    }
Wood is my Model in Grid,which have field -  Key
and Next : (in popUp subGrid(Model WoodPeeling - ) I use this:
.Read("Select", "WoodPeeling", new { IdParent = "${Id}", ParentKey = "${Key}" })
and here I need a KEY,which I create in method  edit(e) : --------    here need : ParentKey = "${Key}"
but Key is ""  
what did I do wrong?

In my opinion I have to call to "edit" event during initialization, but I do not know how to do it
0
Dimiter Madjarov
Telerik team
answered on 09 Apr 2013, 08:52 AM
Hello Grigoriy,


I tried to reproduce the issue, that you are mentioning, but to no avail. Could you please check if there are any JavaScript errors in the console? If you want to use the Kendo UI guid function in the current scenario, you should call kendo.guid() in the edit event handler. If you are still experiencing any issues, please send me a runnable sample project, so I could inspect it locally and assist you further.

I am looking forward to hearing from you.

 

Regards,
Dimiter Madjarov
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Gusev
Top achievements
Rank 1
answered on 10 Apr 2013, 10:57 AM
Hello!
I attach KendoProject with My problem inside!
But I delete all script,because  was need 2 mb :)

next I attach five screenshots:
console -> my new object (then I click button "Add new object") has field: ShipCountry = "GOTOCONTROLLER" (I set this value in js method , then call event edit)
index_event -> I subscribe to an event
index_isnew -> I change value ShipCountry = "GOTOCONTROLLER" 
index_read_with_new_country -> It's my method read with new string Key = #= ShipCountry #
visual_studio -> but my string Key  = " bla bla bla" , not "GOTOCONTROLLER"

Please Help!
0
Dimiter Madjarov
Telerik team
answered on 12 Apr 2013, 09:33 AM
Hi Grigoriy,


To get the ShipCountry value in the Create Action, you could pass it to the template method when you are rendering it, just like the OrderID property.
E.g.
function edit(e) {
    if (e.model.isNew()) {
        e.model.ShipCountry = "GOTOCONTROLLER";
    }
    e.container.find("#popupGridWrapper").html(template({ OrderID: e.model.id, ShipCountry: e.model.ShipCountry }));
}

Wish you a great day!

 

Regards,
Dimiter Madjarov
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Gusev
Top achievements
Rank 1
answered on 15 Apr 2013, 11:30 AM
Hello!
Thanks for your answer! 
And I have second problem(last),similar to the previous :)
1) Grid:
Model Wood has fields: Id , Name, Key
@(Html.Kendo().Grid<Wood>()
        .Name("Wood")
        .Events(e => e.Edit("edit"))
        //Here other
        .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("Wood").Window(w => w.Title("Edit")))
        //Here other
)
2) Event edit:
function edit(e) {
        if (e.model.isNew()) {
            e.model.Key = guid();
        }
    }
3) EditorTemplates: (Wood)
@model Wood
<h2>Wood Edit</h2>
@Html.HiddenFor(o => o.Id)
@Html.LabelFor(o => o.Name)
@Html.EditorFor(o => o.Name)
   
@(Html.Kendo().Grid<WoodPeeling>()
        .Name("WoodPeeling")
        // Other
          .DataSource(....
          // Ajax and i.e. Read,Update and i.e
                .Create("Create", "WoodPeeling", new {ParentKey = "${Key}" })
          //
        )
       //Other
        .ToClientTemplate()
)
And I want Create object in WoodPeeling with ${Key}  from Wood (Key - which created , when we call method edit in Wood)

How I can this do?
0
Dimiter Madjarov
Telerik team
answered on 17 Apr 2013, 10:01 AM
Hi Grigoriy,


In the current scenario, you could pass the generated guid key as a data to the template, when you are rendering it, as demonstrated in the previous post.

 

Greetings,
Dimiter Madjarov
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Gusev
Top achievements
Rank 1
answered on 17 Apr 2013, 12:40 PM
I don't understand, how I can this do!
Because my WoodPeeling.html() does not have template (only its)
e.container.find("#WoodPeeling").html(//here:
 I need replace html()?

Or 
e.container.find("#WoodPeeling").getKendoGrid().dataSource //here
push {Id : ....} ?
0
Accepted
Dimiter Madjarov
Telerik team
answered on 19 Apr 2013, 08:34 AM
Hi Grigoriy,


I am not sure that I understand the exact case. If the approach from the previous posts does not apply, you could use the Data method of the Grid's DataSource Action Builder, which defines a JavaScript function, that will return additional data for the Action. Please check the documentation on this topic.
E.g.
DataSource(source => source.Ajax()
    ...
    .Create(create => create.Action("Create_Order", "Orders").Data("additionalData"))

function additionalData() {
    return {
        parentKey: guid()
    };
}

If this approach does not work in the current case, please send me the modified solution, so I could assist you further.

 

All the best,
Dimiter Madjarov
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Gusev
Top achievements
Rank 1
answered on 19 Apr 2013, 11:34 AM
Hello!
Thanks for your help!
all good works!
If I will find error, I will write)
thanks!)
Tags
Grid
Asked by
Gusev
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
Gusev
Top achievements
Rank 1
Share this question
or