Inline editor with multiple select fields

Inline editor with multiple select fields

avcarrilloavcarrillo Posts: 15Questions: 2Answers: 0

I have been unable to get multiple select fields working using the inline editor. It works fine with the pop out form.
I was having a problem just getting a single field to work, but found a workaround on the tech notes http://datatables.net/manual/tech-notes/11, but this example only shows how to make a single select field work, not with multiple. Also, this documentation makes note about using the editField, which I believe would resolve my issues, but it says you need Editor 1.4 or newer. I only see 1.3.3 available for download. Is this a typo, or just not released yet?

Any help would be appreciated. Below is my code. I will post the server generated json in a separate comment

HTML:

        <table cellpadding="0" cellspacing="0" border="0" class="display"
            id="Parts" width="100%">
            <thead>
                <tr>
                    <th></th>
                    <th>ID</th>
                    <th>Category</th>
                    <th>Vendor</th>
                    <th>Name</th>
                </tr>
            </thead>
        </table>

table.js

function buildDropDownList(list) {
    var api = list+".json";
    //var options = [];
    var options = new Array();
    $.ajax({
        type : "GET",
        processData : false,
        async : false,
        url : api,
        dataType : "json",
        success : function(data) {
            $.each(data, function(key, values) {
                $.each(values, function(val, select) {
                    options.push({
                        value : select.id,
                        label : select.name
                    });

                });
            });
        }
    });
    console.log(options);
    return options;
}
/*
 * Editor client script for DB table Category
 * Automatically generated by http://editor.datatables.net/generator
 */

(function($){

    $(document).ready(function() {
        var editor = new $.fn.dataTable.Editor( {
            "ajax": "parts.js",
            "table": "#Parts",
            "fields": [
                       {
                           "label": "id",
                           "name": "id",
                           "default": "",
                           "type" : "hidden"
                       },
                       {
                           "label": "Category",
                           "name": "category.id",
                           "type": "select",
                           "ipOpts": buildDropDownList("categories")
                       },
                       {
                           "label": "Vendor",
                           "name": "vendor.id",
                           "type": "select",
                           "ipOpts": buildDropDownList("vendors")
                       },
                       {
                           "label": "Name",
                           "name": "name",
                           "default": "Test",
                       }
                       ]
        } );

        $('#Parts').on('click', 'tbody td:not(:first-child)', function(e) {
            //editor.inline( $('#Parts tbody tr:first-child td:last-child'), 'category.id' );
            editor.inline( this, 'category.id', {
                submitOnBlur: true
            } );
        });

        $('#Parts').dataTable( {
            "dom": "Tfrtip",
            "ajax": "parts.json",
            "columns": [
                        {"data" : null, "defaultContent": '', "orderable": false, "width" : "20px"},
                        {"data" : "id", "visible" : false, "searchable" : false},
                        {
                            "data" : "category.name",
                            "editField" : "category.id"
                        },
                        {
                            "data" : "vendor.name",
                            "editField" : "vendor.id"
                        },
                        {
                            "data": "name"
                        }
                        ],
                        "tableTools": {
                            "sRowSelect": "os",
                            "aButtons": [
                                         { "sExtends": "editor_create", "editor": editor },
                                         { "sExtends": "editor_edit",   "editor": editor },
                                         { "sExtends": "editor_remove", "editor": editor }
                                         ]
                        }
        } );
    } );

}(jQuery));

This question has an accepted answers - jump to answer

Answers

  • avcarrilloavcarrillo Posts: 15Questions: 2Answers: 0

    parts.json

    {
      "data": [
        {
          "category_id": 36,
          "vendor_id": 11,
          "name": "Dell",
          "category": {
            "pattern": "^\\w+ .+$",
            "name": "backplan",
            "id": 13
          },
          "id": 41,
          "vendor": {
            "contact_phone": "555-555-5561",
            "website": "http://www.redapt.com",
            "contact_email": "email 11",
            "contact_name": "contact 11",
            "name": "Redapt",
            "id": 11,
            "description": "description 11"
          }
        },
        {
          "category_id": 36,
          "vendor_id": 12,
          "name": "HP",
          "category": {
            "pattern": "^\\w+.*$",
            "name": "motherboard",
            "id": 42
          },
          "id": 42,
          "vendor": {
            "contact_phone": "555-555-5562",
            "website": "http://www.samsung.com",
            "contact_email": "email 12",
            "contact_name": "contact 12",
            "name": "Samsung",
            "id": 12,
            "description": "description 12"
          }
        }
      ]
    }
    

    categories.json

    {
      "data": [
        {
          "pattern": "^\\w+.*$",
          "name": "motherboard",
          "id": 42
        },
        {
          "pattern": "^\\w+ .+$",
          "name": "backplan",
          "id": 13
        },
        {
          "pattern": "^\\w+ .+$",
          "name": "chasis",
          "id": 8
        },
        {
          "pattern": "^\\w+ \\w+ \\w+",
          "name": "writecacheBatt",
          "id": 43
        },
        {
          "pattern": "^\\w+ \\w+ CPU \\w\\d-\\d+ \\d @ \\d\\.\\d+GHz$",
          "name": "CPU",
          "id": 1
        },
        {
          "pattern": "^\\w+(HBA|10-Gigabit)\\w+$",
          "name": "FibreCard",
          "id": 2
        },
        {
          "pattern": "^[A-Z]\\d+.?$",
          "name": "GPU",
          "id": 3
        },
        {
          "pattern": "^\\w+ \\d+GB \\d+ port nic .*$",
          "name": "NIC",
          "id": 4
        },
        {
          "pattern": "^\\w+ \\d+W \\d+\\%$",
          "name": "PSU",
          "id": 5
        },
        {
          "pattern": "^\\w+ \\w+ \\w+ \\d+$",
          "name": "RAIDctlr",
          "id": 6
        },
        {
          "pattern": "^\\w+ .+$",
          "name": "fan",
          "id": 9
        },
        {
          "pattern": "^\\w+ \\d+(GB|TB) \\w+ \\d+K .*$",
          "name": "harddrive",
          "id": 10
        },
        {
          "pattern": "^.+ \\d+GB DDR\\d+ \\d+MHz \\d+x\\d+$",
          "name": "memory",
          "id": 11
        }
      ]
    }
    

    vendors.json

    {
      "data": [
        {
          "contact_phone": "555-555-5551",
          "website": "http://www.amtfcorp.com/shop/home.php",
          "contact_email": "email 1",
          "contact_name": "contact 1",
          "name": "Advance Media Technology Frontier Corporation (UnitedMicro)",
          "id": 1,
          "description": "description 1"
        },
        {
          "contact_phone": "555-555-5553",
          "website": "http://www.hp.com",
          "contact_email": "email 3",
          "contact_name": "contact 3",
          "name": "HP",
          "id": 3,
          "description": "description 3"
        },
        {
          "contact_phone": "555-555-5554",
          "website": "http://www.skhynix.com",
          "contact_email": "email 4",
          "contact_name": "contact 4",
          "name": "Hynix",
          "id": 4,
          "description": "description 4"
        },
        {
          "contact_phone": "555-555-5555",
          "website": "http://www.intel.com",
          "contact_email": "email 5",
          "contact_name": "contact 5",
          "name": "Intel",
          "id": 5,
          "description": "description 5"
        },
        {
          "contact_phone": "555-555-5556",
          "website": "http://www.nvidia.com",
          "contact_email": "email 6",
          "contact_name": "contact 6",
          "name": "Internal",
          "id": 6,
          "description": "description 6"
        },
        {
          "contact_phone": "555-555-5557",
          "website": "http://www.micron.com",
          "contact_email": "email 7",
          "contact_name": "contact 7",
          "name": "Micro",
          "id": 7,
          "description": "description 7"
        },
        {
          "contact_phone": "555-555-5558",
          "website": "http://www.nvidia.com",
          "contact_email": "email 8",
          "contact_name": "contact 8",
          "name": "NVidia",
          "id": 8,
          "description": "description 8"
        },
        {
          "contact_phone": "555-555-5559",
          "website": "http://www.quantatw.com/Quanta/english/",
          "contact_email": "email 9",
          "contact_name": "contact 9",
          "name": "Quanta",
          "id": 9,
          "description": "description 9"
        },
        {
          "contact_phone": "555-555-5560",
          "website": "http://www.rackable.com",
          "contact_email": "email 10",
          "contact_name": "contact 10",
          "name": "Rackable",
          "id": 10,
          "description": "description 10"
        },
        {
          "contact_phone": "555-555-5561",
          "website": "http://www.redapt.com",
          "contact_email": "email 11",
          "contact_name": "contact 11",
          "name": "Redapt",
          "id": 11,
          "description": "description 11"
        },
        {
          "contact_phone": "555-555-5562",
          "website": "http://www.samsung.com",
          "contact_email": "email 12",
          "contact_name": "contact 12",
          "name": "Samsung",
          "id": 12,
          "description": "description 12"
        },
        {
          "contact_phone": "555-555-5563",
          "website": "http://www.seagate.com/",
          "contact_email": "email 13",
          "contact_name": "contact 13",
          "name": "Seagate",
          "id": 13,
          "description": "description 13"
        },
        {
          "contact_phone": "555-555-5564",
          "website": "http://www.softnets.com",
          "contact_email": "email 14",
          "contact_name": "contact 14",
          "name": "Softnet",
          "id": 14,
          "description": "description 14"
        },
        {
          "contact_phone": "555-555-5565",
          "website": "http://www.supermicro.com",
          "contact_email": "email 15",
          "contact_name": "contact 15",
          "name": "Supermicro",
          "id": 15,
          "description": "description 15"
        },
        {
          "contact_phone": "555-555-5552",
          "website": "http://www.dell.com",
          "contact_email": "email 2",
          "contact_name": "contact 2",
          "name": "Dell",
          "id": 2,
          "description": "description 2"
        },
        {
          "contact_phone": "555-555-5566",
          "website": "http://does-not-exist",
          "contact_email": "email 16",
          "contact_name": "contact 16",
          "name": "United Microsystems Inc",
          "id": 16,
          "description": "description 16"
        }
      ]
    }
    
  • allanallan Posts: 61,757Questions: 1Answers: 10,111 Site admin

    Inline editing in Editor will display only one field at a time, so you would need to create a plug-in field type that implements the multi-select in a single field.

    The other option to consider is using Bubble editing which is similar to inline editing, but allows multiple fields to be shown.

    Allan

  • avcarrilloavcarrillo Posts: 15Questions: 2Answers: 0

    Hi Allan,

    Do you ever sleep? :)

    I don't need to display more then one dropdown at a time, but I do need to be able to update each one separately. IE click the category dropdown and change it, then click on the vendor and change it.

    Does that make sense?

    Thank you

  • allanallan Posts: 61,757Questions: 1Answers: 10,111 Site admin

    Do you ever sleep? :)

    I've got a 1 year old baby. Does that answer the question? ;-)

    I think I understand, but it still means two fields in the same cell does it not? You need to modify one and then trigger the other. I guess you could potentially use the API to listen for the submit from the category and then immediately edit in the same cell the vendor based on the new information (since Editor will let you edit any data in any cell for the row). Not ideal, and bubble editing would probably be a lot easier since you already have the continuation working, but it would probably work.

    Allan

  • avcarrilloavcarrillo Posts: 15Questions: 2Answers: 0

    It's not two fields in the same cell, it's two different fields in the same row. Or am I misunderstanding the meaning of a cell? In the example above I have <td>category (dropdown)</td><td>Vendor (dropdown)</td>

    I tried switching to bubble editor using

            $('#Parts').on( 'click', 'tbody td:not(:first-child)', function (e) {
                editor.bubble( this );
            } );
    

    But now I'm getting the error:

    Uncaught Unable to automatically determine field from source. Please specify the field name

    When I click on either dropdown. The Name field works as expected, the same as with inline.

    Hope this is clearer. Sorry I don't have a working example for you. I tried live.datatables.net, but there is no editor plugin (or I just couldn't figure out how to make it work)

    Thank you

  • allanallan Posts: 61,757Questions: 1Answers: 10,111 Site admin
    edited November 2014

    Or am I misunderstanding the meaning of a cell?

    cell as in a td or th element was my meaning.

    With inline editing you can edit one field at a time only. So you would update the category first, submit that and then update the vendor in this case.

    For bubble editing, you probably need to specify the fields to edit using the bubble() method's second parameter.

    Allan

  • avcarrilloavcarrillo Posts: 15Questions: 2Answers: 0

    Ok, I have not made my issue clear. I apologize for that.

    The problem is I can only get one of the dropdowns to work and not the other. I have to separate dropdowns, one for Category, and one for Vendor. By clicking on either of them I get the error "Please specify the field name".

    By using this fix

                editor.inline( this, 'category.id', {
                    submitOnBlur: true
                } );
    

    I am able to get the Category dropdown to work, but not the Vendor dropdown which again is a separate cell.

    As a workflow I would visit the page and update the vendor only. With the current fix I am unable to do that.

    I hope that clears up the confusion.

    Thank you Allan

  • avcarrilloavcarrillo Posts: 15Questions: 2Answers: 0

    I'm sure there is a better way of addressing this issue, but I came up with a workaround by using the following

            $('#Parts').on('click', 'tbody td:not(:first-child)', function(e) {
                var cellIndex = e.target.cellIndex;
                var columnID = ['','category.id', 'vendor.id'];
                editor.inline( this, columnID[cellIndex], {
                    submitOnBlur: true
                } );
    
            });
    

    The last question I have is in regards to editField which is referenced on http://datatables.net/manual/tech-notes/11. Do you need Editor 1.4 or higher, and if so when will 1.4 be released.

    Thank you

  • allanallan Posts: 61,757Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Hi,

    Thanks for the clarification - I completely misunderstood! Sorry.

    Good to hear you have a workaround - yes its a bit of a pain at the moment, which is why Editor 1.4. will address that. It should be available at the start of December, although I'm happy to send you a dev copy just now if you would like to use that feature as it has already been implemented.

    Regards,
    Allan

  • avcarrilloavcarrillo Posts: 15Questions: 2Answers: 0

    Yes please.

    Thank you Allan

  • allanallan Posts: 61,757Questions: 1Answers: 10,111 Site admin

    Sent :-).

This discussion has been closed.