How to use object on Editor?

How to use object on Editor?

yu yen kanyu yen kan Posts: 65Questions: 31Answers: 0
edited March 2017 in Free community support
function obj(array){
        this.array = array;
        this.array_id_list = function(){
            var data = [];
            for (var i = 0; i < this.array.length; i++) {
                data.push(array[i]['id']);
            }
 
            return data;
        }
    }

    var editor = new $.fn.dataTable.Editor( {
                    table: "#table",
               ajax: {
                    create: "{{ Route("route1") }}",
                    edit: {                        
                        url: "{{ Route("route2") }}",
                        type: "PUT"
                    },
                    remove: {                        
                        url: "{{ Route("route3") }}",
                        type: "DELETE"
                    }
                },
                    idSrc:  'id',
                    fields: [  {
                            label: "array:",
                            name: "array_id_list",
                            multiple: true,
                            type: "select",
                            options: [{ label : "abc", value: "1"}, { label: "bcd", value: "2"} ]
                        }
                    ]
                });

    var data = new Array();
 
    for(var i = 0; i < 5;i ++){
        data[i] = new obj(i);
    }
 
    var table= $('#table).DataTable({
                    dom: "Bfrtip",
                    data: data,
                    select: true,
                    columns: [
                        { data: 'array[, ].id, title: "Array id" }
                    ],
                    buttons: [
                        { extend: "create", editor: editor},
                        { extend: "edit", editor: editor},
                        { extend: "remove", editor: editor}
                    ]
                });


when I create a record, I get requested unknown parameter 'array_id_list' for new row, is there a setting to let me assign server returned data to the object?

Answers

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    You are using 'array[, ].id (note your have a syntax error as there is a missing '), but this.array in your obj is an integer. data: 'array' for the column should show just the number.

    The naming of this.array is probably adding to the confusion since it isn't an array.

    Allan

  • yu yen kanyu yen kan Posts: 65Questions: 31Answers: 0

    no, this.array is an object of data that include id, name, and others data to display in table, array[, ].id will show all id in the array with delimiter ', '. The question I want to ask is how can I set default value when edit for multiple select type field

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Not according to the above code it isn't:

    data[i] = new obj(i);
    

    where i is 1-5.

    Then:

    function obj(array){
            this.array = array;
    ...
    

    So this.array is assigned the value of the argument which is passed in, which is 1-5. So unless I'm missing something, this.array is a number in the above code.

    The question I want to ask is how can I set default value when edit for multiple select type field

    The structure of the default value should match whatever it is that you get when you run field().val() to get the value of the field.

    Allan

This discussion has been closed.