Welcome Guest, Not a member yet? Register   Sign In
[JS Help]refresh a div
#11

[eluser]minoh[/eluser]
[quote author="pistolPete" date="1256344046"]
How much do the tables differ? Maybe you could use one function for all, but that depends on your content. Please post some example data and use cases.[/quote]

this a piece of the 'add' method :

Code:
// generate fournitures table data
        $this->load->library('table');
        $this->table->set_empty(" ");
        $this->table->set_heading('No', 'fourniture','Quantité', 'Prix');
        $i = 0 + $offset;

        if(isset($_SESSION['fournitures']))
        {
            $f_sess = $_SESSION['fournitures'];

            foreach ($f_sess as $fourniture)
            {
                $this->table->add_row(++$i, $fourniture['fourniture'],$fourniture['qt'], $fourniture['px_u'].' '.'€');
            }
        }
        $data['fournitures_table'] = $this->table->generate();
        $this->table->clear();

        // generate autres fournitures table data
        $this->load->library('table');
        $this->table->set_empty(" ");
        $this->table->set_heading('No', ' autre fourniture','Quantité', 'Prix');
        $af = 0 + $offset;

        if(isset($_SESSION['autres_fournitures']))
        {
            $af_sess = $_SESSION['autres_fournitures'];

            foreach ($af_sess as $autre_fourniture)
            {
                $this->table->add_row(++$af, $autre_fourniture['autre_fourniture'],$autre_fourniture['qt_autre_fourniture'], $autre_fourniture['ct_autre_fourniture'].' '.'€');
            }
        }
        $data['autres_fournitures_table'] = $this->table->generate();

        $data['title']                      = 'Ajouter une nouvelle intervention';
        $data['header']                     = 'vAdminHeader';
        $data['body']                       = 'interventions/vInterventionsEdit';
        $data['action']                     = site_url('interventions/General/ajoutIntervention');
        $data['link_back']                  = anchor('interventions/General/index/','Retour',array('class'=>'back'));
        $data['link_fourniture_add_popup']  = anchor_popup('interventions/Fournitures/index/','Ajouter une fourniture',$atts_popup_link);
        $data['link_a_fourniture_add_popup']= anchor_popup('interventions/AutresFournitures/index/','Ajouter une autre fourniture',$atts_popup_link);
        $data['general_title']              = 'Informations generales';
        $data['fournitures_title']          = 'Fournitures';
        $data['autres_fournitures_title']   = 'Autres fournitures';

        $this->_set_fields();
        $this->_set_rules();

        // run validation
        if ($this->validation->run() == FALSE)
        {
            $data['message'] = '';
        }
        else
        {
            $ct_total_fournitures = $ct_fournitures + $ct_autres_fournitures;

            $d_heure    = $this->input->post('d_heure');
            $d_minute   = $this->input->post('d_minute');
            $d = $d_heure + $d_minute/60;

            $nb_agents  = $this->input->post('nb_agents');

            $ct_mo = $d * $nb_agents * 23;

            $ct_total = $ct_mo + $ct_total_fournitures;
            
            // save data
            $intervention = array('ID_LIEU'                 => $this->input->post('lieu'),
                                'ID_TACHE'                  => $this->input->post('tache'),
                                'ID_UTILISATEUR'            => $this->session->userdata('userid'),
                                'ID_TYPE_DEMANDEUR'         => $this->input->post('type_demandeur'),
                                'DATE_INTERVENTION'         => $this->input->post('date'),
                                'NB_PERSONNES'              => $nb_agents,
                                'DUREE'                     => $d_heure.':'.$d_minute,
                                'CT_MO'                     => $ct_mo,
                                'CT_FOURNITURES'            => $ct_total_fournitures,
                                'OBSERVATION_INTERVENTION'  => $this->input->post('observation'));

            $id_intervention = $this->mInterventions->save_intervention($intervention);

            $this->validation->id = $id_intervention;

            if(isset($_SESSION['fournitures']))
            {
                foreach($f_sess as $fourniture)
                {
                    $ihf = array('ID_INTERVENTION'  => $id_intervention,
                                'ID_FOURNITURE'     => $fourniture['id'],
                                'QT_FOURNITURE'     => $fourniture['qt'],
                                'CT_FOURNITURE'     => $fourniture['qt']*$fourniture['px_u']);

                    $this->mInterventions->save_fourniture($ihf);
                }
            }

            if(isset($_SESSION['autres_fournitures']))
            {
                foreach($af_sess as $autre_fourniture)
                {
                    $ihaf = array('ID_INTERVENTION'     => $id_intervention,
                                'AUTRE_FOURNITURE'      => $autre_fourniture['autre_fourniture'],
                                'QT_AUTRE_FOURNITURE'   => $autre_fourniture['qt_autre_fourniture'],
                                'CT_AUTRE_FOURNITURE'   => $autre_fourniture['ct_autre_fourniture']);

                    $this->mInterventions->save_autre_fourniture($ihaf);
                }
            }

            $data['ct_fournitures'] = 0;
            $data['ct_autres_fournitures'] = 0;
            $data['ct_total_fournitures'] = 0;

            $data['message'] = '<div class="success">Intervention cr&eacute;&eacute;avec succ&eacute;</div>';
        }
        $this->load->view('vDashboard', $data);

should i put table generate in other method and call it in 'add' method ?
#12

[eluser]garymardell[/eluser]
How i would do it: (might not be the best way, never generated tables)

I would have my controller that gets all the data for that page, both the full page and the table obviously.

Then i would have a view that is designed to render the table. And a view of two to render the whole page and include the table view.

With the javascript i would send a variable back to the controller via post or get called something like is_ajax = true.

In the controller if the post/get variable is set to true then i would just return the table view created before and if it was not set then return the whole page. (There other ways to check if the request came from ajax but i cannot remember off the top of my head).


This way it means you have one controller and if you lay it out well enough by putting the getting the table information near the top as it will be required either way it will be faster.

Code:
// get table data as it is required whether ajax or not
// this example is purely for demonstration of logic not actual use.
if($this->input->post('is_ajax')) {
    $this->load->view("table_view", $data);

}
else {
    // get additional data

    // load the entire site and use the table view to avoid repetition.
    
    $this->load->view("header", $data);
    $this->load->view("table_view", $data);
    $this->load->view("footer", $data);

}
#13

[eluser]minoh[/eluser]
But how can i refresh a div by using different views and not functions ?

by reading jquery docs to refresh a page part we need to use what pistolPete suggest

Code:
$('#myDiv').load('/myFunction')[\code]

[b]now i put the tables generate in a separate function:[/b]

[code]    function tables()
    {

        // offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);

        // generate fournitures table data
        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $this->table->set_heading('No', 'fourniture,'Quantit&eacute;', 'Prix');
        $i = 0 + $offset;

        if(isset($_SESSION['fournitures']))
        {
            $f_sess = $_SESSION['fournitures'];

            foreach ($f_sess as $fourniture)
            {
                $this->table->add_row(++$i, $fourniture['fourniture'],$fourniture['qt'], $fourniture['px_u'].' '.'€');
            }
        }
        $data['fournitures_table'] = $this->table->generate();
        $this->table->clear();

        // generate autres fournitures table data
        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $this->table->set_heading('No', ' autre fourniture','Quantit&eacute;', 'Prix');
        $af = 0 + $offset;

        if(isset($_SESSION['autres_fournitures']))
        {
            $af_sess = $_SESSION['autres_fournitures'];

            foreach ($af_sess as $autre_fourniture)
            {
                $this->table->add_row(++$af, $autre_fourniture['autre_fourniture'],$autre_fourniture['qt_autre_fourniture'], $autre_fourniture['ct_autre_fourniture'].' '.'€');
            }
        }
        $data['autres_fournitures_table'] = $this->table->generate();

        return $data;
    }

And i call it in add function

Code:
$res = $this->tables();

        $data['fournitures_table'] = $res['fournitures_table'];
        $data['autres_fournitures_table'] = &res;['autres_fournitures_table']

refresh function in the popup :

Code:
if(document.popupFournituresEdit.submit)
    {
        window.opener.$("#fournituresDiv").load("../General/tables");
    }
But when i add an item to the table using the popup and submit, 'fournitures_table' disapears
#14

[eluser]minoh[/eluser]
Any help ?




Theme © iAndrew 2016 - Forum software by © MyBB