Prevent editor from changing column with non-existent value

Prevent editor from changing column with non-existent value

jmack44jmack44 Posts: 5Questions: 2Answers: 0

I'm wondering if there is an ability to hide specific options of a select box in DataTables Editor.

I'm mainly doing this because if I edit a row where column has a non-existent value, then editor automatically sets said column to the first value of the available options. Hopefully my code will clear things up:

editor = new $.fn.dataTable.Editor({
      ajax: "/datatables/api/customer/proposals.php",
      table: "#example",
      fields: [{
        label: "Contract Type:",
        name: "proposals.contractid",
        type: "select"
      }, {
        label: "Title:",
        name: "proposals.title",
        type: "text"
      }, {
        label: "Status:",
        name: "proposals.status",
        type: "select",
        options: [{
          label: "Pending",
          value: "Pending",
        }, {
          label: "Rejected",
          value: "Rejected",
        }]
      }, {
      ...

There is also another option for the field proposals.status that is "Approved". However I don't want users to be able to set this as an option via the table.

Currently, when editing a row that is "Approved", editing any field within the row will also set the status to "Pending".

I'm looking to retain the value of "Approved", and have any other data in the row be editable.

There may be a better solution to this than a hidden option, I just thought maybe it would recognize it as a valid value if it was defined but not enabled. I currently cannot find anything in the documentation for support of hidden values, just hidden columns

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    A better options perhaps would be to disable the select when the record's value isn't available in the options, i.e. 'Approved'.

    Something like this might do the trick. Here, if the office isn't Tokyo or London (the two configured options for the select), then the office field is disabled.

    Colin

  • jmack44jmack44 Posts: 5Questions: 2Answers: 0

    Unfortunately with the select disabled it still sends the default option to the server. I've updated a jsbin snippet to more accurately convey my setup.

    Now when changing the "Position" of a row that isn't Tokyo or London, the value of Office (and Start Date) are inadvertently changed

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Apologies - I've been working on moving the live server today and there has been a window of data loss from the database for it. It looks like your change has fallen into that - sorry!

    From your description, I would probably use a server-side event to make the Office and Start Date columns not writeable if you don't want them to be. For example:

    ->on('preEdit', function ($editor, $id, $values) {
      if ($values['proposals']['status'] === 'something') {
        $editor->field('proposals.title')->set(false);
      }
    })
    

    Allan

  • jmack44jmack44 Posts: 5Questions: 2Answers: 0

    No worries, I can't seem to get the table to jsbin to initialize the datatable properly. I'll keep tinkering with it and leave a follow up comment when I get it.

    Because I still want the status field to be editable (E.g., the status can still go from "Approved" to "Pending") on the back end I can't check if the value was changed on purpose or accident, unless I can also pass a flag.

    Is there a client side solution for this where I have the select option present but not visible or enabled?

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Without a bunch of workarounds, I don't think there is an easy solution for this at the moment. However, we do have the fields.submit option which can be used to control if a field should submit or not. There isn't an API method for that, but perhaps I should add that. Then you could call editor.field(...).submit(true/false) to indicate if you want it to be included in the next submit or not. Would that work for you?

    Allan

  • jmack44jmack44 Posts: 5Questions: 2Answers: 0

    After more thought, you're original suggestion for a server side solution will be the best option.

    I've decided to show all available options in the select and if chosen, the server side script will validate the selection within the preEdit event.

    Thank you for your help

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Awesome - thanks for the update!

    Allan

Sign In or Register to comment.