Datatables Editor is sendimg [object Object] instead of actual JSON

Datatables Editor is sendimg [object Object] instead of actual JSON

minifiredragonminifiredragon Posts: 82Questions: 16Answers: 2
edited April 2023 in Editor

I am not sure if I am overlooking something, but the data I populate my datatables with has some JSON within it. And at initialization of the table the data is parsed and the result is displayed properly. However, when I use editor to edit a field and send to server (I need to send all data for the row), the parts of the data that are JSON are sent as [object Object] and not actual JSON data (The raw data also shows [object Object]).

Is there something I am overlooking in a setting for Editor to send the JSON packet correctly? I do know I can use the individual JSON entries in the editor field descriptor, but it uses multiple parts of the JSON to calculate a response. I was ignoring the problem until now, as I am working with the data instead of just displaying it.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Can you show me your Editor initialisation, any events you use from it and ideally a link to the page you are working on please?

    Allan

  • minifiredragonminifiredragon Posts: 82Questions: 16Answers: 2

    Sorry, I did not think the question would need a code example. Everything works as it should aside from datatables not sending the actual JSON.

    assocs and detailData are both JSON data stored in the datatables data array. I can filter, order, etc without an issue, and I checked to make sure the data was unaltered before sending to editor.

    editor = new $j.fn.dataTable.Editor(
            {
                ajax:
                {  
                    url: 'brandedUpdate.cfm?name=test,
                    data: function(d)
                    {
                        return JSON.stringify( d );
                    }
                },
                formOptions: {
                    bubble: {
                        title: true,
                        onBackground: false,
                        submit: 'allIfChanged'
                    }
                },
                table: '#tProfit',
                fields: [
                    { label: 'ID:', name: 'ID' },
                    { label: 'name:',  name: 'name'  },
                    { label: 'sku:',  name: 'sku'  },
                    { label: 'price:',  name: 'price'  },
                    { label: 'item:', name: 'item'  },
                    { label: 'currentSales3:',  name: 'currentSales3'  },
                    { label: 'currentSales30:',  name: 'currentSales30'  },
                    { label: 'currentSales300:',  name: 'currentSales300'  },
                    { label: 'currentSales3:',  name: 'currentSales3'  },
                    { label: 'currentSales30:',  name: 'currentSales30'  },
                    { label: 'currentSales300:',  name: 'currentSales300'  },
                    { label: 'assocs:',  name: 'assocs'  },
                    { label: 'replenishQty:',  name: 'replenishQty'  },
                    { label: 'currentInv:',  name: 'currentInv'  },
                    { label: 'inboundInv:',  name: 'inboundInv'  },
                    { label: 'upc:',  name: 'upc'  },
                    { label: 'sales1:',  name: 'sales1'  },
                    { label: 'sales2:',  name: 'sales2'  },
                    { label: 'sales3:',  name: 'sales3'  },
                    { label: 'detailData:',  name: 'detailData'  },
                    { label: 'keywords:',  name: 'keywords'  },
                    { label: 'images:',  name: 'images'  },
                    { label: 'approved:',  name: 'approved'  },
                    { label: 'notes:',  name: 'notes'  },
                    { label: 'startDate:',  name: 'startDate'  },
                    { label: 'archive:',  name: 'archive'  },
                ]
            });
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Ah I see - it is just specific fields that are the issue, not all of them. I strongly suspect that it is because those fields are JSON data already, but Editor doesn't have a built in JSON editor.

     { label: 'detailData:',  name: 'detailData'  },
    

    for example is a text field. So if you attempt to put an object into it it will indeed be [object Object] since that is the toString() of a JSON object. That isn't a problem with the data being sent to the server, but rather with the field.

    In your original data source is detailData a JSON object? I'm presuming so. And you want it to display as JSON for the end user to edit?

    Allan

  • minifiredragonminifiredragon Posts: 82Questions: 16Answers: 2

    I was confusing myself seeing datatables JSON output. The detailData is an object and assocs is an array of objects. I am sorry I was not clear on that point. However, I realize that I do not need to send those fields, so I removed them from the editors list.

    Sometime in the past when I picked up this lovely bit of software I remember having to send all the data or the datatable would complain it was missing fields (probably user error since I was learning a new package) when the editor would send the data back, and I never checked if I still had the same issue, being stuck in the old way of thought.

    However, is there a way to fix this issue? There may come a time I want to use an object or an array of objects as stored data in a field to be sent to the server. And yes, I do have a field that stores the data as an object that people do edit. However, I handle that code individually and make the edit outside of editor giving the complex nature of that particular field.

    And if you are curious on that field, it is for shipping postage/shippers. Allowing the user to select multiple methods of shipping the item and when they are done the field shows them what was selected, and that data is used to populate other cells to display the postage they selected.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    If it is a field where the end user can plain text edit the JSON string, then you could use a get and set formatter on the client-side: fields.getFormatter and fields.setFormatter - i.e. to JSON.parse(val) in the get formatter and JSON.stringify(val) in the set formatter.

    However, if the end user can't interact with that field in the Editor form, just drop that field. Editor doesn't need to operate on all fields, just because they are in the data set. It will happily ignore them :). Example of that here.

    The final option, if you are using a JSON editor and want to embed it into Editor, is to create a custom field type plug-in. It can then process the value (JSON in this case, perhaps of a certain schema) as needed for editing. Editor would just set and get the value from that field type plug-in.

    Allan

  • minifiredragonminifiredragon Posts: 82Questions: 16Answers: 2

    Thank you for the information.

Sign In or Register to comment.