I want to insert row at specific location in datagrid2

0
I have and data grid and all rows are editable inline and when object is created a blank object is created and row is inserted at top. What I want is I want to inster it at specifc location basically after selcted row.
asked
3 answers
0

Hi Rajan,


If the requirement is to insert the newly created object at a particular position in the Data Grid 2, I would not try to control the physical row position directly.

Data Grid 2 displays the objects according to the data source and its sort order. So the reliable approach is to add an attribute that represents the required order/position and sort the grid on that attribute.

For example, add an Integer attribute such as:

SortOrder

When the user selects a row and clicks New:

  1. Get the selected row.
  2. Determine its current SortOrder.
  3. Set the new object's SortOrder to the position where you want it.
  4. If necessary, increase the SortOrder of the following objects.
  5. Commit the objects.
  6. Configure Data Grid 2 to sort by SortOrder ascending.

For example:

Existing rows:

A   SortOrder = 1
B   SortOrder = 2
C   SortOrder = 3

If the user selects B and creates a new row after it:

A   SortOrder = 1
B   SortOrder = 2
New SortOrder = 3
C   SortOrder = 4

Then the grid will display:

A
B
New
C

You can also avoid renumbering every record by using gaps, for example 100, 200, 300. A new row between 100 and 200 can initially get 150. You only need to renumber when there is no longer enough space between two positions.

One important point: if you don't configure a sort order, Mendix does not provide a reliable "insert at selected row" concept. Without a sort order, objects are shown in creation order.

So I would model the requirement around a persistent SortOrder/Position attribute rather than trying to manipulate the Data Grid 2 UI itself.

If the user only needs the newly created row to appear immediately below the selected row visually, but you don't want to maintain a position attribute in the database, then there isn't a supported Data Grid 2 property that directly inserts a row at an arbitrary UI index. In that case, the ordering needs to come from the data source.

answered
0

Rajan,

You will need to use a microflow for this, no the out of the box Create functionality.


Create a microflow that accepts a parameter of the object type in your Grid. When the microflow executes, create a new object and set an attribute of that object to an appropriate value so that the new object appears in the grid after the selected row.


You may need to change the attribute for objects after the selected row in order for the rows to appear in the order you want them to.


Hope that helps,

Mike

answered
0

Is your datagrid sorted? If so by what attribute? Also how many rows do you have? I would suggest adding an attribute OrderIndex, then run a microflow to patch all your rows so they are ordered properly. Use an index of multiples of 10 (10, 20, 30, ...) instead of 1 (1, 2, 3...). That way when you create a new row you can just set that attribute to be 1 after the previous OrderIndex. You'll need a microflow or nanoflow for that though.


For example:


  1. You have a table of Tasks. It is orderd by OrderIndex.
  2. Task3 has an OrderIndex of 30 and shows up third on the list.
  3. When you select that Task3 and click New Task, it would trigger a microflow or nanoflow that passes in the selected Task3 as a parameter.
  4. You then read the OrderIndex off of that selected Task3 (30), create a new task with an OrderIndex of 31. So when you commit and refresh table, it will show up right after that other one.


At some point you may want to normalize it or do that on a scheduled task so that it goes through your entire data set and resets the current order to indexes of 10 again so that sample new task would now have an index of 40 and everything after would go 50, 60, 70, etc. You wouldn't want to do that on the initial new index creation though because that could be potentially really slow if you have a lot of rows. Better to do as a batch in the background at the end of the day or something like that.


Hope that helps!

answered