How to avoid using DT_RowId

How to avoid using DT_RowId

dcmrktdcmrkt Posts: 2Questions: 1Answers: 0

I'm sorry for the newbie question but I've already tried to look everywhere for this issue and found nothing.
In the database of the CRUD app I'm trying to build I've already defined a primary key, let's call it 'code'. When I use the prebuilt PHP libraries, the primary key that gets printed on the ajax object is called 'DT_rowId', and not 'code', and the values have a "row_" prefix.
How can I avoid this behaviour? I'd like the data to be 1:1 with the one on the database, both with column name and values.
Thank you in advance

Answers

  • allanallan Posts: 63,441Questions: 1Answers: 10,459 Site admin

    Our Editor PHP libraries will always call the primary key DT_RowId - that is not configurable I'm afraid. Making it configurable has never come up before has never come up before as far as I can recall, although I do know it is nice to have full control over the data.

    The Editor libraries can use any column name as the primary key - it doesn't need to be the default id. It will always call it DT_RowId over the wire though.

    At the client-side, both DataTables and Editor will look for DT_RowId by default. If changed (e.g. using a server-side that is not our published libraries), you'd need to change rowId for DataTables and idSrc for Editor.

    If you are happy adding that extra configuration, and really want to lose the DT_RowId over the wire, then there is a way to do it: use the ->data() method rather than ->json() of the Editor class, and modify the data:

    $data = Editor::inst( $db, 'staff' )
        ->fields( ... )
        ->process( ... )
        ->data();
    
    for ($i=0 ; count($data['data']) ; $i++) {
      $data['data'][$i]['code'] = $data['data'][$i]['DT_RowId'];
      unset($data['data'][$i]['DT_RowId']);
    }
    

    I don't see much advantage though - there isn't any to the end user.

    Allan

  • dcmrktdcmrkt Posts: 2Questions: 1Answers: 0

    Honestly it wouldn't be an issue using DT_RowId if datatables is built this way, but the fact that the data gets a "row_" prefix makes it impossible to edit them, since it loads "row_XXX" and not "XXX".
    I'm wondering if loading the primary key as a field, in addition to the primary key in the constructor, can let me load and edit the data 1:1 with the database letting the DT_RowId values hidden and only for the correct functioning of datatables.
    I'm currently on free trial with Editor, but considering buying the full license if I can set things to work.

  • allanallan Posts: 63,441Questions: 1Answers: 10,459 Site admin

    I forgot to address the point about the row_ prefix - apologies. That can be controlled with the idPrefix method.

    Editor::inst( $db, 'staff' )
        ->idPrefix( '' )
        ->fields( ... )
        ->process( ... )
    

    will remove the prefix. The reason it is there is two fold:

    1. To prevent conflicts if there are two tables on a page. You really don't want two rows with the same ID on the page, so the idea is that you would just use a different prefix to prevent overlap.
    2. Historically HTML4 didn't allow a number as the starting character for an id attribute. HTML5 doesn't have that limitation.

    If you are using the DataTables PHP libraries for both fetching and editing data, it will handle the id prefix for you entirely.

    Allan

Sign In or Register to comment.