0

I am trying to use editablegrid with multiple tables. For that, I have a select box with all database tables and onchange I want it to pass the tablename to a prototype function, in order to render the new table. The variable _tableNames is always undefined inside the prototype function. How can I pass that variable?

var _tableNames;

/* passing tablename change */
$(document).ready(function() {
    // Monitor your selects for change by classname
    $('#dbtables').on('change', function() { 
         // Save the place increment and value of the select
         _tableNames = $(this).val();
         console.log(_tableNames);
         datagrid = new DatabaseGrid();
    });
});

// Select table to edit
DatabaseGrid.prototype.fetchGrid = function(_tableNames)  {
    // call a PHP script to get the data
    console.log(_tableNames);
    if(typeof _tableNames === "undefined"){
        alert(_tableNames);
        _tableNames='demo';   
    }   
    url="loaddata.php?db_tablename="+_tableNames;
    this.editableGrid.loadJSON(url);    
};
  • 3
    How and when are you calling the said function? Or is it unclear to you, that arguments are getting their values when invoking a function, not in the argument list when defining a function ..? – Teemu Sep 14 '17 at 16:21
  • well does the onchange execute? when does fetchGrid run? My guess is you are not calling fetchGrid with an argument, but that is impossible to tell since you do not show it. – epascarello Sep 14 '17 at 16:21
  • 1
    It looks like you expect `_tableNames` in the proto function to have the value which you've set to it in the change handler. However, the change handler sets the value of the global `_tableNames`, in the proto function there's the same argument name, which makes `_tableNames` local to the function, i.e. the local variable shadows the global-one. Removing the argument from the list would fix the problem. – Teemu Sep 14 '17 at 16:31
  • the onchange executes and console.log(_tableNames) returns the expected value. This question might lean more toward editablegrid framework. – Cracktastic Sep 14 '17 at 16:32
  • This was indeed a problem of a local variable shadowing the global-one. Can I accept Teemu's aswer as the correct one? – Cracktastic Sep 14 '17 at 16:40
  • The "answer" is in an speculative comment only. This has to be a dup, hard to find, though, since there are so many different descriptions and titles in the posts concerning this problem. In the future, please spend some time at your question, ready to response quicker than on this post, that would be my "green tick". – Teemu Sep 14 '17 at 16:50

0 Answers0