Adding a new row

Adding a new row

peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
edited November 2010 in General
I know that fnAddData() or fnAddDataAndDisplay() can be used to insert new data to the table, but I'm trying to figure out the best interface for the user. I want to do something like open a row and display empty fields that the user can fill in. Once I have the data, I can call fnAddData() to add it properly (and possibly sort it).

But I don't want to open the last row on the screen. I would rather have it be the first row on the screen. I.e., after the last row on the previous page (or the very first row, if on the first page).
How can I figure out what row is on the top of the page (i.e., an index)? I could then use fnDisplayStart to move the previous row to the top of the screen and then do an fnOpen() on that.

Is there any other way to open up a skeleton row?

Any thoughts?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Can't you just use a jQuery selector to get the first visible row in the table? $('#example tbody tr:eq(0)')[0] for example. Pass that into fnOpen and it will always open the top row in the table. You can use the :last (or any other) selector if you want to open a different row.

    However, I'm thinking that you want to insert a row at the very top of the table - is that right? That's not something fnOpen will do - it will always open a row just under the parent. What you could do is insert a TR element manually and that will do the job fine (a bit like what fnOpen does). It would be lost on any redraw, unless you take steps to prevent that, but it's one option. Personally I quite using a lightbox for a CRUD style interface with DataTables.

    Allan
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    Exactly. I want to open it as the first row of the table, not the 2nd row. I thought about changing the code to allow fnOpen to open a row *before* the given row, but it's not as easy as I thought it would be.
    I have no problem having fnOpen open the row after the parent. But I want the parent to be the last row on the previous page.
    But I like the idea of inserting a TR manually. I wasn't sure if that would cause any problems. That's actually perfect behavior if it will be lost on the redraw as I'll do the redraw as soon as I call fnAddData.

    When you say that you use a lightbox, you mean that you use pop up a dialog? I thought of that, but for some reason, didn't like the idea. One thing that I didn't like about lightbox is that it's not draggable. The user might want to enter new data by looking at some of the existing data.

    Do you have any examples?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    > I have no problem having fnOpen open the row after the parent. But I want the parent to be the last row on the previous page.

    It doesn't work quite like this - if you fnOpen the last row in a page, it will still be shown on that page - not on the next one (as can be seen in this example: http://datatables.net/examples/api/row_details.html ).

    > Lightbox
    > Do you have any examples?

    Not any which are publicly available I'm afraid. Whether it is suitable or not for a project depends on the UI that you intend to create. Typically I've used Fancybox ( http://fancybox.net/ ) and that's been working well for me, although I have also done something a bit like you are intending (available not public code I'm afraid) and that also works well in that setting. It just depends on what you want to create :-)

    Allan
This discussion has been closed.