Welcome Guest, Not a member yet? Register   Sign In
Jqgrid and refreshing page
#1

[eluser]chicca[/eluser]
Hi.
I have a page with some text boxes and a jqgrid which i realized following this thread http://ellislab.com/forums/viewthread/157452/
The grid works perfectly but when i add/edit/delete a row, i need to update another table in db with sums of some fields in the table linked to the grid and then i have to write results on some text boxes in the page, so i need to refresh the page to see the new values on these boxes.
I put all these operation in setData function

Code:
public function setData()
{
  $idcard = $this->session->userdata('card');
  $this->load->library('datagrid');
  $grid     = $this->datagrid;
  $response = $grid->operations('shoppinglist','idShoppingList');
  $this->Cards_model->update_total_costs($idcard);
  redirect('this_page/'.$idcard, 'refresh');
}

where 'this_page' is the page with boxes and grid.
I can see that model function is executed correctly but the redirect instruction is totally ignored: infact if i check db, i see the right values but if i write 'blablabla' instead of 'this_page' i dont get any errors.
The only way to see updated text boxes is pressing f5.
Why is redirect ignored and what can i do refresh page w/o using f5?
Thanks in advance.
#2

[eluser]jwindhorst[/eluser]
Perhaps you're taking a difficult approach to a relative simple problem. You are trying to deal with redirects in order to update an element of the page. That is, assuming I interpreted the problem correctly.

Assuming you are using jquery somewhere, why not put the grid in it's own file and use jquery to lazy load it via Ajax. Then you already have the code:
Code:
// opening javascript tag here //

$(document).ready(function(){

    $('#grid-wrapper').post("my_grid.php", function(data) {
        alert("Data Loaded: " + data);
        // NOW SET IT INTO THE HTML FUNCTION OF YOUR WRAPPER ELEMENT.
        // THIS ESSENTIALLY "REFRESHES" THAT ELEMENT FOR YOU.
        $('#grid-wrapper').html(data);
    };

})

// closing javascript tag here //

NOTE: You should include your CSS files before your Javascript files. If you have slow loading libraries in your JS there are other ways to handle that, but at least your styles will be loaded and your page can appear proper even if a slow loading library is slowing execution.


NOTE: It may rub some truists in the MVC communities, I still find it much easier to create the HTML in the Ajax controller. I've often used an Ajax_controller.php to routes ajax calls, in tandem with a views/ajax/view_name.php to manage these calls. These ajax view files would never include a views/header.php file or an <html> tag, it is merely a section of a page expected to be loaded into a full html document.

I definitely find it easy to group these together in a controller called lazy_partials, or lpartials is advantages. It allows you to set specific security restrictions on these requests. Just to keep things clean I then like to create a views/lpartials directory to hold these lazy loaded elements. Snippets is likely a more PC term but I learned it as lazy loading so that's what I call it.

#3

[eluser]chicca[/eluser]
thanks for answer and sorry for my english
I dont doubt i'm doing something more difficult than it is, i always do so in my life Smile
but now either i dont understand what you suggest or you are doing it simpler than it is Smile
I try to explain better: i have some controls in a page and in these, there are 3 text boxes (T1, T2, T3) which show 3 fields A1, A2 and A3 of a db table A. In the same page i have a jqgrid which is linked to a db table B. When a user changes (add/edit/delete) a row in jqgrid, the datagrid library and the controller function make the same changes on db table B, and it works w/o any problem. Done this, i have to

- execute a model function to get all data in jqgrid and calculate sum(B1*B2), sum(B3*B4) and the sum of these results <--- it works
- save with a model function the 3 sums in A1, A2 and A3 <--- it works
- write the new values of A1, A2 and A3 in T1, T2 and T3 and show them to the user <--- it doesnt work.

The user can see the updated values only pressing f5.
I thought it could be a cache problem so i add set_header function with no cache and so on, but i got the same situation even if i saw the set_header was executed.
I tryed to add a sleep between the model function and the redirect but no lucky again.
Then i tried to write a nosense string as first parameter of redirect function so i expected an error message like "page not found" or something like it but i didnt get any error.
So i think, and i dont know why, the redirect call is totally ignored.
Now i have 2 problems: the most urgent is to show updated values in T1, T2 and T3 after the updating of db table A by model function, and the second one is to undestand why redirect call is ignored.
Thanks in advance for any help you can give me.
#4

[eluser]jwindhorst[/eluser]
Right, let me see if I can explain my thought a little better.

Let your page load (initial load) without your text boxes. Then send a "lazyload" Ajax request out to a controller called Ajax_controller. Inside that controller, make the calls to your model to get and manipulate your data. Pass that data back to your Ajax_controller, and use that controller pull in your "text_fields.php view". You can call it whatever you want, but it's a view that does nothing but display those three text boxes with their proper/current values.

If you run this when the page is first loaded it might look something like:
Code:
[removed]
$(document).ready(function(){
    $.post('Ajax_controller/get_text_fields', function(data) {
        $('#text_fields_wrapper').html(data);
    });
});
[removed]

What that does is say, ok, our page is loaded, now I'm going to grab the HTML to create the text boxes, and I'm going to dump it into my html div element that has an id of text_fields_wrapper.

You could alter that script just slightly to make it work against any javascript event. For example, the user changes a value and clicks the 'Send Values' button:
Code:
[removed]
$(document).ready(function(){
    $('#send_values').click(function(){

        $.post('Ajax_controller/get_text_fields', function(data) {
            $('#text_fields_wrapper').html(data);
        });

    });

});
[removed]

The only difference here is that you are not re loading that element until you click the button. Depending on how it needs to work for the user, you could bind to the change event or anything else also.

This sends an ajax request to your Ajax_controller. Inside there you can do any of the processing you normally would; call a model, manipulate your data, whatever you need to do. Once you load that view at the end of the controller call, the request will come back to the browser and repopulate the HTML inside of your #text_field_wrapper div, which will naturally be the same HTML as before, with new values.
#5

[eluser]chicca[/eluser]
Thanks for answer
I'll try to do as you suggest




Theme © iAndrew 2016 - Forum software by © MyBB