Author Topic: Switch tables from database  (Read 4255 times)

benni

  • Newbie
  • *
  • Posts: 3
    • View Profile
Switch tables from database
« on: February 14, 2014, 04:45:12 pm »
Hi,
I'm new so maybe I'm doing something terrible wrong :=)
But the problem is that the grid shows the data from the previusly loaded data.
Flow: Selects a database, fetches data from that db's table. Generate html table. The table shows the right data, but the converted grid is wrong.
Please se code below.  The load fetches a generated table.

Code: [Select]
$("#databases").dblclick(function() {
var input = $("#databases").val();;
if(input != db)
db =  $("#databases").val();
else
   return;
   
var dbstring = 'fieldmappings.php?dbname=' + db;
    $('#fortune').load(dbstring);

tbl = $("#fortune");
        obj = $.paramquery.tableToArray(tbl);
if(newObj == null)
{
newObj = { width: 1000, height: 800, title: "ONREGFieldMappings " + db, resizable: true };
        newObj.dataModel = { data: obj.data, rPP: 50, paging: "local" };
        newObj.colModel = obj.colModel;
        $("#grid_table").pqGrid(newObj);
$("#grid_table").pqGrid("option", "scrollModel", { horizontal: $(this).is(":checked") });
}
else {

$("#grid_table").pqGrid( "destroy" );
newObj = { width: 1000, height: 800, title: "ONREGFieldMappings " + db, resizable: true };
        newObj.dataModel = { data: obj.data, rPP: 50, paging: "local" };
        newObj.colModel = obj.colModel;
        $("#grid_table").pqGrid(newObj);
$("#grid_table").pqGrid("option", "scrollModel", { horizontal: $(this).is(":checked") });
}
« Last Edit: February 15, 2014, 10:06:30 am by paramquery »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Wrong data displayd
« Reply #1 on: February 14, 2014, 05:55:59 pm »
I couldn't reproduce your issue as I'm unable to trace global variables used in your function.

But I've created a similar fiddle for your reference.

http://jsfiddle.net/LAgZx/222/


benni

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Wrong data displayd
« Reply #2 on: February 14, 2014, 06:15:25 pm »
UPDATE: Made it work, but not sexy, any clever guys who knows a solution? If I placed some of the code into an other click event things works, but I would be sad to have to use an extra button just for this ;=)

Se below, and more below for orig post.

Code: [Select]
$("#databases").dblclick(function() {

var input = $("#databases").val();;
if(input != db)
db =  $("#databases").val();
else {
   alert('returned');
   return;  }
alert(db);
dbstring = 'fieldmappings.php?dbname=' + db;
//alert(dbstring);
    $('#fortune').load(dbstring);
});

$("#btnCheck1").click(function() {
tbl = $("#fortune");
var obj = $.paramquery.tableToArray(tbl);
    var newobj = {dataModel: {data:obj.data}, colModel: obj.colModel};
    if($("#grid_table").hasClass("pq-grid")){
        $("#grid_table").pqGrid("destroy");
    }
    $("#grid_table").pqGrid(newobj);
});

Thank you, i'll tried and took a look.
The difference on the example and mine is that the old table is supposed to be replaced by a new, but I'll try.
I'v posted the full, updated, code again. Yours work the same way, the table i view is updated, but the grid is still showing the old data - always one 'refresh' behind.
Will it help to change the name of the table each time you think ?

Code: [Select]
<script>
var dbstring = 'fieldmappings.php?dbname=' + db;
//alert(dbstring);
    $('#fortune').load(dbstring);


tbl = $("#fortune");
var obj = $.paramquery.tableToArray(tbl);
    var newobj = {dataModel: {data:obj.data}, colModel: obj.colModel};
    if($("#grid_table").hasClass("pq-grid")){
        $("#grid_table").pqGrid("destroy");
    }
    $("#grid_table").pqGrid(newobj);

</script>
« Last Edit: February 14, 2014, 07:16:23 pm by benni »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Wrong data displayd
« Reply #3 on: February 14, 2014, 07:57:39 pm »
It's one table behind because you invoke load method which makes async server request while code execution proceeds further and loads prev table in the grid.

You need to use $.ajax method and create grid in success callback of ajax method, I hope you get the idea of what you are doing wrong.

https://api.jquery.com/jQuery.ajax/
« Last Edit: February 15, 2014, 10:08:16 am by paramquery »

benni

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Switch tables from database
« Reply #4 on: February 18, 2014, 05:50:07 pm »
Thank you very much, works like a beuty :)
 ;)