Angular 2+: Searching in json data when search is a shared component










0














the app.component has json data displayed in the view using *ngfor. User can perform search as he/she types in the search box (basically on keyup event).



For future ease, I have made this search.component as a shared component.



I have written a code and want to know if this is the correct approach or even better one exists. Please do help.



Note: Currently, the code has 7 entries, but the actual entries will be minimum 1000.



Here is my sample POC for the application.



enter image description here



search.component.html - On every character entered, calling the method getSearchString()



<input type="text" placeholder="type here to search" (keyup)="getSearchString(text)" #text>


search.component.ts - Using subject _searchText defined in a common service to emit the value.



getSearchString(text) 
this.appService._searchText.next(text.value);



app.service.ts - Defining the subject being used.



 _searchText = new Subject<string>();
getSearchText$ = this._searchText.asObservable();


app.component.ts - Subscribing on the observable getSearchText$ to search the json.



ngOnInit() 
this.data = [

name: 'pranjal',
id: 's1'
,
name: 'alini',
id: 's2'
,
name: 'nishank',
id: 's3'
,
name: 'pranvi',
id: 's3'
,
name: 'mayuri',
id: 's4'
,
name: 'malya',
id: 's5'
,
name: 'pravi',
id: 's5'

];
this.filteredData = JSON.parse(JSON.stringify(this.data));
this.appService.getSearchText$.subscribe((searchText) =>
this.filteredData = this.searchData(this.data, searchText);
);


searchData(data, searchText)
let newData = ;
if (!searchText)
newData = this.data;
else
newData = data.filter((obj) =>
return obj.name.includes(searchText);
);

return newData;










share|improve this question


























    0














    the app.component has json data displayed in the view using *ngfor. User can perform search as he/she types in the search box (basically on keyup event).



    For future ease, I have made this search.component as a shared component.



    I have written a code and want to know if this is the correct approach or even better one exists. Please do help.



    Note: Currently, the code has 7 entries, but the actual entries will be minimum 1000.



    Here is my sample POC for the application.



    enter image description here



    search.component.html - On every character entered, calling the method getSearchString()



    <input type="text" placeholder="type here to search" (keyup)="getSearchString(text)" #text>


    search.component.ts - Using subject _searchText defined in a common service to emit the value.



    getSearchString(text) 
    this.appService._searchText.next(text.value);



    app.service.ts - Defining the subject being used.



     _searchText = new Subject<string>();
    getSearchText$ = this._searchText.asObservable();


    app.component.ts - Subscribing on the observable getSearchText$ to search the json.



    ngOnInit() 
    this.data = [

    name: 'pranjal',
    id: 's1'
    ,
    name: 'alini',
    id: 's2'
    ,
    name: 'nishank',
    id: 's3'
    ,
    name: 'pranvi',
    id: 's3'
    ,
    name: 'mayuri',
    id: 's4'
    ,
    name: 'malya',
    id: 's5'
    ,
    name: 'pravi',
    id: 's5'

    ];
    this.filteredData = JSON.parse(JSON.stringify(this.data));
    this.appService.getSearchText$.subscribe((searchText) =>
    this.filteredData = this.searchData(this.data, searchText);
    );


    searchData(data, searchText)
    let newData = ;
    if (!searchText)
    newData = this.data;
    else
    newData = data.filter((obj) =>
    return obj.name.includes(searchText);
    );

    return newData;










    share|improve this question
























      0












      0








      0







      the app.component has json data displayed in the view using *ngfor. User can perform search as he/she types in the search box (basically on keyup event).



      For future ease, I have made this search.component as a shared component.



      I have written a code and want to know if this is the correct approach or even better one exists. Please do help.



      Note: Currently, the code has 7 entries, but the actual entries will be minimum 1000.



      Here is my sample POC for the application.



      enter image description here



      search.component.html - On every character entered, calling the method getSearchString()



      <input type="text" placeholder="type here to search" (keyup)="getSearchString(text)" #text>


      search.component.ts - Using subject _searchText defined in a common service to emit the value.



      getSearchString(text) 
      this.appService._searchText.next(text.value);



      app.service.ts - Defining the subject being used.



       _searchText = new Subject<string>();
      getSearchText$ = this._searchText.asObservable();


      app.component.ts - Subscribing on the observable getSearchText$ to search the json.



      ngOnInit() 
      this.data = [

      name: 'pranjal',
      id: 's1'
      ,
      name: 'alini',
      id: 's2'
      ,
      name: 'nishank',
      id: 's3'
      ,
      name: 'pranvi',
      id: 's3'
      ,
      name: 'mayuri',
      id: 's4'
      ,
      name: 'malya',
      id: 's5'
      ,
      name: 'pravi',
      id: 's5'

      ];
      this.filteredData = JSON.parse(JSON.stringify(this.data));
      this.appService.getSearchText$.subscribe((searchText) =>
      this.filteredData = this.searchData(this.data, searchText);
      );


      searchData(data, searchText)
      let newData = ;
      if (!searchText)
      newData = this.data;
      else
      newData = data.filter((obj) =>
      return obj.name.includes(searchText);
      );

      return newData;










      share|improve this question













      the app.component has json data displayed in the view using *ngfor. User can perform search as he/she types in the search box (basically on keyup event).



      For future ease, I have made this search.component as a shared component.



      I have written a code and want to know if this is the correct approach or even better one exists. Please do help.



      Note: Currently, the code has 7 entries, but the actual entries will be minimum 1000.



      Here is my sample POC for the application.



      enter image description here



      search.component.html - On every character entered, calling the method getSearchString()



      <input type="text" placeholder="type here to search" (keyup)="getSearchString(text)" #text>


      search.component.ts - Using subject _searchText defined in a common service to emit the value.



      getSearchString(text) 
      this.appService._searchText.next(text.value);



      app.service.ts - Defining the subject being used.



       _searchText = new Subject<string>();
      getSearchText$ = this._searchText.asObservable();


      app.component.ts - Subscribing on the observable getSearchText$ to search the json.



      ngOnInit() 
      this.data = [

      name: 'pranjal',
      id: 's1'
      ,
      name: 'alini',
      id: 's2'
      ,
      name: 'nishank',
      id: 's3'
      ,
      name: 'pranvi',
      id: 's3'
      ,
      name: 'mayuri',
      id: 's4'
      ,
      name: 'malya',
      id: 's5'
      ,
      name: 'pravi',
      id: 's5'

      ];
      this.filteredData = JSON.parse(JSON.stringify(this.data));
      this.appService.getSearchText$.subscribe((searchText) =>
      this.filteredData = this.searchData(this.data, searchText);
      );


      searchData(data, searchText)
      let newData = ;
      if (!searchText)
      newData = this.data;
      else
      newData = data.filter((obj) =>
      return obj.name.includes(searchText);
      );

      return newData;







      angular search subject-observer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 7:08









      Pranjal Successena

      899




      899






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Nothing wrong with this approach. Different ways to achieve it. In your case, you are using observables to send a notification from the search component to the service.
          I would do it the other way around, the search component would call a method on the search service. The search service then would publish the results in an observable.



          Have you looked at the redux pattern? Can highly recommend it, especially for larger apps. I'm using ngrx in very big applications, and loving it.






          share|improve this answer




















          • Thanks for sharing. Will surely look into ngrx..
            – Pranjal Successena
            Nov 12 at 8:29


















          0














          Everything seems to be good - but i have some idea - you can just filter your data from the component where you have your original data in your case app.component.ts has the data so just trigger a event from the child search.component.ts and filter the data



          search.component.ts



          @Output() searchText = EventEmitter<any>();
          getSearchString(text)
          this.searchText.emit(text);



          Every time when the text changes it will emit the text changes to your parent component



          app.component.html



          <child-component (searchText)="searchData($event)" ></child-component>



          app.component.ts



          searchData(event: any) 
          let newData = ;
          if (!event)
          newData = this.data;
          else
          newData = this.data.filter(obj => obj.name.includes(event));

          this.data = [...newData];



          This will be your dynamic component whenever you use this component it will pass the search text and you can use this search component in any of your components - Please don't bind the service in your component - if you want to update the service you need to change your code - your component should be something that generates code on demand it should be something that do same action every time



          Hope this helps you- Happy coding :)






          share|improve this answer




















          • Yeah.. this looks good. Thanks for sharing. Search component is shared, not immediate child component of app.component, so I need to keep EventEmitter/Subject in a shared service only.
            – Pranjal Successena
            Nov 12 at 8:28











          • This seems to be other way of adding filters component based - if its not an immediate child you can work with service that will work well :) if in case you are using it as a child component try this and let me know if you find any problem :) thanks Happy coding
            – Rahul
            Nov 12 at 9:53










          • Everything worked as expected :)
            – Pranjal Successena
            Nov 12 at 11:43










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257326%2fangular-2-searching-in-json-data-when-search-is-a-shared-component%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Nothing wrong with this approach. Different ways to achieve it. In your case, you are using observables to send a notification from the search component to the service.
          I would do it the other way around, the search component would call a method on the search service. The search service then would publish the results in an observable.



          Have you looked at the redux pattern? Can highly recommend it, especially for larger apps. I'm using ngrx in very big applications, and loving it.






          share|improve this answer




















          • Thanks for sharing. Will surely look into ngrx..
            – Pranjal Successena
            Nov 12 at 8:29















          0














          Nothing wrong with this approach. Different ways to achieve it. In your case, you are using observables to send a notification from the search component to the service.
          I would do it the other way around, the search component would call a method on the search service. The search service then would publish the results in an observable.



          Have you looked at the redux pattern? Can highly recommend it, especially for larger apps. I'm using ngrx in very big applications, and loving it.






          share|improve this answer




















          • Thanks for sharing. Will surely look into ngrx..
            – Pranjal Successena
            Nov 12 at 8:29













          0












          0








          0






          Nothing wrong with this approach. Different ways to achieve it. In your case, you are using observables to send a notification from the search component to the service.
          I would do it the other way around, the search component would call a method on the search service. The search service then would publish the results in an observable.



          Have you looked at the redux pattern? Can highly recommend it, especially for larger apps. I'm using ngrx in very big applications, and loving it.






          share|improve this answer












          Nothing wrong with this approach. Different ways to achieve it. In your case, you are using observables to send a notification from the search component to the service.
          I would do it the other way around, the search component would call a method on the search service. The search service then would publish the results in an observable.



          Have you looked at the redux pattern? Can highly recommend it, especially for larger apps. I'm using ngrx in very big applications, and loving it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 7:26









          Boland

          544724




          544724











          • Thanks for sharing. Will surely look into ngrx..
            – Pranjal Successena
            Nov 12 at 8:29
















          • Thanks for sharing. Will surely look into ngrx..
            – Pranjal Successena
            Nov 12 at 8:29















          Thanks for sharing. Will surely look into ngrx..
          – Pranjal Successena
          Nov 12 at 8:29




          Thanks for sharing. Will surely look into ngrx..
          – Pranjal Successena
          Nov 12 at 8:29













          0














          Everything seems to be good - but i have some idea - you can just filter your data from the component where you have your original data in your case app.component.ts has the data so just trigger a event from the child search.component.ts and filter the data



          search.component.ts



          @Output() searchText = EventEmitter<any>();
          getSearchString(text)
          this.searchText.emit(text);



          Every time when the text changes it will emit the text changes to your parent component



          app.component.html



          <child-component (searchText)="searchData($event)" ></child-component>



          app.component.ts



          searchData(event: any) 
          let newData = ;
          if (!event)
          newData = this.data;
          else
          newData = this.data.filter(obj => obj.name.includes(event));

          this.data = [...newData];



          This will be your dynamic component whenever you use this component it will pass the search text and you can use this search component in any of your components - Please don't bind the service in your component - if you want to update the service you need to change your code - your component should be something that generates code on demand it should be something that do same action every time



          Hope this helps you- Happy coding :)






          share|improve this answer




















          • Yeah.. this looks good. Thanks for sharing. Search component is shared, not immediate child component of app.component, so I need to keep EventEmitter/Subject in a shared service only.
            – Pranjal Successena
            Nov 12 at 8:28











          • This seems to be other way of adding filters component based - if its not an immediate child you can work with service that will work well :) if in case you are using it as a child component try this and let me know if you find any problem :) thanks Happy coding
            – Rahul
            Nov 12 at 9:53










          • Everything worked as expected :)
            – Pranjal Successena
            Nov 12 at 11:43















          0














          Everything seems to be good - but i have some idea - you can just filter your data from the component where you have your original data in your case app.component.ts has the data so just trigger a event from the child search.component.ts and filter the data



          search.component.ts



          @Output() searchText = EventEmitter<any>();
          getSearchString(text)
          this.searchText.emit(text);



          Every time when the text changes it will emit the text changes to your parent component



          app.component.html



          <child-component (searchText)="searchData($event)" ></child-component>



          app.component.ts



          searchData(event: any) 
          let newData = ;
          if (!event)
          newData = this.data;
          else
          newData = this.data.filter(obj => obj.name.includes(event));

          this.data = [...newData];



          This will be your dynamic component whenever you use this component it will pass the search text and you can use this search component in any of your components - Please don't bind the service in your component - if you want to update the service you need to change your code - your component should be something that generates code on demand it should be something that do same action every time



          Hope this helps you- Happy coding :)






          share|improve this answer




















          • Yeah.. this looks good. Thanks for sharing. Search component is shared, not immediate child component of app.component, so I need to keep EventEmitter/Subject in a shared service only.
            – Pranjal Successena
            Nov 12 at 8:28











          • This seems to be other way of adding filters component based - if its not an immediate child you can work with service that will work well :) if in case you are using it as a child component try this and let me know if you find any problem :) thanks Happy coding
            – Rahul
            Nov 12 at 9:53










          • Everything worked as expected :)
            – Pranjal Successena
            Nov 12 at 11:43













          0












          0








          0






          Everything seems to be good - but i have some idea - you can just filter your data from the component where you have your original data in your case app.component.ts has the data so just trigger a event from the child search.component.ts and filter the data



          search.component.ts



          @Output() searchText = EventEmitter<any>();
          getSearchString(text)
          this.searchText.emit(text);



          Every time when the text changes it will emit the text changes to your parent component



          app.component.html



          <child-component (searchText)="searchData($event)" ></child-component>



          app.component.ts



          searchData(event: any) 
          let newData = ;
          if (!event)
          newData = this.data;
          else
          newData = this.data.filter(obj => obj.name.includes(event));

          this.data = [...newData];



          This will be your dynamic component whenever you use this component it will pass the search text and you can use this search component in any of your components - Please don't bind the service in your component - if you want to update the service you need to change your code - your component should be something that generates code on demand it should be something that do same action every time



          Hope this helps you- Happy coding :)






          share|improve this answer












          Everything seems to be good - but i have some idea - you can just filter your data from the component where you have your original data in your case app.component.ts has the data so just trigger a event from the child search.component.ts and filter the data



          search.component.ts



          @Output() searchText = EventEmitter<any>();
          getSearchString(text)
          this.searchText.emit(text);



          Every time when the text changes it will emit the text changes to your parent component



          app.component.html



          <child-component (searchText)="searchData($event)" ></child-component>



          app.component.ts



          searchData(event: any) 
          let newData = ;
          if (!event)
          newData = this.data;
          else
          newData = this.data.filter(obj => obj.name.includes(event));

          this.data = [...newData];



          This will be your dynamic component whenever you use this component it will pass the search text and you can use this search component in any of your components - Please don't bind the service in your component - if you want to update the service you need to change your code - your component should be something that generates code on demand it should be something that do same action every time



          Hope this helps you- Happy coding :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 7:30









          Rahul

          9631315




          9631315











          • Yeah.. this looks good. Thanks for sharing. Search component is shared, not immediate child component of app.component, so I need to keep EventEmitter/Subject in a shared service only.
            – Pranjal Successena
            Nov 12 at 8:28











          • This seems to be other way of adding filters component based - if its not an immediate child you can work with service that will work well :) if in case you are using it as a child component try this and let me know if you find any problem :) thanks Happy coding
            – Rahul
            Nov 12 at 9:53










          • Everything worked as expected :)
            – Pranjal Successena
            Nov 12 at 11:43
















          • Yeah.. this looks good. Thanks for sharing. Search component is shared, not immediate child component of app.component, so I need to keep EventEmitter/Subject in a shared service only.
            – Pranjal Successena
            Nov 12 at 8:28











          • This seems to be other way of adding filters component based - if its not an immediate child you can work with service that will work well :) if in case you are using it as a child component try this and let me know if you find any problem :) thanks Happy coding
            – Rahul
            Nov 12 at 9:53










          • Everything worked as expected :)
            – Pranjal Successena
            Nov 12 at 11:43















          Yeah.. this looks good. Thanks for sharing. Search component is shared, not immediate child component of app.component, so I need to keep EventEmitter/Subject in a shared service only.
          – Pranjal Successena
          Nov 12 at 8:28





          Yeah.. this looks good. Thanks for sharing. Search component is shared, not immediate child component of app.component, so I need to keep EventEmitter/Subject in a shared service only.
          – Pranjal Successena
          Nov 12 at 8:28













          This seems to be other way of adding filters component based - if its not an immediate child you can work with service that will work well :) if in case you are using it as a child component try this and let me know if you find any problem :) thanks Happy coding
          – Rahul
          Nov 12 at 9:53




          This seems to be other way of adding filters component based - if its not an immediate child you can work with service that will work well :) if in case you are using it as a child component try this and let me know if you find any problem :) thanks Happy coding
          – Rahul
          Nov 12 at 9:53












          Everything worked as expected :)
          – Pranjal Successena
          Nov 12 at 11:43




          Everything worked as expected :)
          – Pranjal Successena
          Nov 12 at 11:43

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257326%2fangular-2-searching-in-json-data-when-search-is-a-shared-component%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3