Skip to content

Commit e61701a

Browse files
author
Pavlo Aksonov
committed
add filter for JSON, optional items
1 parent e396394 commit e61701a

File tree

14 files changed

+139
-66
lines changed

14 files changed

+139
-66
lines changed

.DS_Store

0 Bytes
Binary file not shown.

RCTTableView/JSONDataSource.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ -(id)initWithFilename:(NSString *)filename filter:(NSString *)filter args:(NSArr
1717
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
1818
NSError *error = nil;
1919
NSArray *json = (NSArray *)[NSJSONSerialization JSONObjectWithData:data
20-
options:kNilOptions
20+
options:NSJSONReadingMutableContainers
2121
error:&error];
2222

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

2626
if (filter){
27-
json = [json filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:filter argumentArray:filterArgs]];
27+
for (NSMutableDictionary *sections in json){
28+
sections[@"items"] = [sections[@"items"] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:filter argumentArray:filterArgs]];
29+
}
2830
}
2931

3032
_sections = json;

RCTTableView/RCTTableView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;
2525

2626
@property (nonatomic, copy) NSMutableArray *sections;
27+
@property (nonatomic, copy) NSArray *additionalItems;
2728
@property (nonatomic, strong) NSString *json;
2829
@property (nonatomic, strong) NSString *filter;
2930
@property (nonatomic, strong) NSArray *filterArgs;

RCTTableView/RCTTableView.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,14 @@ - (void)setSections:(NSArray *)sections
8383
_sections = [NSMutableArray arrayWithCapacity:[sections count]];
8484
for (NSDictionary *section in sections){
8585
NSMutableDictionary *sectionData = [NSMutableDictionary dictionaryWithDictionary:section];
86-
NSMutableArray *items = [NSMutableArray arrayWithCapacity:[sectionData[@"items"] count]];
87-
for (NSDictionary *item in sectionData[@"items"]){
86+
NSMutableArray *allItems = [NSMutableArray array];
87+
if (self.additionalItems){
88+
[allItems addObjectsFromArray:self.additionalItems];
89+
}
90+
[allItems addObjectsFromArray:sectionData[@"items"]];
91+
92+
NSMutableArray *items = [NSMutableArray arrayWithCapacity:[allItems count]];
93+
for (NSDictionary *item in allItems){
8894
NSMutableDictionary *itemData = [NSMutableDictionary dictionaryWithDictionary:item];
8995
if (self.selectedValue && [self.selectedValue isEqualToString:item[@"value"]]){
9096
_selectedSection = [_sections count];

RCTTableView/RCTTableViewManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ - (UIView *)view
2424
RCT_EXPORT_VIEW_PROPERTY(filter, NSString)
2525
RCT_EXPORT_VIEW_PROPERTY(selectedValue, NSString)
2626
RCT_EXPORT_VIEW_PROPERTY(filterArgs, NSArray)
27+
RCT_EXPORT_VIEW_PROPERTY(additionalItems, NSArray)
2728
RCT_EXPORT_VIEW_PROPERTY(selectedIndex, NSInteger)
2829
RCT_EXPORT_VIEW_PROPERTY(selectedSection, NSInteger)
2930
RCT_CUSTOM_VIEW_PROPERTY(tableViewStyle, UITableViewStyle, RCTTableView) {

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Native iOS UITableView for React Native with JSON support.
77
- Automatic scroll to initial selected value during component initialization
88
- Automatic item selection with "checkmark" with old item de-selection (optionally), see demo, useful to select country/state/etc.
99
- 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!
10+
- Filter JSON datasources using NSPredicate syntax. For example you could select states for given country only (check demo)
1011

1112
## Supports UITableView styles
1213
- UITableViewStylePlain (TableView.Consts.Style.Plain)
@@ -98,6 +99,21 @@ AppRegistry.registerComponent('TableViewExample', () => TableViewExample);
9899
}
99100
```
100101

102+
### Example 3 (JSON filter and optional items at the beginning)
103+
```
104+
// list spanish provinces and add 'All states' item at the beginning
105+
render(){
106+
var country = "ES";
107+
return (
108+
<TableView selectedValue="" style={{flex:1}} json="states" filter={`country=='${country}'`}
109+
tableViewCellStyle={TableView.Consts.CellStyle.Subtitle}
110+
onPress={(event) => console.log(event.nativeEvent)}>
111+
<Item value="">All states</Item>
112+
</TableView>
113+
);
114+
}
115+
```
116+
101117
## Getting started
102118
1. `npm install react-native-tableview --save`
103119
2. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`

examples/.DS_Store

0 Bytes
Binary file not shown.

examples/TableViewDemo/.idea/workspace.xml

Lines changed: 43 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)