JSFiddle - React, Tailwind, and code Playground

by ke zheng

HTML

<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.0.8/lib/index.js"></script>
<div id="app">
  <div class="panel-body">
    <!-- 查询表单 -->
    <div>
      <table class="table table-bordered table-striped" ref="zx">
        <thead>
          <tr>
            <th>
              <!-- <input type="checkbox" @sel="checkfull()" /> -->
              <check-all data="zx" @ymhy-select-all="checkall"></check-all>
            </th>
            <th>序号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>手机号码</th>
            <th>职位</th>
            <th>入职时间</th>
            <th>在职状态</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item,index) in zx">
            <td>
              <!--               <input type="checkbox" data="list" @click="checkitem(item,index)" /> -->
              <check-item data="zx" @ymhy-select-item="checkitem"></check-item>
            </td>
            <td>{{index+1}}</td>
            <td>
              <a href="mandetails" style="color:#20a0ff;text-decoration: underline;">{{item.name}}</a>
            </td>
            <td>{{item.sex}}</td>
            <td>{{item.phone}}</td>
            <td>{{item.position}}</td>
            <td>{{item.date}}</td>
            <td>{{item.status}}</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[100, 200, 300, 400]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="8000">
      </el-pagination>
    </div>
  </div>
</div>

CSS

@import url("//unpkg.com/element-ui@2.0.8/lib/theme-chalk/index.css");

JavaScript

Vue.component('check-all', {
  template: '<input type="checkbox" @click="checkall()" />',
  // 但是我们却给每个组件实例返回了同一个对象的引用
  props: {
    // 必须为string类型
    data: {
      type: String,
    },
  },
  methods: {
    checkall: function() {
      var data = this.data ? this.data : "list";
      var row = this.$root.$refs[data].rows;
      var arr = [];
      var sels = [];
      for (var i = 1; i < row.length; i++) {
        if (row[i].children[0].children[0].checked) {
          arr.push(row[i].children[0].children[0].checked);
        };
      };
      if (arr.length === row.length - 1) {
        for (var i = 1; i < row.length; i++) {

          row[i].children[0].children[0].checked = false;
        }
      } else {
        for (var i = 1; i < row.length; i++) {
          row[i].children[0].children[0].checked = true;
          sels.push(this.$root[data][row[i - 1].rowIndex]);
        }
      }
      this.$emit('ymhy-select-all', sels)
    }
  }
})
Vue.component('check-item', {
  template: '<input type="checkbox" @click="checkitem()" />',
  // 技术上 data 的确是一个函数了,因此 Vue 不会警告,
  // 但是我们却给每个组件实例返回了同一个对象的引用
  props: {
    // 数组/对象的默认值应当由一个工厂函数返回
    data: {
      type: String,
    },
  },
  data: function() {
    return {}
  },
  methods: {
    checkitem: function() {
      var data = this.data ? this.data : "list";
      var row = this.$root.$refs[data].rows;
      var arr = [];
      var sels = [];
      for (var i = 1; i < row.length; i++) {
        if (row[i].children[0].children[0].checked) {
          arr.push(row[i].children[0].children[0].checked);
          sels.push(this.$root[data][row[i - 1].rowIndex]);
        };
      }
 
      if (arr.length === row.length - 1) {
        row[0].children[0].children[0].checked = true;
      } else {
        row[0].children[0].children[0].checked = false;
      }
      this.$emit('ymhy-select-item', sels)
    },
  }
})

var Main = {
  data() {
      return {
        zx: [{
          id: 1,
          date: '2016-05-03',
       ...