Author Topic: Custom editor for array data  (Read 5535 times)

webifi

  • Pro Deluxe
  • Newbie
  • *
  • Posts: 45
    • View Profile
Custom editor for array data
« on: November 29, 2014, 12:25:24 am »
Not sure how to handle cell data that is an array in pqGrid.

I have the following colModel that displays the array data just fine using a custom render, but the custom editor/getData doesn't seem to agree with pqGrid.   Not sure if I'm just misunderstanding how the custom editor should work, or if I just can't use arrays in the column data.

colModel object for array column:
Code: [Select]
                      {
                            title:"Tax Rates in Group",
                            width:'50%',
                            sortable:false,
                            dataIndx: "tax_rates",
                            render: function(ui){
                                return _itemListHtml(ui.rowData.tax_rates);
                            },
                            editable: true,
                            editor: {
                                type: function(ui){
                                    var data = $.extend(true,[],ui.rowData.tax_rates);
                                    var $cell = ui.$cell;
                                    ui.rowData.tax_rates.forEach(function(d){
                                        $cell.append(_itemRemoveButton(d.name, data));
                                    });
                                    $cell.append(_itemAddButton("Add Tax Rate", data));;
                                    $cell.data("pq-itemData",data);
                                },
                                getData: function(ui){
                                    return ui.$cell.data("pq-itemData");
                                }
                            }
                        },

Supporting functions:
Code: [Select]
    function _itemListHtml(itemData){
        var items = [];
        itemData.forEach(function(r){
            items.push('<span style="white-space:nowrap;">'+r.name+'</span>');
        });
        return items.join(', ');
    }
    function _itemRemoveButton(name, data){
        return $('<button class="delete_item_btn">'+name+'</button>').button({
            icons:{
                primary:'ui-icon-close'
            }
        }).bind("click",function(e){
            var $this = $(this);
            data.splice($this.parent().index(),1);
            $this.remove();
        });
    }
    function _itemAddButton(name, data, itemSelection){
        return $('<button class="add_item_btn">'+name+'</button>').button({
            icons:{
                primary:'ui-icon-plus'
            }
        }).bind("click",function(e){
            var $this = $(this);
            var selectedItem = _selectModal(itemSelection,data);
            if(selectedItem){
                data.push(selectedItem);
                $this.before(removeItemButton(selectedItem.name));
            }
        });
    }

getData returns the modified array just fine, but pqGrid just seems to ignore the changes.

webifi

  • Pro Deluxe
  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Custom editor for array data
« Reply #1 on: November 29, 2014, 08:11:22 pm »
It appears that pqGrid cannot handle editing data types of arrays/objects.   After editing such a cell, it seems to convert an array of two object to the string "[object Object],[object Object]", or an array containing one object to "[object Object]".

Is there any hope, or is pqGrid not going to work for this situation?

igorien2k

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Custom editor for array data
« Reply #2 on: December 15, 2014, 06:30:22 am »
I have a similar case where the cell value is a combination of a value and a unit, which I am storing as an object. It displays fine, but does not save on edit. Is this something that is easy for us to add ourselves? This is one of the things that will make or break the evaluation for us

igorien2k

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Custom editor for array data
« Reply #3 on: December 15, 2014, 06:50:51 am »
One thing I've tried and have had moderate success with is storing the "array" cell data as a JSON string, and adding a custom cell render, cell edit, and getData functions which convert to JSON and back. Still seeing how this workaround affects the other features...

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6122
    • View Profile
Re: Custom editor for array data
« Reply #4 on: December 15, 2014, 09:19:39 am »
Storing the array cell data as JSON string might work.

Another way is to utilize data API http://paramquery.com/pro/api#method-data Any kind of data can be stored / associated with a cell with this API.

webifi

  • Pro Deluxe
  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Custom editor for array data
« Reply #5 on: December 15, 2014, 09:10:35 pm »
Storing the array cell data as JSON string might work.

Another way is to utilize data API http://paramquery.com/pro/api#method-data Any kind of data can be stored / associated with a cell with this API.

Does the data API allow use of rollback to undo the changes?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6122
    • View Profile
Re: Custom editor for array data
« Reply #6 on: December 15, 2014, 10:16:02 pm »
Currently rollback, undo, etc is not supported for meta data.