Default Content

Default Content

karliekarlie Posts: 77Questions: 15Answers: 0

Apologies if this is covered somewhere, but I haven't found a solution. If I edit a record and one of the fields is blank, when the record saves it inserts 0.00 into the blank table cell. I have tried adding:

{
                "label": "Each:",
                "name": "each_price",
                "defaultContent": ""                
            },

An suggestions?

This question has an accepted answers - jump to answer

Answers

  • Loren MaxwellLoren Maxwell Posts: 167Questions: 37Answers: 7

    The issue might be on the back end.

    Can the database accept empty string? And is the database default 0?

    Also, what are you hoping to insert into the field? null?

    If so, then maybe null (or function () { return null; } )) for the default content might work.

  • karliekarlie Posts: 77Questions: 15Answers: 0
    edited February 14


    Just tried default content as null

    {
                    "label": "10:",
                    "name": "ten",
                    "defaultContent": "null"
                },
    

    Sadly that didn't work.

    db.png 74.3K
  • Loren MaxwellLoren Maxwell Posts: 167Questions: 37Answers: 7
    edited February 14

    With the quotes around it it's interpreted as a string that incidentally says "null" rather than an actual null value.

    Try null without the quotes:

    {
                    "label": "10:",
                    "name": "ten",
                    "defaultContent": null
                },
    

    If that doesn't work then try the function that returns null without any quotes:

    {
                    "label": "10:",
                    "name": "ten",
                    "defaultContent": function () { return null; }
                },
    
  • tangerinetangerine Posts: 2,158Questions: 15Answers: 238

    https://editor.datatables.net/manual/php/formatters#Examples

    Apply a formatter to your editor field. I use "null if empty"

                Field::inst( 'table.field')->setFormatter( 'Format::nullEmpty' )
    
  • Loren MaxwellLoren Maxwell Posts: 167Questions: 37Answers: 7

    @tangerine's solution is a good one too! I had forgotten about that!

  • karliekarlie Posts: 77Questions: 15Answers: 0

    Thanks both of you for your help. @Loren Maxwell those aren't working sadly.

    @tangerine could you assist me with where exactly it goes in the editor code?

    var editor = new $.fn.dataTable.Editor( {
            formOptions: {
            bubble: {
            drawType: 'none'
            },
            },
            ajax: 'php/my_ajax_file.php',
            table: '#mytable',
            fields: [
                {
                    "label": "Web:",
                    "name": "web",
                    "type": "select",
                    "def": "YES",
                    "options": [
                        "YES",
                        "NO"
                    ]
                },
                {
                    "label": "Sold Out:",
                    "name": "sold_out",
                    "type": "select",
                    "def": "NO",
                    "options": [
                        "NO",
                        "YES"
                    ]
                },
                {
                    "label": "SKU:",
                    "name": "sku"
                },
                {
                    "label": "Type:",
                    "name": "type",
                    "type": "select",
                    "def": "EACH",
                    "options": [
                        "EACH",
                        "PCT",
                        "PAIR",
                        "GRAM",
                        "BOX"
                    ]
                },
                {
                    "label": "Category:",
                    "name": "category"
                },
                {
                    "label": "Material:",
                    "name": "material"
                },
                {
                    "label": "Shape:",
                    "name": "shape"
                },
                {
                    "label": "Size:",
                    "name": "size"
                },
                {
                    "label": "Name:",
                    "name": "name"
                },
                {
                    "label": "Each:",
                    "name": "each_price"                
                },
                {
                    "label": "10:",
                    "name": "ten"
                },
                {
                    "label": "50:",
                    "name": "fifty"
                },
                {
                    "label": "ppct:",
                    "name": "ppct"
                },
                {
                    "label": "Av. Weight:",
                    "name": "av_weight"
                },
                {
                    "label": "Gram Price:",
                    "name": "gram_price"
                },
                {
                    "label": "Datafile Desc.:",
                    "name": "datafile_desc"
                },
                {
                    "label": "Extra Info:",
                    "name": "extra_info"
                }
            ]
        } );
    
  • karliekarlie Posts: 77Questions: 15Answers: 0

    I thought of just targeting it with CSS but guess 0.00 would still be exported in CSV and Excel from the buttons

  • Loren MaxwellLoren Maxwell Posts: 167Questions: 37Answers: 7
    edited February 14 Answer ✓

    The formatter @tangerine is referring to would go in your PHP file "php/my_ajax_file.php".

  • karliekarlie Posts: 77Questions: 15Answers: 0

    Haha, can't believe I was being so thick, that works, thanks very much both!

  • karliekarlie Posts: 77Questions: 15Answers: 0

    Just need to update all the records now to get rid of 0.00 in rows that have already been edited. Might just import through phpmyadmin

  • tangerinetangerine Posts: 2,158Questions: 15Answers: 238

    I'm not sure whether you're asking, but anyway....

    UPDATE table SET field = null WHERE field = 0.00
    
  • karliekarlie Posts: 77Questions: 15Answers: 0

    Thanks Tangerine, I took the longer route, I exported it, did a search and replace, and imported again!

Sign In or Register to comment.