multiple row add

multiple row add

advaniaadvania Posts: 35Questions: 13Answers: 0

So.. i setup an editor overlay tied to a button "Import" and it pushes only a single field named "csvimport", there i input some comma separated string that matches the columns in the datatable.

I'm then catching this data after submit with preCreate event server-side.. and now I'm wondering what would be the best way to split this down and run multi-insert, triggering each columns validator/formatter etc..

I found a solution client-side on the forums here but I'd much rather do this server-side if possible.

->on( 'preCreate', function ( $e, $row) {
$row['csvinput'] <-- this has a string containing multiple lines of CSV

any ideas?

This question has an accepted answers - jump to answer

Answers

  • advaniaadvania Posts: 35Questions: 13Answers: 0

    So I added a custom event for backend just as _process is run, to handle the CSV and re-format $data['data']. And then a custom function in the editor class, to handle input of the data and push that back into $data['data'].

    Any idea on how to solve this less hacky? This is of course not what I want as a permanent solution but it works like a charm.

    **Editor.php:**
    
    // piggyback on the _processData variable, it hasn't been set when the custom event runs anyhow..
    
        public function set_formData ($data) {
            $this->_processData = $data;
        }
    
    
    // inside the _process function, just before setting _processData variable..
    
            if ( isset($data['action']) ) {
                if ( $data['action'] == 'create' ) {
                    $cancel = $this->_trigger( 'inputData', $data );
                }
            }
    // The _processData variable should be empty, if not it's cause we pushed new data into it from set_formData in 'inputData' event handler.
    
            if(!empty($this->_processData)){
                $data['data'] = $this->_processData;
            }
    
            $this->_processData = $data;
    
    **editor_backend.php:**
    
            ->on( 'inputData', function ( $e, $row ) {
    
                $lines = preg_split("/\r\n|\n|\r/", $row['data'][0]['csvinput']);
    
                foreach($lines as $line) {
                    // Handle empty lines
                    if($line=="")
                        continue;
                    // Split line to array
                    $arr_ = explode(',', $line);
                    // Put split array into editor format.
                    $data[]['foo'] = array(
                        'test1' => $arr_[0],
                        'test2' => $arr_[1],
                        'test3' => $arr_[2]
                    );
                }
    
                $e->set_formData($data);
            } )
    
    
  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin
    Answer ✓

    I would suggest doing this on the client-side in fact. The create() method can be used to create multiple rows with a single Ajax call, and the multi-row editing API can be used to set the values for the fields / rows. Might make an interesting blog post this in fact :).

    Allan

  • advaniaadvania Posts: 35Questions: 13Answers: 0

    Yeah i tried that on the client side, but had issues with the textarea box not pushing more than a few lines.. and after that output was empty and just felt the code was messy client side.

    I cleaned up my Editor.php patch, and marked the code properly so when I upgrade it'll be easy to re-patch.. I'm keeping this 'hack' as is.. but I'm really open to that blog post! ;-)

    I wanna be able to push 256 entries minimum with ~50 chars per line of comma separated entries. this works very well server-side.

This discussion has been closed.