Help with filling Datatable Editor Fields

Help with filling Datatable Editor Fields

nkitchen66nkitchen66 Posts: 7Questions: 3Answers: 0
edited April 2020 in Free community support

Hello,

I've been racking my brain trying to get select boxes to work in Datatables Editor. I can get the values to show up in Datatables, but it is not selecting the correct item when clicking on Edit. Attached is the javascript and a sample of the JSON (the full JSON is really big). I realize that I only have one field entered in the editor right now but it's just so I can figure out what is going on and I can wire up the other two. Thank you very much for any insight.

Javascript

       var student_id = "#customer_userrole_results"

        editor = new $.fn.dataTable.Editor( {
            ajax: "router.php?page=customercare-ajax_userrole&student_id="+$('#customer_userrole_results').data('student'),
            table: "#customer_userrole_results",
            fields: [ {
                label: "Site Name",
                name:  "users.site_id_i",
                type:  "select",
                options: "users.role"
            }
            ]
        } );

        var table = $('#customer_userrole_results').DataTable({
            "dom": 'Bfrtip',
            "displayLength": 10,
            ajax: "router.php?page=customercare-ajax_userrole&student_id="+$('#customer_userrole_results').data('student'),
            columns: [
                { data: "site.name_vc"},
                { data: "userrole.rolename_vc" },
                {
                    data: "token",
                    render: "[, ].description_vc",
                    editField: "users.token"
                }
            ],
            select: {
                style: 'os',
                selector: 'td:first-child'
            },
            buttons: [
                { extend: "create", editor: editor },
                { extend: "edit",   editor: editor },
                { extend: "remove", editor: editor }
            ]
        });

JSON Load

{
  "data": [
    {
      "DT_RowId": "187267",
      "users": [
        {
          "site_id_i": "100",
          "role_id_i": "100"
        }
      ],
      "site": {
        "id_i": "100",
        "name_vc": "My Company"
      },
      "userrole": {
        "id_i": "100",
        "rolename_vc": "CUSTOMER"
      },
      "token": [
        {
          "token_id_i": null,
          "tokenname_vc": null,
          "description_vc": null
        }
      ]
    },
    {
      "DT_RowId": "188730",
      "users": [
        {
          "site_id_i": "100",
          "role_id_i": "101"
        }
      ],
      "site": {
        "id_i": "100",
        "name_vc": "My Company"
      },
      "userrole": {
        "id_i": "101",
        "rolename_vc": "ADMIN"
      },
      "token": [
        {
          "token_id_i": null,
          "tokenname_vc": null,
          "description_vc": null
        }
      ]
    },
    {
      "DT_RowId": "188733",
      "users": [
        {
          "site_id_i": "100",
          "role_id_i": "103"
        }
      ],
      "site": {
        "id_i": "100",
        "name_vc": "My Company"
      },
      "userrole": {
        "id_i": "103",
        "rolename_vc": "ROLE_ADMIN"
      },
      "token": [
        {
          "token_id_i": null,
          "tokenname_vc": null,
          "description_vc": null
        }
      ]
    },
    {
      "DT_RowId": "189827",
      "users": [
        {
          "site_id_i": "100",
          "role_id_i": "105"
        }
      ],
      "site": {
        "id_i": "100",
        "name_vc": "My Company"
      },
      "userrole": {
        "id_i": "105",
        "rolename_vc": "DASHBOARD_ADMIN"
      },
      "token": [
        {
          "token_id_i": null,
          "tokenname_vc": null,
          "description_vc": null
        }
      ]
    },
    {
      "DT_RowId": "194598",
      "users": [
        {
          "site_id_i": "100",
          "role_id_i": "1"
        }
      ],
      "site": {
        "id_i": "100",
        "name_vc": "My Company"
      },
      "userrole": {
        "id_i": "1",
        "rolename_vc": "DASHBOARD_SYS_ADMIN"
      },
      "token": [
        {
          "token_id_i": null,
          "tokenname_vc": null,
          "description_vc": null
        }
      ]
    },
    {
      "DT_RowId": "373076",
      "users": [
        {
          "site_id_i": "1120",
          "role_id_i": "104"
        }
      ],
      "site": {
        "id_i": "1120",
        "name_vc": "REMAX Realty Group"
      },
      "userrole": {
        "id_i": "104",
        "rolename_vc": "ROLE_AFFIL_ADMIN"
      },
      "token": [
        {
          "token_id_i": "125",
          "tokenname_vc": "PRE_TOUCHPOINT_EMAIL",
          "description_vc": "Prelicense Touchpoint Email"
        }
      ]
    },
    {
      "DT_RowId": "643441",
      "users": [
        {
          "site_id_i": "100",
          "role_id_i": "206"
        }
      ],
      "site": {
        "id_i": "100",
        "name_vc": "My Company"
      },
      "userrole": {
        "id_i": "206",
        "rolename_vc": "ADMIN-Edit"
      },
      "token": [
        {
          "token_id_i": null,
          "tokenname_vc": null,
          "description_vc": null
        }
      ]
    }
  ],
  "options": {
    "users.site": [
      {
        "value": "98",
        "label": "98 - My Company - SRS"
      },
      {
        "value": "100",
        "label": "100 - My Company"
      },
      {
        "value": "102",
        "label": "102 - Company2"
      },
      {
        "value": "103",
        "label": "103 - Company3"
      },
      {
        "value": "128",
        "label": "128 - Company4"
      },
      {
        "value": "129",
        "label": "129 - Company5"
      },
      {
        "value": "2716",
        "label": "2716 - Company6"
      }
    ],
    "users.role": [
      {
        "value": "1",
        "label": "1 - DASHBOARD_SYS_ADMIN"
      },
      {
        "value": "100",
        "label": "100 - CUSTOMER"
      },
      {
        "value": "101",
        "label": "101 - ADMIN"
      },
      {
        "value": "102",
        "label": "102 - AFFILADMIN"
      },
      {
        "value": "103",
        "label": "103 - ROLE_ADMIN"
      },
      {
        "value": "204",
        "label": "204 - DASHBOARD_GIFTCARDS"
      },
      {
        "value": "205",
        "label": "205 - COMPLIANCE_ADMIN"
      },
      {
        "value": "206",
        "label": "206 - ADMIN-Edit"
      }
    ],
    "users.token": [
      {
        "value": "100",
        "label": "AFFILADMIN_COMMISSIONREPORTING",
        "description_vc": ""
      },
      {
        "value": "101",
        "label": "AFFILADMIN_COMMISSIONREPORTINGNAME",
        "description_vc": ""
      },
      {
        "value": "102",
        "label": "AFFILADMIN_COMMISSIONREPORTINGEMAIL",
        "description_vc": ""
      },
      {
        "value": "103",
        "label": "STUDENT_HISTORY",
        "description_vc": "Student History"
      },
      {
        "value": "104",
        "label": "CURRENT_COMMISSION",
        "description_vc": "Current Commission"
      },
      {
        "value": "105",
        "label": "COMMISSION_HISTORY",
        "description_vc": "Commission Hist."
      },
      {
        "value": "106",
        "label": "COMMISSION_EMAIL",
        "description_vc": "Commission Email"
      },
      {
        "value": "107",
        "label": "COMPLETION_EMAIL",
        "description_vc": "Completion Email"
      },
      {
        "value": "108",
        "label": "CC_COMPLETION",
        "description_vc": "Copy of Completion Email (Ethics)"
      },
      {
        "value": "109",
        "label": "ACHIEVERS_CLUB",
        "description_vc": "Achiever's Club"
      },
      {
        "value": "111",
        "label": "MANAGER_ACTIVITY_REPORT",
        "description_vc": "Manager Activity Report"
      },
      {
        "value": "112",
        "label": "ORDER_REPORT",
        "description_vc": "Order Report"
      },
      {
        "value": "113",
        "label": "STATE_COMPLETION_REPORT",
        "description_vc": "State Completion Report"
      },
      {
        "value": "114",
        "label": "SALESAGENT",
        "description_vc": "Sales Agent Manager"
      },
      {
        "value": "115",
        "label": "PRELICENSING_EMAIL",
        "description_vc": "Pre-Licensing Order Notification Email"
      },
      {
        "value": "116",
        "label": "RECEIPT_BCC_EMAIL_LEAP_COURSETYPE:1",
        "description_vc": "Receipt BCC Email - SalespersonCE"
      },
      {
        "value": "117",
        "label": "RECEIPT_BCC_EMAIL_LEAP_COURSETYPE:2",
        "description_vc": "Receipt BCC Email - BrokerCE"
      }
    ]
  },
  "files": null
}

Answers

  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin

    Hi,

    Thanks for the details. options: "users.role" this isn't going to work I'm afraid. The options property is only used for specifying an array/object of options - not telling Editor where to get the options from.

    Editor will look in the Ajax loaded data for options with the same name as the field name. So you'd need to use the field name users.site_id_i in the options object for the JSON return.

    Allan

  • nkitchen66nkitchen66 Posts: 7Questions: 3Answers: 0

    Allan,

    Thank you so much for getting back so quick. I had tried that before but get this Cannot use 'in' operator to search for 'length' in users.site_id_i error that pops up. I looked and saw your thread regarding it at https://datatables.net/forums/discussion/44524/uncaught-typeerror-cannot-use-in-operator-to-search-for-length but could not understand how and where to implement the fix you recommended.

  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin

    Can you show me the server-side code you are using please? I don't see anything in the above about using an IN operator? If you are using it as a where condition, then this part of the manual might be of some interest.

    Regards,
    Allan

  • nkitchen66nkitchen66 Posts: 7Questions: 3Answers: 0

    I'm not using the Editor sql calls as I already utilize another framework for sql. The 'IN' error that is being passed is relating jquery and having an issue there.

    This is the query I am using on the server which returns the correct values:

    select site_id_i as value, concat(id_i, ' - ', name_vc) as label from site where active_b = 1

  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin

    I'm afraid I don't understand how jQuery relates to your SQL query? Are you passing the query to the server? If so, that's an SQL attack waiting to happen.

    Allan

This discussion has been closed.