Select2 field is blank on Edit modal window and inline edit

Select2 field is blank on Edit modal window and inline edit

mak1mak1 Posts: 25Questions: 13Answers: 1

Hi Allan,

I have the following code




<table id="mytable" class="table table-striped table-bordered" cellspacing="0" width="100%" ></table> <script> $(document).ready(function() { var editor; // use a global for the submit and return data rendering in the examples editor = new $.fn.dataTable.Editor( { table: "#mytable", ajax: "/CRUD", idSrc: "primarykey", fields: [ { label: "Holder", name: "holder", "type": "select2", "options":{{=asis(holders)}} } ] } ); // Activate an inline edit on click of a table cell $('#mytable').on( 'click', 'tbody td', function (e) { editor.inline( this, { onBlur: 'submit' } ); } ); var table = $('#mytable').DataTable( { data: {{=asis(results)}}, "scrollX": true, dom: 'lTBfrtip', columns: [ { data: 'holder',editField: title: 'Holder', defaultContent: "", render: function ( data, type, full, meta ) { return data.user.first_name+' '+data.user.last_name; } } ], select: { style: 'os' }, buttons: [ { extend: 'create', editor: editor }, { extend: 'edit', editor: editor }, { extend: 'remove', editor: editor } ] } ); } ); </script>

when rendered my editor options (that is options:{{=asis(holders)}}") is as follows

   "options":[{"value": 1, "label": "Joe"}, {"value": 2, "label": "Mort"}, {"value": 3, "label": "Jane"}]

and my datatable data (that is "data: {{=asis(results)}},") is follows

{"holder": {"value": 1, "label": "Joe"}}

When the page loads, the correct option in the select2 list is automatically selected.
However, my problem is that when I click on the row and then Edit button OR the inline edit of this field, it goes to blank. It's not detecting the field that was already selected.

I have tried the "editField" option in datatables and that didn't help.

Any thoughts?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Answer ✓

    name: "holder",

    Should probably be name: "holder.value",.

    Also the editField should match that name.

    I think at the moment the issue is that your Editor instance is looking at the object, not the value.

    Allan

  • mak1mak1 Posts: 25Questions: 13Answers: 1

    Yeap that did the trick :)

This discussion has been closed.