0

I want to search in this array of json based on

track_order

I get this result from server and set in orders variable:

   "records": [
        {
            "phone": "09********8",
            "fullName": "******",
            "records": [
                {
                    "status": "processing",
                    "details": "***",
                    "cost": "1500000",
                    "date": "۹۹/۰۳/۰۷  ۱۰:۱۴",
                    "track_order": ""
                },
                {
                    "status": "send",
                    "details": "***",
                    "cost": "2000000",
                    "date": "۹۹/۰۳/۰۷  ۱۰:۳۹",
                    "track_order": "******"
                },
                {
                    "status": "cancel",
                    "details": "******",
                    "cost": "2000000000000",
                    "date": "۹۹/۰۳/۰۷  ۱۰:۱۴",
                    "track_order": ""
                },
                {
                    "status": "proccing",
                    "details": "******",
                    "cost": "2000000000000",
                    "date": "۹۹/۰۳/۰۷  ۱۰:۱۴",
                    "track_order": ""
                }
            ]
        },
        {
            "phone": "093*******8",
            "fullName": "*******",
            "records": [
                {
                    "status": "send",
                    "details": "********",
                    "cost": "2000000000000",
                    "date": "۹۹/۰۳/۰۷  ۱۰:۴۰",
                    "track_order": "*************"
                }
            ]
        },
        {
            "phone": "09*********1",
            "fullName": "*********",
            "records": []
        }
    ]

this is my vue.js code for search I want to filter orders in getOrders and return it:

   data(){
         return {
           orders:[],
           search:""
        }
  }
  computed:{

      getOrders(){
         if(this.search){
            for(let i=0;i<this.orders.length;i++){

            return this.orders[i].records.filter(record=>{

                return this.search.toLowerCase().split(' ').every(v=>    record.track_order.toLowerCase().includes(v));
            })
        }



    }else{
        return this.orders;
    }
}
    }

and I set

getOrders

in Table like this:

         <tbody v-for="(item,index) in getOrders" :key="index">
                      <tr  v-for="(record,index1) in item.records" :key="index1">
                        <td >{{item.fullName}}</td>
                        <td >{{record.details}}</td>
                        <td>{{record.cost}}</td>
                        <td  >{{record.date}}</td>
                        <td >{{record.status}}</td>
                        <td  >{{record.track_order}}</td>
     </tbody>

But it doesn't work. And by entering the word to search, the table does not change What is wrong? Can you Help me?

1 Answer 1

2

new Vue({
  el: '#app',
  template: `
    <div>
      <input v-model="search"><button v-if="search" type="button" @click="setDefault">default</button>
      <table>
        <tbody v-for="(item,index) in getOrders" :key="index">
          <tr  v-for="(record,index1) in item.records" :key="index1">
            <td >{{item.fullName}}</td>
            <td >{{record.details}}</td>
            <td>{{record.cost}}</td>
            <td  >{{record.date}}</td>
            <td >{{record.status}}</td>
            <td  >{{record.track_order}}</td>
          </tr>  
        </tbody>
      </table>
    </div>
  `,
  data() {
    return {
      search: "",
      records: [{
          "phone": "09********8",
          "fullName": "******",
          "records": [{
              "status": "processing",
              "details": "***",
              "cost": "1500000",
              "date": "۹۹/۰۳/۰۷  ۱۰:۱۴",
              "track_order": "aloha"
            },
            {
              "status": "send",
              "details": "***",
              "cost": "2000000",
              "date": "۹۹/۰۳/۰۷  ۱۰:۳۹",
              "track_order": "******"
            },
            {
              "status": "cancel",
              "details": "******",
              "cost": "2000000000000",
              "date": "۹۹/۰۳/۰۷  ۱۰:۱۴",
              "track_order": ""
            },
            {
              "status": "proccing",
              "details": "******",
              "cost": "2000000000000",
              "date": "۹۹/۰۳/۰۷  ۱۰:۱۴",
              "track_order": "---"
            }
          ]
        },
        {
          "phone": "093*******8",
          "fullName": "*******",
          "records": [{
            "status": "send",
            "details": "********",
            "cost": "2000000000000",
            "date": "۹۹/۰۳/۰۷  ۱۰:۴۰",
            "track_order": "*************"
          }]
        },
        {
          "phone": "09*********1",
          "fullName": "*********",
          "records": []
        }
      ],
    };
  },
  computed: {
    getOrders() {
      if (!this.search) {
        return this.records;
      }
      return this.getOrdersFiltered;
    },

    getOrdersFiltered() {
      const RECORDS = [...this.records];
      const RECORD_NEW = [];
      RECORDS.forEach(item => {
        const RECORDS_CHILD = item.records.filter(record => {
          return record.track_order.toLowerCase().indexOf(this.search) !== -1;
        });
        if (RECORDS_CHILD.length) {
          item.records = RECORDS_CHILD;
          RECORD_NEW.push(item)
        }
      });
      return RECORD_NEW;
    },
  },
  methods: {
    setDefault() {
      this.search = "";
    },
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app"></div>

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer.but im want when remove search tabel back to the Default mode.Do you have an opinion?
mean you like that? I adjusted the answer.
No, I want to be like that [stackoverflow.com/questions/52558770/vuejs-search-filter] .The first answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.