can't set default value for a field in bubble

can't set default value for a field in bubble

mokozukimokozuki Posts: 13Questions: 3Answers: 0
edited April 2018 in Free community support

I am trying to prefill one of the fields (sort order field) with the highest number + 1 when a new record is added vi the bubble. i keep getting NaN, when i use the same expression in the js console, i do get the expected result. I have also tried with unary operator and Number() as well.

 editor = new $.fn.dataTable.Editor({
    ajax: '/ax/tableProductList.php?list_id='+$('#list-id').html(),
    table: '#recordsTable',
    fields: [
        { label: 'Order', name: "mz_product_list_items_bak.item_order", def: parseInt($('#recordsTable tr:last td:first-child').val() ,10)+1 },
        { label: 'ID', name: "mz_product_list_items_bak.product_list_item_id" },
        { label: 'Name', name: "mz_getProducts_dyfs.name_external" },
        { label: 'Item #', name: "mz_product_list_items_bak.product_id" },
        { label: 'Last Update', name: "mz_product_list_items_bak.last_update" },
        { label: 'Created By', name: "mz_product_list_items_bak.created_by" }
    ]
    });

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Hi Mokozuki,

    I don't think you can do expressions there, I suspect it's expecting a constant. Could you just assigned that to a variable outside of the Editor initialisation and just refer to the variable there instead?

    Cheers,

    Colin

  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    Answer ✓

    That line of code will evaluate, but it is attempting to get the val() from a td cell, which isn't going to work (since a cell doesn't have a value property). Do you have an input inside that cell you want to get the value of, or do you just want the text value of the last cell in the table (cell().data())?

    Also you might want to use the field.def option as a function so it is evaluated when the "New" form is shown, rather than when the Editor is constructed.

    Allan

  • mokozukimokozuki Posts: 13Questions: 3Answers: 0
    edited April 2018

    solved my problem. this how i implemented the suggestion:

    setting last row number +1 as default value in bubble form when adding a new record

        editor = new $.fn.dataTable.Editor({
        ajax: '/ax/tableProductList.php?list_id='+$('#list-id').html(),
        table: '#recordsTable',
        fields: [
            { label: 'Order', 
                  name: "mz_product_list_items_bak.item_order",
                  def: function() { 
                            return parseInt(table.cell('#recordsTable tr:last-child td:first-child').data()) + 1 
                  }
                },
            { label: 'ID', name: "mz_product_list_items_bak.product_list_item_id" },
            { label: 'Name', name: "mz_getProducts_dyfs.name_external" },
            { label: 'Item #', name: "mz_product_list_items_bak.product_id" },
            { label: 'Last Update', name: "mz_product_list_items_bak.last_update" },
            { label: 'Created By', name: "mz_product_list_items_bak.created_by" }
        ],
        formOptions: { main: { focus: 'mz_product_list_items_bak.product_id' } }
        });
    
This discussion has been closed.