Code Monkey home page Code Monkey logo

Comments (15)

JonathanLouw avatar JonathanLouw commented on May 25, 2024 11

@singhabahu I know its been a while but maybe this will help someone else, if you leave out the slot-scope attribute it should work:

<template slot="h__check">
     <input type='checkbox' id='checkbox' @click='selectAllAtOnce()'>
</template>

from vue-tables-2.

dpacmittal avatar dpacmittal commented on May 25, 2024 3

How do I handle events on clicking the heading checkbox?

from vue-tables-2.

freeman-g avatar freeman-g commented on May 25, 2024 3

@dpacmittal

Here's how I'm handling events.

I have the template v-modeled to a selected property:

         <v-client-table ref="myGrid" :data="tableData" :columns="myColumns">
            <template slot="selected" scope="props">
              <div>
                <input v-model="props.row.selected" type="checkbox">
              </div>
            </template>
          </v-client-table>

Here's the heading element:

          headings: {
            selected: function (h) {
              return h('input', {
                attrs: {
                  type: 'checkbox',
                  id: 'selectAllCheckbox'
                },
                on: {
                  click: (e) => {
                    this.selectAll(e.srcElement.checked)
                  }
                },
                ref: 'selectAllCheckbox'
              })
            },

I have a selectAll method that sets or unsets the underlying property when the heading is clicked:

      selectAll(checked) {
        let length = tableData.length
        for (let i = 0; i < length; i++) {
          if (checked) {
            tableData[i].selected = true
          } else {
            tableData[i].selected = false
          }
        }
      },

from vue-tables-2.

singhabahu avatar singhabahu commented on May 25, 2024 2

@jeff-pang @freeman-g @matfish2 I've been trying to add a checkbox for the table header but below code didn't work. However code to add checkbox to each row works fine.

<template slot="h__selected" slot-scope="props">
     <input type='checkbox' id='checkbox' @click='selectAll()'>
</template>

any idea where I made the mistake?

Here's my code

{
      tableColumns: [
        "check",
        "Name",
        "WindowId",
        "Enabled",
        "Duration",
        "Description"
      ],
      tableData: [],
      tableOptions: {
        headings: {
          Name: "Name",
          WindowId: "Window ID",
          Enabled: "Status",
          Duration: "Duration",
          Description: "Description"
        },
        sortable: ["Name", "WindowId", "Enabled", "Duration", "Description"],
        filterable: ["Name", "WindowId", "Enabled", "Duration", "Description"]
}

This is how I render my table

<template slot="h__check" slot-scope="props">
     <input type='checkbox' id='checkbox' @click='selectAllAtOnce()'>
</template>

from vue-tables-2.

jeff-pang avatar jeff-pang commented on May 25, 2024 1

following up on the @freeman-g 's example. an alternative to the render function selected is to replace it using scope-slot like this

<!-- checkbox for each row-->
<template slot="selected" slot-scope="props">
            <input type='checkbox' id='checkbox' v-model='props.row.isChecked'>
</template>
<!-- checkbox for each header (prefix column name with h__-->
<template slot="h__selected" slot-scope="props">
            <input type='checkbox' id='checkbox' @click='selectAll()'>
</template>

define a variable to toggle check/uncheck states. in my example i use isSelected

selectAll() {
     let length = this.tableData.length
     this.isRead=!this.isSelected;

     for (let i = 0; i < length; i++) {
         this.tableData[i].isChecked=this.isSelected;
     }
}

from vue-tables-2.

singhabahu avatar singhabahu commented on May 25, 2024 1

@JonathanLouw thanks for the input. will look into this when I get a free time.

from vue-tables-2.

jayarc avatar jayarc commented on May 25, 2024

I've been experimenting with ways to do this with templates. So far I have an implementation that shows the checkboxes but unfortuanately does not correctly move them on sorting.

from vue-tables-2.

matfish2 avatar matfish2 commented on May 25, 2024

I've added a possibility to pass a function to render dynamic HTML. E.g:

headings:{
   title: function(h) {
     return <h6><input type="checkbox"/>Title</h6>
   }
}

Note that I'm using JSX, not HTML. Of course you can also write the node using plain JavaScript.

from vue-tables-2.

ChetnaGupta avatar ChetnaGupta commented on May 25, 2024

How can we add checkboxes to each row ? Apparently below code doesnt work for me
<template slot="selected" slot-scope="props"> <input type='checkbox' id='checkbox' v-model='props.row.isChecked'> </template>

from vue-tables-2.

songoo avatar songoo commented on May 25, 2024

JonathanLouw - Tnx much that one helped , seems 'h__' slot still do not like slot-scope="props" .

from vue-tables-2.

hansa0712004212 avatar hansa0712004212 commented on May 25, 2024

Thanks everyone.
Well I was able to add checkbox in header row as Select/Deselect All option for rows.

Btw, my problem is,
I want to use select all on filtered results only.
Or I want to select Row checkboxes when select HeaderRow checkbox for filtered/currently visible results on table only.

from vue-tables-2.

andikajayaw avatar andikajayaw commented on May 25, 2024

Thanks everyone.
Well I was able to add checkbox in header row as Select/Deselect All option for rows.

Btw, my problem is,
I want to use select all on filtered results only.
Or I want to select Row checkboxes when select HeaderRow checkbox for filtered/currently visible results on table only.

Can I see your code for Header checkbox to select all/deselect all the rows? Thank you.

from vue-tables-2.

ohasy avatar ohasy commented on May 25, 2024

After banging my head on the wall for 4 hours, I finally found this thread. slot-scope dont work with h__ slots.

              <template
                  v-for="column in columns"
                  :slot="`h__${column}`"
                >
                  <div
                    :key="column"
                    v-html="options.headings[column]"
                    @click="clickhandler($event,column)"
                  />
                </template>

from vue-tables-2.

hoai avatar hoai commented on May 25, 2024

Hi,
My solution is working. maybe this is help other.
Please see it.
https://gist.github.com/hoai/dc37116f6a17a45232647dcfe8090e51

from vue-tables-2.

SauceTV avatar SauceTV commented on May 25, 2024

The header on my checkbox does not update with the "allSelected" variable. The only way to fix it that I found was to use a ref on the header and change the ".checked" var to true or false. The problem is that vue doesnt like when you change the prop directly.

HTML:

<v-client-table :columns="['name', 'select']" :data="products" :options="table.options" :theme="table.theme" class="dataTable p-0 m-0 w-100">
      <div slot="h__select" style="float: right">
            <b-checkbox @change="cart.selectedProducts = selectAll()" color="success" style="float: right"
                                          v-model="allSelected"/>
      </div>
      <div slot="select" slot-scope="props" style="float: right">
            <b-form-checkbox v-model="cart.selectedProducts" :value="props.row._id" color="success"
                                          style="float: right" />
      </div>
</v-client-table>

Vars:

data: () => {
      return {
            products: {
                  {name: "socks", _id: 1},
                  {name: "shirt", _id: 2},
                  {name: "pants", _id: 3},
            },
            cart:  {
                  selectedProducts: [],
                  ...  // Other cart Data
            },
            allSelected: false,
            ...  // Other Page Data
      }
}

Watchers:

watch: {
      selectedProducts: {
            deep: true,
            handler() {
                  let all = this.products.filter(product => {
                        return this.cart.selectedProducts.includes(product._id);
                  }
                  if (all.length === this.products.length) {
                       console.log("change to true")
                        this.allSelected = true;
                  } else {
                       console.log("change to false")
                        this.allSelected = false;
                  }
            }
      }
}

Methods:

methods: {
      selectAll() {
                  let result = [];
                  let all = this.products.filter(product => {
                        return this.cart.selectedProducts.includes(product._id);
                  }
                  if (all.length === this.products.length) {
                        return result;
                  } else {
                        this.products.forEach(function (product, index) {
                              Vue.set(result, index, product);
                        })
                        return result;
                  }
      }
}

from vue-tables-2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.