Skip to content

Commit

Permalink
add filter for JSON, optional items
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Aksonov committed Aug 19, 2015
1 parent e396394 commit e61701a
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 66 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 4 additions & 2 deletions RCTTableView/JSONDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ -(id)initWithFilename:(NSString *)filename filter:(NSString *)filter args:(NSArr
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSError *error = nil;
NSArray *json = (NSArray *)[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
options:NSJSONReadingMutableContainers
error:&error];

NSAssert(error==nil, @"JSON Error %@", [error description]);
NSAssert([json isKindOfClass:[NSArray class]], @"JSON should be NSArray type");

if (filter){
json = [json filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:filter argumentArray:filterArgs]];
for (NSMutableDictionary *sections in json){
sections[@"items"] = [sections[@"items"] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:filter argumentArray:filterArgs]];
}
}

_sections = json;
Expand Down
1 change: 1 addition & 0 deletions RCTTableView/RCTTableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;

@property (nonatomic, copy) NSMutableArray *sections;
@property (nonatomic, copy) NSArray *additionalItems;
@property (nonatomic, strong) NSString *json;
@property (nonatomic, strong) NSString *filter;
@property (nonatomic, strong) NSArray *filterArgs;
Expand Down
10 changes: 8 additions & 2 deletions RCTTableView/RCTTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@ - (void)setSections:(NSArray *)sections
_sections = [NSMutableArray arrayWithCapacity:[sections count]];
for (NSDictionary *section in sections){
NSMutableDictionary *sectionData = [NSMutableDictionary dictionaryWithDictionary:section];
NSMutableArray *items = [NSMutableArray arrayWithCapacity:[sectionData[@"items"] count]];
for (NSDictionary *item in sectionData[@"items"]){
NSMutableArray *allItems = [NSMutableArray array];
if (self.additionalItems){
[allItems addObjectsFromArray:self.additionalItems];
}
[allItems addObjectsFromArray:sectionData[@"items"]];

NSMutableArray *items = [NSMutableArray arrayWithCapacity:[allItems count]];
for (NSDictionary *item in allItems){
NSMutableDictionary *itemData = [NSMutableDictionary dictionaryWithDictionary:item];
if (self.selectedValue && [self.selectedValue isEqualToString:item[@"value"]]){
_selectedSection = [_sections count];
Expand Down
1 change: 1 addition & 0 deletions RCTTableView/RCTTableViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(filter, NSString)
RCT_EXPORT_VIEW_PROPERTY(selectedValue, NSString)
RCT_EXPORT_VIEW_PROPERTY(filterArgs, NSArray)
RCT_EXPORT_VIEW_PROPERTY(additionalItems, NSArray)
RCT_EXPORT_VIEW_PROPERTY(selectedIndex, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(selectedSection, NSInteger)
RCT_CUSTOM_VIEW_PROPERTY(tableViewStyle, UITableViewStyle, RCTTableView) {
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Native iOS UITableView for React Native with JSON support.
- Automatic scroll to initial selected value during component initialization
- Automatic item selection with "checkmark" with old item de-selection (optionally), see demo, useful to select country/state/etc.
- Native JSON support for datasource. If you need to display large dataset, generated Javascript will became very large and impact js loading time. To solve this problem the component could read JSON directly from app bundle without JS!
- Filter JSON datasources using NSPredicate syntax. For example you could select states for given country only (check demo)

## Supports UITableView styles
- UITableViewStylePlain (TableView.Consts.Style.Plain)
Expand Down Expand Up @@ -98,6 +99,21 @@ AppRegistry.registerComponent('TableViewExample', () => TableViewExample);
}
```

### Example 3 (JSON filter and optional items at the beginning)
```
// list spanish provinces and add 'All states' item at the beginning
render(){
var country = "ES";
return (
<TableView selectedValue="" style={{flex:1}} json="states" filter={`country=='${country}'`}
tableViewCellStyle={TableView.Consts.CellStyle.Subtitle}
onPress={(event) => console.log(event.nativeEvent)}>
<Item value="">All states</Item>
</TableView>
);
}
```

## Getting started
1. `npm install react-native-tableview --save`
2. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
Expand Down
Binary file modified examples/.DS_Store
Binary file not shown.
86 changes: 43 additions & 43 deletions examples/TableViewDemo/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e61701a

Please sign in to comment.