javascript - Could someone clarify this code snippet for me?

292

This piece should create a csv file. The method that is calling to the nonAjaxPost is:

function exportCSV()
{
    nonAjaxPost('getExport', 'post', {action: '/getView', 'view': current_pi, 'parameters': encodeURIComponent(JSON.stringify(current_parameters))});
}

function nonAjaxPost(action, method, input) {
    "use strict";
    var form;
    form = $('<form />', {
        action: action,
        method: method,
        style: 'display: none;'
    });
    if (typeof input !== 'undefined') {
        $.each(input, function (name, value) {
            $('<input />', {
                type: 'hidden',
                name: name,
                value: value
            }).appendTo(form);
        });
    }
    form.appendTo('body').submit();
}

My problem is that i just can't seem to understand how this is going to create a csv file for me. I'm probaly missing out on something that i just can't see.

I really hope someone could help me out.

Update:

This is the getExport function:

$databundle = $this->_getData();
    $data = $databundle['rows'];
    $columns_all = $databundle['columns'];

    $columns = array("Id");
    foreach($data[0] as $key => $column) {
        $column = "";
        $found = false;
        foreach($columns_all as $col_search) {
            if($col_search['key'] == @$key) {
                $found = true;
                $column = $col_search['title'];
                break;
            }
        }
        if($found) {
            //echo $key . ",";
            $columns[] = $column;
        }
    }   

    $contents = putcsv($columns, ';', '"');

    foreach($data as $key => $vals) {
        if(isset($vals['expand'])) {
            unset($vals['expand']);
        }

        array_walk($vals, '__decode');
        $contents .= putcsv($vals,';', '"');
    }


    $response = Response::make($contents, 200);
    $response->header("Last-Modified",gmdate("D, d M Y H:i:s") . " GMT");
    $response->header("Content-type","text/x-csv");
    $response->header("Content-Disposition","attachment; filename=".str_replace(" ","_",$databundle['title'])."_".date("Y-m-d_H:i").".csv");        

    return $response;

It also calls the getData function which is this:

$viewClass = str_replace('/', '', (isset($_POST['view']) ? $_POST['view'] : $_GET['view']));

    $fileView = '../app/classes/view.'.$viewClass.'.php';
    if(file_exists($fileView))
    {
        require_once($fileView);
        $className = 'view_'.$viewClass;
        if(class_exists($className))
        {
            $view = new $className();

            //Seek for parameters
            if(isset($_REQUEST['parameters']))
            {
                //Decode parameters into array
                $parameters = json_decode(urldecode((isset($_POST['parameters']) ? $_POST['parameters'] : $_GET['parameters'])),true);

                //Get supported parameters
                $parameterTypes = $view->getVars();

                $vars = array();
                foreach($parameterTypes as $key => $type)
                {
                    //If a value is found for a supported parameter in $_GET
                    if(isset($parameters[$key]))
                    {
                        switch($type)
                        {
                            case 'int':
                                $vars[$key] = intval($parameters[$key]);
                                break;
                            case 'float':
                                $vars[$key] = floatval($parameters[$key]);
                                break;
                            case 'filterdata':
                                // todo: date validation
                                $vars[$key] = $parameters[$key];
                                break;
                        }
                    }
                }

                $view->setVars($vars);
            }

            return $view->getData();
        }
        else {
            /*
            header('HTTP/1.1 500 Internal Server Error');
            echo 'Class ' . $className . ' does not exist.';
            */
            return false;
        }
    }
    else {
        /*
        header('HTTP/1.0 404 Not Found');
        die('Cannot locate view (' . $fileView . ').');
        */
        return false;

I hope this is sufficient.

In short what i am trying to find out is that the csv that it produces has more columns than columns headers and where the difference comes from

357

Answer

Solution:

My guess would be that the page you are calling (on the server) is generating the CSV file.

You would need to write code on the server to do the conversion.

325

Answer

Solution:

This method is making a post request to getView page. Your csv create code would be present on getView page.

863

Answer

Solution:

This is the front end code that creates an invisible form with your data:current_parameters.

See the content ofcurrent_parameters in the the current file.

Review back-end code and look for the"getExport" function (it should be the current php file loaded)

If you just copied this function from some example... you have to add also the back-end code on your own.

Update:

look at the getExport code:

$contents = putcsv($columns, ';', '"');

$contents .= putcsv($vals,';', '"');;

First row insert the titles , and the second loops the data and insert the other rows. Print the content of$columns and$vals and see what is happening. There are some strange conditions for filtering the columns... but can help you if you don't show the data you try to parse.

People are also looking for solutions to the problem: php - show div with forms when customer starts typing in first form

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.