How to make cell().data() persist to DB

How to make cell().data() persist to DB

ntstravelntstravel Posts: 15Questions: 2Answers: 1

Noob question here: once I've modified a cell's data with cell().data() how do I get it to 'post' that to the DB?

So... I've added a button (based on one of your examples) that re-populates my 'rowReorder' column with 1...n based on the current sort. This is so that I can initially sort my data maybe alphabetically by name, for example, then 'save' that sort in my rowReorder column, then start reordering again from there. (Order is relevant to a lot of our lists.)

The function is working great, but it's just not saving to the DB. Can anyone please help me add to below code so it saves?

buttons: [
    {
        text: 'Reorder',
        action: function (e, dt, node, config) {
            table.column(0, {search:'applied', order:'applied'}).nodes().each(function (cell, i) {
                table.cell(cell).data(i + 1).draw();
            });
        },
    },

Answers

  • allanallan Posts: 51,440Questions: 1Answers: 7,758 Site admin

    once I've modified a cell's data with cell().data() how do I get it to 'post' that to the DB?

    DataTables itself won't do that for you I'm afraid. If you are updating the data directly, you'd then need to call $.ajax (or similar, e.g. fetch if you are targeting a newer browser) to submit the data to the server and then have a script there to write that to the database.

    It is also possible to use Editor to do this if you are using Editor.

    Allan

  • ntstravelntstravel Posts: 15Questions: 2Answers: 1

    Yes, I have a paid version of editor, with an editor instance on that same page.

  • ntstravelntstravel Posts: 15Questions: 2Answers: 1

    Thanks, Allan, I'll check out that link - looks like that'll work.

  • ntstravelntstravel Posts: 15Questions: 2Answers: 1

    I'm getting a little closer, but still struggling. I've tried the following and I'm a bit confused, rowLoop logs to the console as 0, 1, 2... but all the 'name's change to '0'?

    I'm not sure whether what I'm passing into the .edit() method is correct either?

    table.rows({order:'current'}).every(function (rowIdx, tableLoop, rowLoop) {
        var data = this.data();
        console.log(rowIdx + " " + rowLoop);
        editor
            .edit(this.data(), false)
            .set('name', rowLoop)
            .submit();
    });
    
  • ntstravelntstravel Posts: 15Questions: 2Answers: 1
  • colincolin Posts: 8,085Questions: 0Answers: 1,357

    You wouldn't want to do that on a column where Editor is sending the data back, since the data is really just specific to the current ordering. The best bet would be to have a column specifically for ordering, and do something like this example here.

    Colin

  • ntstravelntstravel Posts: 15Questions: 2Answers: 1

    Thanks, Colin. I've already managed to do what's in that example link - my question, though, is how would I use editor to save that column if I did want to save that column to the DB?

  • ntstravelntstravel Posts: 15Questions: 2Answers: 1

    So, currently, I'm able to reorder rows perfectly. I have a display_order column that's under rowReorder and it's working great - we can drag-and-drop rows and display_order is updated and saved to the DB.

    My user, though, now wants a button to 'reset' that column (1...n) based on the current state of the table (so if they've decide to just sort it alphabetically, they can sort by the 'name' column, for example, then reset display_order and it'll 'save' that).

  • ntstravelntstravel Posts: 15Questions: 2Answers: 1

    In other words, we want to be able to sort our data manually, and save that sort - which is already working 100%. However, we'd like to also be able to reset our manual sorts if they become jumbled and we want to start afresh.

    Basically, if I could find a way for editor to post the data in the order column of this example, I'd be able to do the rest.

  • ntstravelntstravel Posts: 15Questions: 2Answers: 1

    I've tried this code:

    table.column(0, {order:'current'}).nodes().each(function(cell, i){
        console.log('i = ' + i);
        editor.edit(this, false)
            .set('display_order', i)
            .submit();
    });
    

    However, even though i is the value I want, the entire column just gets set to '0' - can't understand why...

  • ntstravelntstravel Posts: 15Questions: 2Answers: 1
    edited February 24

    Here is the simplest code, I think, so far:

    var rowIndexes = table.rows({order:'current'}).indexes().toArray();
    for (i = 0; i < rowIndexes.length; i++) {
        editor.edit(rowIndexes[i], false).set('display_order', i).submit();
    }
    

    This, mysteriously, is only changing the first row in the table. Do I need to be waiting for a 'submitComplete' event before going on to the next row?

  • colincolin Posts: 8,085Questions: 0Answers: 1,357

    Ah, you would want to iterate through the rows, rather that the columns - something like this here.

    Colin

Sign In or Register to comment.