Select Field Type - how to set default value?

Select Field Type - how to set default value?

Karl53Karl53 Posts: 72Questions: 29Answers: 0

How do I set the type -select to "Monthly"? Currently, it defaults to the first item.

This is my options list:

        CMP_FREQ = {"Continuous": 0, "Daily": 1, "Weekly": 2, "Biweekly": 3, "Twice Monthly": 4, "Every 4 Weeks": 5, "Monthly": 6, "Bimonthy": 7, "Quarterly": 8, "Every 4 Months": 9, "Semiannually": 10, "Annually": 11, "Exact": 12, "None": 13},

My field initialization is this:

        var editor = new $.fn.dataTable.Editor({
                ajax: function (method, url, data, success, error) {
                    if (data.action === 'edit') {
                        if (typeof rowDataArray[CMP_FREQ_SEL] !==  'number') {
                            rowDataArray[CMP_FREQ_SEL] = parseInt(rowDataArray[CMP_FREQ_SEL], 10);
                        }
                        success({row: rowDataArray});
                    }
                },
                legacyAjax: true, // if true, versions prior to v1.5.0
                idSrc: IDX_COL,
                formOptions: {
                    inline: {
                        onBlur: 'submit'
                    }
                },
                fields: [
                    {
                        name: CMP_FREQ_SEL,
                        type: "select",
                        options: CMP_FREQ,
//                      def: "Monthly",
                        def: 6,
                        id: "cmp_freq_sel"
                    },


init -placeholder does not seem to be what I need in that the default is an item in the select list.

Thanks.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,055 Site admin

    def: 6

    Should be all that you need there. Perhaps try def: '6'?

    If that doesn't work, can you give me a link to the page so I can check it out please?

    Allan

  • Karl53Karl53 Posts: 72Questions: 29Answers: 0
    edited March 2018

    Thanks, Allan. I believe I understand what my problem is. The table is initialized with "empty" rows, that is, each cell is set to null. Editor is used to add values. Thus def: '6' does not work because the cell's value is not undefined when the initial Editor open event fires.

    Can you give me a pointer how to hook the default select element and set its value when the cell contains a null? I assume I would do this in the Editor's open?

    Currently the page is only on localhost.

  • allanallan Posts: 61,446Questions: 1Answers: 10,055 Site admin
    Answer ✓

    Yes, for that approach you would need to explicitly set the values. Perhaps something like:

    editor.on('preEdit', function () {
      // some logic to determine if it is a "new" row, or an old one
      if ...
    
      editor.val( 'fieldName', 6 );
    } );
    

    Allan

  • Karl53Karl53 Posts: 72Questions: 29Answers: 0

    Thanks, Allan.

    Putting the code in the 'preOpen' event handler worked for me.

    For anyone else that has this use case:

    .on('preOpen', function (e, type) {
        var arr, rowModifier;
        rowModifier = editor.modifier();
        if (rowModifier && rowModifier.cellIndex === COL_NO) {
            arr = tblDebtList.row(rowModifier.parentNode).data();
            if (arr[DATA_IDX] === null) {
                editor.val('field_name', SELECT_INDEX);
            }
        }
    })
    
  • Rusty BallingerRusty Ballinger Posts: 21Questions: 7Answers: 0

    Hey, thanks for this question & answer; it helped me today.

    (I added a call to editor.val() in initEdit to set the selection to the most-recently-selected value; to get that, I'm stuffing it into a var in preSubmit.)

    This site's search engine + the quality of the forums are a lot of what makes DataTables so great to use; every time I want to do something, there's an example I can copy from.

This discussion has been closed.