1

Apologies for all the code in this post, but I don't know how else to describe it.

I have some Javascript, which is in a Vue component (the component is to encapsulate a Datatable):

let table = $('#datatables-table').DataTable({ 

    "ajax": {
        "url": self.dataUrl,
        "type": "POST",

        "data": function(d){
            d.table = self.table;
        },

    },


"columns": self.tableColumns,
    order: [1, 'asc'],
    dom: "Bfrtip",
    responsive: false,//true wont let me hide columns
    "scrollY": "600px",
    "scrollCollapse": true,

    "paging": false,
    select: {
        style: 'single',
        selector: 'td:first-child',
        blurable: true

    },

    buttons: self.buttons,

});

I am trying to pass the buttons (self.buttons) configuration in, this is what I want it to look like:

[{extend:'remove', editor: editor},{extend:'create', editor: editor},{extend:'edit', editor: editor},],

The variables get populated, in my mounted method. via AJAX:

axios.get(self.tableUrl)
            .then(function (response) {

                self.tableHeaders = response.data.tableHeaders;
                self.panelTitle = response.data.panelTitle;
                self.table = response.data.table;
                self.editorFields = response.data.editorFields;
                self.tableColumns = response.data.tableColumns;
                self.buttons = response.data.buttons;

This is the PHP (Laravel), to form the response:

$dt->buttons = array(
    array('extend' => 'remove', 'editor' => 'editor'),
    array('extend' => 'create', 'editor' => 'editor'),
    array('extend' => 'edit', 'editor' => 'editor')

);

Then I do a JSON.stringify in the JS, but the problem here is that I end up with this:

"[{"extend":"remove","editor":"editor"},{"extend":"create","editor":"editor"},{"extend":"edit","editor":"editor"}]"

So, how can I pass the buttons in, so that they are not a string and the editor is not "editor" (double quoted).

I think I need to somehow pass it as a string, but remove the quotes.

Mick

Update: editor is defined above:

 let editor = new $.fn.dataTable.Editor({
                 "ajax": {
                "url": self.dataUrl,
                "type": "POST",
                data: {table: self.table}
            },

    table: "#datatables-table",
    idSrc: self.table + '.id',
    fields: [self.editorFields]
        });

Full code here: http://artisantinkerer.github.io/2016/12/15/Vue-Datatables.html

7
  • Sounds like you want to JSON.parse. Commented Apr 10, 2017 at 14:35
  • I can't see where that would help? It turns strings to objects. Commented Apr 10, 2017 at 14:51
  • Apparently I'm missing something. You indicate you want to pass an array of objects to the buttons property of your DataTable initialization object. What is the content of response.data.buttons? If you're getting the JSON string, then JSON.parse(response.data.buttons) should turn it into the array you want. Commented Apr 10, 2017 at 15:02
  • [{extend: "remove", editor: "editor"}, {extend: "create", editor: "editor"},…] but this is not any use because of the double quotes around "editor". Commented Apr 10, 2017 at 15:04
  • I need it to look like this: [{extend:'remove', editor: editor},{extend:'create', editor: editor},{extend:'edit', editor: editor},], Commented Apr 10, 2017 at 15:05

1 Answer 1

1

If response.data.buttons is truly a javascript array, then you could replace the value of the editor property this way.

response.data.buttons.map(b => ({extend: b.extend, editor: editor}))

Or just not render the editor property at all from the server and map over and add it clientside.

Sign up to request clarification or add additional context in comments.

2 Comments

So, I just added this self.buttons = self.buttons.map(b => ({extend: b.extend, editor: editor})); just after the editor definition. It worked! Brilliant. Thanks.
Yes, editor hadn't been defined in the AJAX success, so I had to do it later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.