How to reload grid without checking if it exists JS Jquery

Asked

Viewed 300 times

0

The system was built with modals and it is possible that there is a modal there in the 2nd level that needs to update two grids below (one in the main screen and the other in the modal in the 1st level)but not always this modal is called from the same place so when completing the action it is possible that these two grids do not exist below it, just every time we will use grid.fnDraw() in JS we need to check first if the grid in question exists with the if(typeof(grid) != "undefined".

I wanted an option so I don’t have to check every time I reload a grid if it exists.

I thought of something like creating a method like this:

function recarregarGrid(grid) {
   if (typeof (grid) != "undefined")
       grid.fnDraw();
}

inserir a descrição da imagem aqui

There are cases where we reload several grids that may not exist at that time:

//Concluíu uma ação e precisa atualizar os dados das grids abaixo da modal
if(typeof(gridItem) != "undefined")
  gridItem.fnDraw();
if(typeof(gridFornecedor) != "undefined")
  gridFornecedor.fnDraw();
if(typeof(gridLocalEntrega) != "undefined")
  gridLocalEntrega.fnDraw();
if(typeof(gridStackOverflow) != "undefined")
  gridStackOverflow.fnDraw();

But anyway I can’t pass a Undefined variable per parameter. So I need a way to pass the grid as a parameter even if it’s like "undefined" or a better solution to no longer need to use this every time if before reloading the grid.

  • 1

    how about putting the if within the function fnDraw?

  • I can’t use it. fnDraw on a Undefined variable and because of that it bursts error, so no use I put inside fnDraw because it doesn’t even get there

  • 1

    grid would be related to an element of the FOD?

  • The grid structure is in HTML, but the grid is mounted in JS and loaded in HTML.

2 answers

3

Edited after the comment: You can initialize grids at the beginning of the script as a global object that will gather all the grids that exist. Whenever you set a grid, create a property inside that object to store. So when you call recarregarGrids() simply iterate over the existing grids there and call the function.

// Guarda um objeto para todos os grids
var grids = grids || {};

// Define a função de forma genérica, que vai iterar sobre os grids
// adicionados ao objeto
recarregaGrids = function() {
  for( var grid in grids ) {
    if ( grids.hasOwnProperty(grid) ) {  
        console.log(grids[grid]); // apenas para testar o valor de cada grid
        // grids[grid].fnDraw();
    }
  }
}

// Quando estiver definindo as grids, guarde todas elas dentro do objeto master
grids.grid1 = 'gridStackOverflow';
grids.grid2 = 'gridPadrao';
//etc

recarregaGrids();

  • I set there, I have several cases where I need to update several grids IF they exist. So I don’t see how I could apply this solution.

  • Like, it’s not that the grid was initialized with no value, but that it was never initialized because it doesn’t exist there

  • 1

    but you will recharge all together or one at a time?

  • All that exist at the same time. I updated the post there

  • 1

    edited with another method.

  • 1

    But you’re checking a property, I think what he’s trying to do is check if it exists in the gift.

  • 1

    You can’t tell by the code he put in. I’m assuming that he controls the creation of grids, so they have different names, including.

Show 2 more comments

1


if you are running from the browser, you can use the global 'window' object to see if a certain global variable exists

function recarregarGrid(nome) {
    window[nome] && window[nome].fnDraw();
}

recarregarGrid('exemplo')

Note that in the above example, 'example' is a string containing the variable name.

Or, if you don’t want to pollute the global scope, you can use a single global variable to represent all grids, but in that case, you need to remember to always log each grid when it first loads:

gridsExistentes = {};

function recarregarGrid(nome) {
    gridsExistentes[nome] && gridsExistentes[nome].fnDraw();
}

function registrarGrid(nome, grid) {
    gridsExistentes[nome] = grid;
}

recarregarGrid('exemplo')

// (...)

registrarGrid('exemplo', gridPreviamenteCarregado);
recarregarGrid('exemplo')
  • I think the first example is the best case. It works perfectly, but it’s kind of weird to pass a string with the name of the grid...

  • the team here also did not like to pass the name as string ): although it is still better to do an if every time before using fndraw. I will search if there is an even better option

Browser other questions tagged

You are not signed in. Login or sign up in order to post.