Upload plugin not working correctly

Upload plugin not working correctly

jtoler5jtoler5 Posts: 93Questions: 34Answers: 3

Every time I edit a row to upload a file to a row, it ends up changing the date to null or start of SQL date. I can not figure out why it is passing date even though I have told it not to.

I created a custom button for file uploads when editing a row because I am using inline edit.

This is the query it runs that makes the date change:

"UPDATE charges SET Date = :Date, Paid = :Paid, Modified = :Modified, User = :User, documentId = :documentId WHERE charges.id = :where_0"

http://debug.datatables.net/amuzal

This question has an accepted answers - jump to answer

Answers

  • jtoler5jtoler5 Posts: 93Questions: 34Answers: 3

    I actually just discovered it is doing this on columns I have a setFormatter applied to. How would I only make it run if the value is set? I tried simply saying if($val != null) but that doesn't seem to work.

    Editor::inst($db, 'charges')
            ->fields(
                Field::inst('charges.ID'),
                Field::inst('charges.fy')
                    ->validator('Validate::notEmpty'),
                Field::inst('charges.AccountCode'),
                Field::inst('charges.Fund'),
                Field::inst('charges.FBFY'),
                Field::inst('charges.SubCode2'),
                Field::inst('charges.Date')
                    ->validator('Validate::notEmpty')
                    ->getFormatter(function ($val, $data, $opts) {
                        return date('m/d/Y', strtotime($val));
                    })
                    ->setFormatter( function ( $val, $data, $opts ) {
                            return date('Y-m-d H:i:s', strtotime($val));
                    }),
                Field::inst('charges.Vendor')
                    ->validator('Validate::notEmpty'),
                Field::inst('charges.Description')
                    ->validator('Validate::notEmpty'),
                Field::inst('charges.Expense')
                    ->validator('Validate::notEmpty'),
                Field::inst('charges.Type')
                    ->validator('Validate::notEmpty'),
                Field::inst('charges.Paid')
                    ->setFormatter(function ($val, $data, $opts) {
                        return !$val ? 0 : 1;
                    }),
                Field::inst('charges.Modified')
                    ->getFormatter(function ($val, $data, $opts) {
                        return date('m/d/Y', strtotime($val));
                    })
                    ->setFormatter(function ($val, $data, $opts) {
                        return date('Y-m-d H:i:s');
                    }),
                Field::inst('charges.User')
                    ->setFormatter(function ($val, $data, $opts) {
                        return $_SESSION['username'];
                    }),
                Field::inst('charges.B1'),
                Field::inst('charges.B2'),
                Field::inst('charges.B3'),
                Field::inst('charges.documentId')
                    ->upload(
                        Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads/__ID__.__EXTN__' )
                            ->db( 'uploads', 'id', array(
                                'uploads.webPath'     => Upload::DB_WEB_PATH,
                                'uploads.fileName'    => Upload::DB_FILE_NAME,
                                'uploads.fileSize'    => Upload::DB_FILE_SIZE,
                                'uploads.systemPath'  => Upload::DB_SYSTEM_PATH
                            ) )
                    ),
                Field::inst('uploads.webPath'),
                Field::inst('uploads.fileName')
            )
            ->leftJoin( 'uploads', 'charges.documentId', '=', 'uploads.id' )
            ->where('charges.FBFY', '10135115', '=')
            ->where('charges.AccountCode', $ac, '=')
            ->where('charges.Type', $tipe, '<>')
            ->process($_POST)
            ->json();
    
  • jtoler5jtoler5 Posts: 93Questions: 34Answers: 3

    I think the main issue is actually in my custom upload button function. I got it to stop making date null but now it is not returning the correct row. What would be the best way to fix this?

    When the editor_upload gets created per row, in the callback, I assigned that ID to editor_upload which is why this gets its id.

    $('#data_entry').on( 'click', '.editor_upload', function (e) {
            e.preventDefault();
            console.log(this.id);
            var node = $('tr#'+this.id);
            var values = editor.edit(node[0], false).val();
            uploadEditor
                .title( 'Upload Document' )
                .buttons( { label: 'Save', fn: function() { this.submit(); } } )
                .edit(values);
        });
    
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Hi,

    Could you clarify what you mean by "in the callback, I assigned that ID to editor_upload which is why this gets its id"? I don't quite understand what the use case is I'm afraid.

    It looks like you have two different Editor instances being used at the same time, is that correct? edit( values ) doesn't look quite right to me - the edit() method will accept a row identifier - it does not expect an object of values as is being given above.

    Allan

  • jtoler5jtoler5 Posts: 93Questions: 34Answers: 3
    edited July 2015

    Yes I have different instances being used. The child row needed to be editable also. It basically just repeats the same information for columns that may or may not be hidden by the user. I work for some crazy people. The use case is that at the end of each row is a drop down with a 'duplicate, delete, upload' action and I only wanted the upload action to display the upload field not the rest of the row. Here is my code... let me know if I could provide anything else. Thanks!

    Sorry, I should have posted my whole code. It's just lengthy so I didn't.
    Forgive any stupidity in the code, I know it's not the best.

    http://pastie.org/10282091

    Edit: I forgot to mention. I've been playing around with the function and just trying ways to work around it. I know it's not currently the best but it's 80% working now.

    Here is a screenshot of the table, if it helps. I couldn't show the whole thing for reason beyond my control but it should show the idea.

    http://i.imgur.com/BusVPyN.png

  • jtoler5jtoler5 Posts: 93Questions: 34Answers: 3
    edited July 2015

    So you mentioning the it accepts a row identifier, it appears I finally got it working. Can't believe it took me so long to figure this out. Can you please tell me if there would be a better way to do this?

        $('#data_entry').on( 'click', '.editor_upload', function (e) {
            e.preventDefault();
            var node = $('tr#'+this.id);
            uploadEditor
                .title( 'Upload Document' )
                .buttons(
                    {
                        label: 'Save', fn: function() {
                        this.submit();
                    }
                }).edit(node[0]);
        });
    

    The only issue I seem to be having now is, when I click upload, perform the upload, and click save. When I later go back and click on upload, it shows 2 'x's at the top right?

    http://i.imgur.com/eYVtRJI.png

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    The only change I think that could be made is to use edit( this ). However I've made an assumption there - is .editor_upload the tr element for the row? If not, then how does the above work without having duplicate ids in the HTML.

    it shows 2 'x's at the top right?

    Odd! If you do it a third time, does it show 3? Are you using the Bootstrap modal for Editor (it doesn't look like the default lightbox)?

    Regards,
    Allan

  • jtoler5jtoler5 Posts: 93Questions: 34Answers: 3

    Well wouldn't this then refer to the a href?

    <a href="" class="editor_upload" id="upload_'+data.DT_RowId">Upload</a>

    It gets called as a render for column and I added upload before the id so it would be different than row_id.

    I know I have the editor bootstrap css included. Is there a separate one for just modals?

    No it never shows 3 if I click it a third, fourth, etc.

This discussion has been closed.