Displaying unique ID but not allow editing

Displaying unique ID but not allow editing

waynehultumwaynehultum Posts: 5Questions: 0Answers: 0
edited June 2012 in Editor
Hi,

I'm trying out the editor as a replacement for our paper job book system.

Everything is fine except I would like the id (AUTO_INCREMENT) field to be used as the job number. I would like this to be displayed but not editable.

I can remove the ID field from the add/edit form but when I add or edit i get the following error:

DataTables warning (table id = 'example'): Requested unknown parameter 'id' from the data source for row 69

The ID is not showing in the datatable when I click OK on the error message. When I refresh the page after the error the ID shows.

Could some please help with this.

Kind regards

Wayne.

Code for test page.

[code]

var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "php/browsers.php",
"domTable": "#example",
"fields": [ {
"label": "Browser:",
"name": "browser"
}, {
"label": "Rendering engine:",
"name": "engine"
}, {
"label": "Platform:",
"name": "platform"
}, {
"label": "Version:",
"name": "version"
}, {
"label": "CSS grade:",
"name": "grade"
}
],
"events": {
"onSetData": function (json, setData, action) {
if ( action === "create" || action === "edit" ) {
setData.updated_date = json.data.updated_date;
}
}
}
} );

$('#example').dataTable( {
"sDom": "Tfrtip",
"sAjaxSource": "php/browsers.php",
"aoColumns": [
{ "mDataProp": "id" },
{ "mDataProp": "browser" },
{ "mDataProp": "engine" },
{ "mDataProp": "platform" },
{ "mDataProp": "version", "sClass": "center" },
{ "mDataProp": "grade", "sClass": "center" }
],
"oTableTools": {
"sRowSelect": "multi",
"aButtons": [
{ "sExtends": "editor_create", "editor": editor },
{ "sExtends": "editor_edit", "editor": editor },
{ "sExtends": "editor_remove", "editor": editor }
]
}
} );
} );

[/code]

browsers.php

[code]
$editor = new DTEditor(
$db, // DB resource
'browsers', // DB table
'id', // Primary key
'row_', // ID prefix to add to the TR rows (makes it valid and unique)
array( // Fields
new DTField( array(
"name" => "id",
"dbField" => "id",
"set" => false,
"dynamicGet" => true
) ),
new DTField( array(
"name" => "engine",
"dbField" => "engine",
"validator" => "DTValidate::required"
) ),
new DTField( array(
"name" => "browser",
"dbField" => "browser",
"validator" => "DTValidate::required"
) ),
new DTField( array(
"name" => "platform",
"dbField" => "platform"
) ),
new DTField( array(
"name" => "version",
"dbField" => "version"
) ),
new DTField( array(
"name" => "grade",
"dbField" => "grade",
"validator" => "DTValidate::required"
) )
)
);

// The "process" method will handle data get, create, edit and delete
// requests from the client
echo json_encode( $editor->process($_POST) );
[/code]

Could

Replies

  • allanallan Posts: 63,573Questions: 1Answers: 10,483 Site admin
    Hi Wayne,

    There are a few options open to you here for how this could be achieved.

    1. You could add a "readonly" field to your Editor initialisation - something like:

    [code]
    {
    "label": "Job number:",
    "name": "id",
    "type": "readonly"
    }
    [/code]

    Then using the Editor PHP class you should add "set" => false, to the 'id' field input, so that DTEditor doesn't try to set the value of that column!

    2. Very similar to the above, but rather than using a readonly field, you could disable the field when it is shown:

    [code]
    editor.disable('id');
    [/code]

    You'd still need to tell DTEditor in PHP to ignore the set for this field.

    One other point you might want to consider - you could use the onOpen event + the show/hide API methods to show and hide the Job number ID field (presumably since it is auto generated, there is little use in showing it in the 'create' form (unless you want to show a message saying that it will be auto generated - the same approach applies as this):

    [code]
    editor.on('onShow', function() {
    if ( this.s.action === 'create' ) {
    this.hide('id');
    }
    else {
    this.show('id');
    }
    } );
    [/code]

    Editor 1.1 will have onInitCreate etc events to make this a little bit clearer in future. Editor 1.1 will be out int he next few days :-)

    Regards,
    Allan
  • waynehultumwaynehultum Posts: 5Questions: 0Answers: 0
    Thanks for the quick response Allan,

    Both option 1 and 2 stop the error and allow the record to be inserted/edited.

    However when a record is inserted the ID is not shown in the datatable until it's refreshed.

    Is there a way to refresh the datatable after inserting? or is there another method to use.

    Regards

    Wayne.
  • allanallan Posts: 63,573Questions: 1Answers: 10,483 Site admin
    Form the code above it looks like you are most of the way there, but you need to use 'id' as a dynamicGet, very much like how you currently have "updated_date" being handled at the moment. The need for that is because Editor will use the local data to add to the DataTable, rather than requiring that data be sent back from the server - but it is possible using dynamic gets and setting the property value as you have for "updated_date".

    Allan
  • waynehultumwaynehultum Posts: 5Questions: 0Answers: 0
    Thank you very much, that's exactly what I wanted.
This discussion has been closed.