No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    how to filter cities based on the state selcted

    Help
    2
    4
    143
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      thri60 last edited by thri60

      I am working on a form that involves users selecting their exact city based on the state selected. I have a Json file of this states and cities but cant seem to pull it into a qselect… or I cant seem to pull the cities into the city qselect

      i dont exactly no how to describe my issue… but I hope you understand

      this is the json file (with reference to 1 state)
      [{
      "state": {
      "name": “Abia State”,
      "id": 1,
      "locals": [
      { “name”: “Aba South”, “id”: 1 },
      { “name”: “Arochukwu”, “id”: 2 },
      { “name”: “Bende”, “id”: 3 },
      { “name”: “Ikwuano”, “id”: 4 },
      { “name”: “Isiala Ngwa North”, “id”: 5 },
      { “name”: “Isiala Ngwa South”, “id”: 6 },
      { “name”: “Isuikwuato”, “id”: 7 },
      { “name”: “Obi Ngwa”, “id”: 8 },
      { “name”: “Ohafia”, “id”: 9 },
      { “name”: “Osisioma”, “id”: 10 },
      { “name”: “Ugwunagbo”, “id”: 11 },
      { “name”: “Ukwa East”, “id”: 12 },
      { “name”: “Ukwa West”, “id”: 13 },
      { “name”: “Umuahia North”, “id”: 14 },
      { “name”: “Umuahia South”, “id”: 15 },
      { “name”: “Umu Nneochi”, “id”: 16 }
      ]
      }
      }]

      this is how the form looks like
      form.png

      code:
      <div class=“q-gutter-md”>
      <div>
      <q-select
      behavior=“menu”
      rounded
      outlined
      v-model=“locations”
      :options=“states”
      label=“Located in what State”
      />
      </div>
      <div>
      <q-select
      behavior=“menu”
      rounded
      outlined
      @filter=“filterFn”
      v-model=“category”
      :options=“categories”
      label=“City”
      />
      </div>
      </div>

      <script>
      import location from “assets/location.json”;
      export default {
      data() {
      return {
      locations: [],
      states: [],
      city: [],
      state: null,
      city: null,

      },

      mounted() {
      location.forEach(element => {
      const name = element.state.name;
      this.states.push(name);
      });
      }
      };
      </script>

      Lurrik 1 Reply Last reply Reply Quote 0
      • Lurrik
        Lurrik @thri60 last edited by Lurrik

        Hi @thri60,

        Try this (sorry I don’t use a Json file but what I did is the same, that can be usefull for others who don’t use Json file) :

        TEMPLATE

        <template>
          <q-page class="flex flex-center">
          <div class="q-gutter-md" style="width:250px">
            <div>
              <q-select
                behavior="menu"
                rounded
                outlined
                v-model="ModelState"
                :options="states"
                label="Located in what State"
                @input="pushCity"
                style=""
                />
              </div>
              <div>
              <q-select
                behavior="menu"
                rounded
                outlined
                v-model="Modelcity"
                :options="city"
                label="City"
                />
              </div>
          </div>
          </q-page>
        </template>
        

        SCRIPT

        <script>
        export default {
          data() {
            return {
                ModelState: null,
                states: [],
                Modelcity: null,
                city: [],
                location :[
                  {
                    state: {
                        name: "Abia State",
                        id: 1,
                        locals: [
                        { name: "Aba South", id: 1 },
                        { name: "Arochukwu", id: 2 },
                        { name: "Bende", id: 3 },
                        { name: "Ikwuano", id: 4 },
                        { name: "Isiala Ngwa North", id: 5 },
                        { name: "Isiala Ngwa South", id: 6 },
                        { name: "Isuikwuato", id: 7 },
                        { name: "Obi Ngwa", id: 8 },
                        { name: "Ohafia", id: 9 },
                        { name: "Osisioma", id: 10 },
                        { name: "Ugwunagbo", id: 11 },
                        { name: "Ukwa East", id: 12 },
                        { name: "Ukwa West", id: 13 },
                        { name: "Umuahia North", id: 14 },
                        { name: "Umuahia South", id: 15 },
                        { name: "Umu Nneochi", id: 16 }
                        ]
                    }
                },
                {
                  state: {
                  name: "France",
                    id: 2,
                        locals: [
                        { name: "Paris", id: 1 },
                        ]
                  }
                }
                ]
            }
          },
          methods: {
            pushCity() {
                const isnameAdd = (element) => element.state.name == this.ModelState;
                const index = this.location.findIndex(isnameAdd);
                this.city = []
                this.location[index].state.locals.forEach(element => {
                  const name = element.name;
                  this.city.push(name);
                });
            }
          },
          mounted() {
            this.location.forEach(element => {
            const name = element.state.name;
            this.states.push(name);
            });
          }
        }
        </script>
        
        T 2 Replies Last reply Reply Quote 1
        • T
          thri60 @Lurrik last edited by

          @lurrik thanks so much… will try it out and get back to you…

          1 Reply Last reply Reply Quote 0
          • T
            thri60 @Lurrik last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • First post
              Last post