Editor.dependent infinite loop
Editor.dependent infinite loop

I have 4 dependent on my datatable, when I open Editor it works, but its never stopping, which makes it not able to save the information.
editor.dependent('ProdutoEdit', function () {
$.ajax({
url: "/PedidoVenda/GetProdutos/",
dataType: 'json',
success: function (retorno) {
editor.field('ProdutoEdit').update(retorno)
}
});
});
editor.dependent('CorEdit', function (val, data, callback) {
$.ajax({
url: "/PedidoVenda/GetCores?Produto=" + data.values.ProdutoEdit,
dataType: 'json',
success: function (retorno) {
editor.field('CorEdit').update(retorno)
}
});
});
editor.dependent('TamanhoEdit', function (val, data, callback) {
$.ajax({
url: "/PedidoVenda/GetTamanhos?Produto=" + data.values.ProdutoEdit,
dataType: 'json',
success: function (retorno) {
editor.field('TamanhoEdit').update(retorno)
}
});
});
editor.dependent('ValorUnEdit', function (val, data, callback) {
var Url = "/PedidoVenda/GetPreco?Produto=" + data.values.ProdutoEdit + "&Cor=" + data.values.CorEdit + "&Tamanho=" + data.values.TamanhoEdit;
$.ajax({
url: Url,
dataType: 'json',
success: function (retorno) {
editor.field('ValorUnEdit').val(retorno);
}
});
});
All of them have a valid return and it shows on Editor, but it keeps on looping
This question has accepted answers - jump to:
Answers
Well, you are making an ajax call when the value changes and the ajax call changes the value: That is an infinite loop by definition.
Why are you doing this? What is the business context?
I have
1. a list of products
2. a list of colors
3. a list of sizes
4. the price
to get colors and sizes I need product.
to get price i need everything.
Will make something so i does not update if the value is the same
i changed so it does not call the server. it keeps on loop anyways
editor.dependent('ProdutoEdit', function (val, data, callback) {
if (data.values.ProdutoEdit == null) {
$.ajax({
url: "/PedidoVenda/GetProdutos/",
dataType: 'json',
success: function (retorno) {
editor.field('ProdutoEdit').update(retorno)
}
});
}
});
Then you would need to post a test case as per the forum rules.
how to do it ?
Try replacing this
if (data.values.ProdutoEdit == null)
with
if ( editor.val("ProdutoEdit") <= '' )
because "data" might retain the original values read from the server - but not sure
I use dependent 204 times in my code - and I never had an infinite loop. I never use the "data" parameter - but I forgot why ...
https://datatables.net/forums/discussion/12899/post-test-cases-when-asking-for-help-please-read#latest
Are you aware that "upate()" doesn't update the field content but only the options of a select field?? Are your fields select fields?
https://editor.datatables.net/reference/api/field().update()
If you wanted to update the field content you would need to replace
editor.field('ProdutoEdit').update(retorno)
with
editor.set( { "ProdutoEdit": retorno } );
http://live.datatables.net/xujunata/2/edit
not sure why does not show the editor buttons on live
So with validations it only calls once, but visually it never ends to load
https://datatables.net/forums/uploads/editor/sw/lzwfhzwkfq37.png
editor.dependent('ProdutoEdit', function (val, data, callback) {
if (editor.val("ProdutoEdit") <= '') {
$.ajax({
url: "/PedidoVenda/GetProdutos/",
dataType: 'json',
success: function (retorno) {
editor.field('ProdutoEdit').update(retorno);
}
});
}
});
The browser's console has lots of errors. You can start with this basic Editor template:
http://live.datatables.net/guwafemu/1/edit
Look at your browser's console for errors.
Kevin
Why do you update the options of the field if the field is empty? I can't think of a business reason for that.
I was able to reproduce the problem.
http://live.datatables.net/guwafemu/130/
if you click on New you can see it loads but "keeps loading" and if you try to save it does nothing.
rf1234 to fill the empty one is not a problem, the problem are the other 3 fields. but the problem happens with any of them.
Sorry, I don't understand what you are trying to achieve in terms of business outcome. So I am unable to help you further.
Speaking about "business outcome" ... I saw this on LinkedIn today.
I don't remember how many ppt-slides I used to make "pretty and fast" ...
Great stuff and 100% correct
For the sake of completeness:
Partner Level: convince client boards that everyone else in the industry believes in the impact of pretty slides.
the bussiness porpuse is this:
I have a list of Produt, Color, Size imagine like 1k products;
Color and Size is linked with Product, the real point is when someone selects a product i will get the color and size for this product.
In the example it does not show this, but that whats really happening, in the example there is a error even with the simple example.
I must be forgetting something.
The whole point is to make this one work.
editor.dependent('CorEdit', function (val, data, callback) {
$.ajax({
url: "/PedidoVenda/GetCores?Produto=" + data.values.ProdutoEdit,
dataType: 'json',
success: function (retorno) {
editor.field('CorEdit').update(retorno)
}
});
});
Ok what you say is:
When someone selects a product you need to show the available options for the selected product in terms of color and size. Subsequently when product, color and size have been selected the price needs to be retrieved for that combination.
Sorry, I am a business person by profession, but coding is my passion.
I can't do color and size befeore they choose the product, but assuming i could, im still getting error
True, and I never said you could.
No, you shouldn't assume that because it makes no sense!
I haven't used
dependent()
much but I did mess with your test case. It seems thatdependent()
expects some sort of positive return value. See this example:http://live.datatables.net/nesoyune/1/edit
Made this updare:
With this either the combo of
field().update()
andreturn true
or simply returning the options in the in the format above seems to work. I could be way off base thoughKevin
I've done dependents more than two hundred times ...
This code should do the job if it doesn't have typos.
The code inside the last two dependents is identical. So you might move it into a function.
update: fixed some typos replaced [ ] with ( ). Now they should be ok ... hopefully.
As a simplification it should also work with an array of field names:
So the code could be shortened like this (haven't tried it myself yet):
This may still be something with the test case but I need to use
return true;
in the dependent function to for the edit modal to be happy. This example simulates an ajax response.http://live.datatables.net/woqebafo/1/edit
Commenting out the
return true;
statement causes the issue described by @Bynv .Kevin
Amazing; I never needed to return true in my over 200 use cases ...
But anyway if you can implement the logic above and you need to return true: So be that as it may ...