Sorting shinobidatagrid on reload


#1

how can i sort my shinobigrid in descending order when im done updating the value.

//
// DelEquipments.m
// TMS
//
// Created by Ariel Marlon Javier on 11/17/13.
// Copyright (c) 2013 Procuro. All rights reserved.
//

#import "DelEquipments.h"
#import "ProductGroupDataSource.h"
#import "DelEquipment.h"
#import "GlobalObjects.h"
#import "PIMMGlobals.h"
#import "Comments.h"
@interface DelEquipments ()

@end

@implementation DelEquipments

NSArray *_data;
NSMutableArray *_sortedData;
@synthesize keypad;

CGRect _realBounds;
NSInteger summaryLineIndex;
NSInteger rowIndicator;
NSInteger rowSelected;
NSInteger rowSelectedPickUp;
SDataGridCoord *_editingCell;
NSMutableArray *_modifiedEquipment;
@synthesize btnComment;

NSString *strIn, *strOut, *strNet;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)addSummaryLine
{
    
    
    DelEquipment *delequipment=[[DelEquipment alloc]initWithTempValue:@"" equipmentID:@"" dropOff:@"" pickUp:@"" net:@""];
    [_sortedData addObject:delequipment];
    summaryLineIndex=[globArrayEquipmentTypes count];
    [_shinobiDataGrid reload];
    // }
}
- (void)viewDidLoad
{
    
    [super viewDidLoad];
    _realBounds = self.view.bounds;
    [self renderDeliveryEquipment];
    rowIndicator=0;
    rowSelected=0;
    rowSelectedPickUp=0;
    _modifiedEquipment =[[NSMutableArray alloc]init];
    
    
    if ([globDelEquipmentComments length]>0) {
        btnComment.selected=YES;
        
    }
    else
    {
           btnComment.selected=NO;
    }

    
    // Do any additional setup after loading the view.
}
-(void)renderDeliveryEquipment
{
    _shinobiDataGrid=[[ShinobiDataGrid alloc]initWithFrame:CGRectMake(20, 130, 700, 270)];
    
    SDataGridTheme *verticalLinesTheme =[SDataGridiOS7Theme new];
    
    
    verticalLinesTheme.hideVerticalGridlines = NO;
    [_shinobiDataGrid applyTheme:verticalLinesTheme];
    
    
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:16.0f];
    UIFont *fontHeader = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
    
    // Set the padding for the cell.
    UIEdgeInsets padding = UIEdgeInsetsMake(5, 5, 5, 5);
    _shinobiDataGrid.singleTapEventMask = SDataGridEventEdit;
    
    //create cell style for input
    
    
    // Create an alternating row style.
    SDataGridCellStyle* alternatingRowStyle = [[SDataGridCellStyle alloc] init];
    alternatingRowStyle.font = font;
    alternatingRowStyle.contentInset = padding;
    alternatingRowStyle.backgroundColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
    
    // Create the selected row style.
    SDataGridCellStyle* selectedRowStyle = [[SDataGridCellStyle alloc] init];
    selectedRowStyle.font = font;
    
    selectedRowStyle.contentInset = padding;
    selectedRowStyle.backgroundColor = [UIColor orangeColor];
    selectedRowStyle.textColor = [UIColor whiteColor];
    
    // Create the header style.
    // Style the headers to have a dark background, and white text.
    SDataGridCellStyle* headerStyle = [[SDataGridCellStyle alloc] init];
    headerStyle.font = fontHeader;
    headerStyle.contentInset = padding;
    headerStyle.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
    headerStyle.textColor = [UIColor whiteColor];
    
    //[deliveryEquipment.layer setCornerRadius:10.f]
    [_shinobiDataGrid.layer setCornerRadius:4.f];
    // Set the row heights.
    _shinobiDataGrid.defaultHeaderRowHeight = @47;
    _shinobiDataGrid.defaultRowHeight = @37;
    
    // Set the cell styles.
    _shinobiDataGrid.defaultCellStyleForHeaderRow = headerStyle;
    _shinobiDataGrid.defaultCellStyleForSelectedRows = selectedRowStyle;
    _shinobiDataGrid.defaultCellStyleForAlternateRows = alternatingRowStyle;
    
    // Create the style for the cell.
    SDataGridCellStyle* cellStyle;
    
    // Create the columns.
    SDataGridColumn* column;
    column = [[SDataGridColumn alloc] initWithTitle:@"Equipment ID"];
    column.editable = NO;
    column.width = @145;
    column.headerCellStyle.contentInset = padding;
    column.headerCellStyle.textAlignment = NSTextAlignmentCenter;
    
    cellStyle = [[SDataGridCellStyle alloc] init];
    cellStyle.font = font;
    cellStyle.contentInset = padding;
    cellStyle.textAlignment = NSTextAlignmentCenter;
    column.cellStyle = cellStyle;
    
    [_shinobiDataGrid addColumn:column];
    
    column = [[SDataGridColumn alloc] initWithTitle:@"Equipment Name"];
    column.editable = NO;
    column.width = @220;
    column.headerCellStyle.contentInset = padding;
    column.headerCellStyle.textAlignment = NSTextAlignmentCenter;
    
    cellStyle = [[SDataGridCellStyle alloc] init];
    cellStyle.font = font;
    cellStyle.contentInset = padding;
    cellStyle.textAlignment = NSTextAlignmentCenter;
    column.cellStyle = cellStyle;
    
    [_shinobiDataGrid addColumn:column];
    
    // *************************************
    // Uom column
    // *************************************
    column = [[SDataGridColumn alloc] initWithTitle:@"Drop Off"];
    //forenameColumn.sortMode = SDataGridColumnSortModeTriState;
    
   
    column.editable = YES;
    column.width = @110;
    column.headerCellStyle.contentInset = padding;
    column.headerCellStyle.textAlignment = NSTextAlignmentCenter;
    
    cellStyle = [[SDataGridCellStyle alloc] init];
    cellStyle.font = font;
    cellStyle.contentInset = padding;
    cellStyle.textAlignment = NSTextAlignmentLeft;
    column.cellStyle = cellStyle;
    [_shinobiDataGrid addColumn:column];
    
    column = [[SDataGridColumn alloc] initWithTitle:@"Pick Up"];
    column.editable = YES;
    column.width = @110;
    column.headerCellStyle.contentInset = padding;
    column.headerCellStyle.textAlignment = NSTextAlignmentCenter;
    
    cellStyle = [[SDataGridCellStyle alloc] init];
    cellStyle.font = font;
    cellStyle.contentInset = padding;
    cellStyle.textAlignment = NSTextAlignmentLeft;
    column.cellStyle = cellStyle;
    [_shinobiDataGrid addColumn:column];
    
    column = [[SDataGridColumn alloc] initWithTitle:@"Net"];
    column.editable = NO;
    column.width = @110;
    column.headerCellStyle.contentInset = padding;
    column.headerCellStyle.textAlignment = NSTextAlignmentCenter;
    
    cellStyle = [[SDataGridCellStyle alloc] init];
    cellStyle.font = font;
    cellStyle.contentInset = padding;
    cellStyle.textAlignment = NSTextAlignmentLeft;
    column.cellStyle = cellStyle;
    [_shinobiDataGrid addColumn:column];
    
    _shinobiDataGrid.dataSource = self;
    _shinobiDataGrid.delegate = self;
    
    
    _data=[ProductGroupDataSource getEquipmentInfo:globDeliveryDictionary];
    //_sortedData=[PIMMGlobals sortArray:_data sortBy:@"DropOffs"];
    _sortedData=[NSMutableArray arrayWithArray:_data];
    //_sortedData=[NSArray arrayWithArray:_data];
    [self addSummaryLine];
    [self.view addSubview:_shinobiDataGrid];
}
-(NSUInteger)shinobiDataGrid:(ShinobiDataGrid *)grid numberOfRowsInSection:(int)sectionIndex
{
    return _sortedData.count;
}
- (void)shinobiDataGrid:(ShinobiDataGrid *)grid didReorderRow:(SDataGridRow *)rowSwitched withRow:(SDataGridRow *)rowSwitchedWith
{
    // clone the sorted data into a mutable array
    NSMutableArray* sortedData = [[NSMutableArray alloc] initWithArray:_sortedData];
    
    // perform the switch
    [sortedData exchangeObjectAtIndex:rowSwitched.rowIndex withObjectAtIndex:rowSwitchedWith.rowIndex];
    
    // copy back to the sorted data property
    _sortedData = [[NSMutableArray alloc] initWithArray:sortedData];
}
- (void)shinobiDataGrid:(ShinobiDataGrid *)grid prepareCellForDisplay:(SDataGridCell *)cell
{
    // both columns use a SDataGridTextCell, so we are safe to perform this cast
    SDataGridTextCell* textCell = (SDataGridTextCell*)cell;
    
    // locate the stock
    DelEquipment *delequipment=_sortedData[cell.coordinate.row.rowIndex];
    textCell.textField.text=@"";
    // NSLog(@"delequipment %@" , delequipment.EquipmentID);
    textCell.textField.borderStyle=UITextBorderStyleNone;
    
    if ([cell.coordinate.column.title isEqualToString:@"Equipment ID"]) {
        textCell.textField.text=[NSString stringWithFormat:@"%@", delequipment.EquipmentID];
        
    }
    
    if ([cell.coordinate.column.title isEqualToString:@"Equipment Name"]) {
        textCell.textField.text=delequipment.EquipmentName;
        
    }
    
    if ([cell.coordinate.column.title isEqualToString:@"Drop Off"] && ! (cell.coordinate.row.rowIndex==summaryLineIndex)) {
        
        textCell.textField.text=delequipment.DropOffs;
        textCell.textField.borderStyle=UITextBorderStyleRoundedRect;
        
        
        
    }
    if ([cell.coordinate.column.title isEqualToString:@"Pick Up"] && ! (cell.coordinate.row.rowIndex==summaryLineIndex)) {
        
        textCell.textField.text=delequipment.PickUp;
        textCell.textField.borderStyle=UITextBorderStyleRoundedRect;
    }
    if ([cell.coordinate.column.title isEqualToString:@"Net"]&& ! (cell.coordinate.row.rowIndex==summaryLineIndex)) {
        
        NSUInteger pickup=[delequipment.PickUp integerValue];
        NSUInteger dropoff=[delequipment.DropOffs integerValue];
       NSUInteger net=pickup-dropoff;
        
        textCell.textField.text=[NSString stringWithFormat:@"%d", net];
        textCell.textField.textAlignment=UITextAlignmentRight;
        
    }
    
    if ([cell.coordinate.column.title isEqualToString:@"Equipment ID"]&& cell.coordinate.row.rowIndex==summaryLineIndex) {
        
        textCell.textField.text=@"SUMMARY";
        UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
        textCell.textField.font=font;
        textCell.textField.enabled=NO;
        textCell.textField.userInteractionEnabled=NO;
        
    }
    
    
    if ([cell.coordinate.column.title isEqualToString:@"Drop Off"] && (cell.coordinate.row.rowIndex==summaryLineIndex)) {
        
        
        UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
        textCell.textField.font=font;
        textCell.textField.enabled=NO;
        textCell.textField.userInteractionEnabled=NO;
        NSInteger totalDOff=0;
        for (int x=0; x<[_sortedData count]; x++) {
            totalDOff=totalDOff+[[[_sortedData objectAtIndex:x]valueForKey:@"DropOffs"]integerValue];
        }
        strOut=[NSString stringWithFormat:@"%d", totalDOff];

        textCell.textField.text=[NSString stringWithFormat:@"%d",totalDOff];
        
    }
    if ([cell.coordinate.column.title isEqualToString:@"Pick Up"] && (cell.coordinate.row.rowIndex==summaryLineIndex)) {
        
        
        UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
        textCell.textField.font=font;
        textCell.textField.enabled=NO;
        textCell.textField.userInteractionEnabled=NO;
        NSInteger totalDOff=0;
        for (int x=0; x<[_sortedData count]; x++) {
            totalDOff=totalDOff+[[[_sortedData objectAtIndex:x]valueForKey:@"PickUp"]integerValue];
        }
        strIn=[NSString stringWithFormat:@"%d", totalDOff];
        textCell.textField.text=[NSString stringWithFormat:@"%d",totalDOff];
        
    }
    if ([cell.coordinate.column.title isEqualToString:@"Net"] && (cell.coordinate.row.rowIndex==summaryLineIndex)) {
        
        
        UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
        textCell.textField.font=font;
        textCell.textField.enabled=NO;
        textCell.textField.userInteractionEnabled=NO;
        NSInteger totalDOff=0;
        for (int x=0; x<summaryLineIndex; x++) {
            
            
            totalDOff=totalDOff+[[[_sortedData objectAtIndex:x]valueForKey:@"Net"]integerValue];
        }
        
        strNet=[NSString stringWithFormat:@"%d", totalDOff];
        
        textCell.textField.text=[NSString stringWithFormat:@"%d",totalDOff];
        // textCell.textField.text=[NSString stringWithFormat:@"%d",67];
        
        
    }
    
    
    
}




- (BOOL)shinobiDataGrid:(ShinobiDataGrid *)grid shouldBeginEditingCellAtCoordinate:(const SDataGridCoord *)coordinate {
    
    _editingCell=nil;
    _editingCell = (SDataGridCoord *)coordinate;
    
    // Get the cell that was tapped.
    SDataGridCell *cell = (SDataGridCell*)[grid visibleCellAtCoordinate:_editingCell];
    
    
    // Get the frame of the tapped cell
    // CGRect cellFrame = cell.frame;
    if ([_editingCell.column.title isEqualToString:@"Drop Off"]) {
        if (self.pop) {
            [self.pop dismissPopoverAnimated:YES];
        }
        cell.backgroundColor=[UIColor lightGrayColor];
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        Keypad *controller = [storyBoard instantiateViewControllerWithIdentifier:@"Keypad"];
        controller.delegate=self;
        UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:controller];
        popover.delegate = self;
        popover.popoverContentSize = CGSizeMake(250,300);
        
        self.pop=popover;
        
        [popover presentPopoverFromRect:cell.bounds inView:cell permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        
        
    }
    
    if ([_editingCell.column.title isEqualToString:@"Pick Up"]) {
        if (self.pop) {
            [self.pop dismissPopoverAnimated:YES];
        }
        cell.backgroundColor=[UIColor lightGrayColor];
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        Keypad *controller = [storyBoard instantiateViewControllerWithIdentifier:@"Keypad"];
        controller.delegate=self;
        UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:controller];
        popover.delegate = self;
        popover.popoverContentSize = CGSizeMake(250,300);
        
        self.pop=popover;
        
        [popover presentPopoverFromRect:cell.bounds inView:cell permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        
        
    }
    
    
    return NO;
    
}



-(void)dismissKeypad:(NSString *)value
{
    
    
    SDataGridTextCell *cell = (SDataGridTextCell*)[_shinobiDataGrid visibleCellAtCoordinate:_editingCell];
    
    DelEquipment *delequipment=_sortedData[cell.coordinate.row.rowIndex];
    NSString *strDropOff;
    NSString *strPickUp;
    
    
    if ([_editingCell.column.title isEqualToString:@"Drop Off"]) {
        cell.textField.text=value;
        delequipment.DropOffs=value;
        strDropOff=value;
        
    }
    
    if ([_editingCell.column.title isEqualToString:@"Pick Up"]) {
        cell.textField.text=value;
        delequipment.PickUp=value;
        strPickUp=value;
        
    }
    
    
    NSUInteger drop=[delequipment.DropOffs integerValue];
    NSUInteger pick=[delequipment.PickUp integerValue];
    NSUInteger net=pick-drop;
    delequipment.Net=[NSString stringWithFormat:@"%d", net];
    
    NSMutableDictionary *editedEquipment =[[NSMutableDictionary alloc]init];
    [editedEquipment setObject:delequipment.EquipmentID forKey:@"EquipmentID"];
    [editedEquipment setObject:delequipment.EquipmentName forKey:@"EquipmentName"];
    [editedEquipment setObject:delequipment.DropOffs forKey:@"DropOffs"];
    [editedEquipment setObject:delequipment.PickUp forKey:@"PickUp"];
    [editedEquipment setObject:delequipment.Net forKey:@"Net"];
    [_modifiedEquipment addObject:editedEquipment];
    
    
    [_shinobiDataGrid reload];
    
    cell.backgroundColor=[UIColor clearColor];
    
    _editingCell = nil;
    [self.pop dismissPopoverAnimated:YES];
}



-(void)viewWillAppear:(BOOL)animated
{
    self.view.superview.bounds = _realBounds;
}

-(IBAction)btnClose:(id)sender
{
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier]isEqualToString:@"deliveryEquipmentComment"]) {
        Comments *cm=segue.destinationViewController;
        cm.delEquipment=self;
      // cm.deliveryequipment=self;
        
        cm.src=@"deliveryEquipmentComment";
    }
    
}

-(IBAction)btnSave:(id)sender
{
      

    for (DelEquipment *equipment in _modifiedEquipment) {
            
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"EquipmentID == %@", [equipment valueForKey:@"EquipmentID"]];
           
        NSMutableDictionary *equipmentDetails = [[[[globDeliveryDictionary valueForKey:@"DeliveryEquipmentInfo"] valueForKey:@"EquipmentDetails"] filteredArrayUsingPredicate:predicate] objectAtIndex:0];
                [equipmentDetails setValue:[equipment valueForKey:@"DropOffs"] forKey:@"DropOffs"];
                [equipmentDetails setValue:[equipment valueForKey:@"Net"] forKey:@"Net"];
                [equipmentDetails setValue:[equipment valueForKey:@"PickUp"] forKey:@"PickUp"];

    }
    NSLog(@"strout %@", strOut);
    [[globDeliveryDictionary objectForKey:@"DeliveryEquipmentInfo"] setObject:strOut forKey:@"TotalEquipmentOut"];
    [[globDeliveryDictionary objectForKey:@"DeliveryEquipmentInfo"] setObject:strIn forKey:@"TotalEquipmentIn"];
    [[globDeliveryDictionary objectForKey:@"DeliveryEquipmentInfo"] setObject:strNet forKey:@"TotalEquipmentNet"];
    [[globDeliveryDictionary objectForKey:@"DeliveryEquipmentInfo"] setObject:globDelEquipmentComments forKey:@"EquipmentComment"];
    
    NSLog(@"delivery dictionary now $%@", globDeliveryDictionary);
    
    [self dismissViewControllerAnimated:YES completion:nil];
     
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

#2

Greetings Program!

(Re)sort the _sortedData before the [_shinobiDataGrid reload]:

// Re-sort the _sortedData.
NSArray *newSortedData = [_sortedData sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
	NSUInteger *first = [(DelEquipment *)a Net];
	NSUInteger *second = [(DelEquipment *)b Net];
	// Sort descending.
	return [second compare:first];
	// Sort ascending.
	//return [first compare:second];
}];
_sortedData = [newSortedData mutableCopy];

// Reload the data grid.
[_shinobiDataGrid reload];

Wg


#3

thank you! happy holidays.