iView學習筆記(三):表格搜索,過濾及隱藏列操作

iView學習筆記(三):表格搜索,過濾及隱藏某列操作

1.后端準備工作

環境說明

python版本:3.6.6
Django版本:1.11.8
數據庫:MariaDB 5.5.60

新建Django項目,在項目中新建app,配置好數據庫

api_test/app01/models.py文件內容

from django.db import modelsfrom .utils.parse_time import parse_datetime_to_stringHOST_STATUS = ((1, "processing"),(2, "error"),
)class HostInfo(models.Model):hostname = models.CharField("主機名", max_length=32)ip = models.CharField("IP地址", max_length=16)status = models.IntegerField("主機狀態", choices=HOST_STATUS, default=1)date = models.DateTimeField("主機添加時間", auto_now_add=True)def to_dict(self):return {"hostname": self.hostname,"ip": self.ip,"status": self.status,"when_insert": parse_datetime_to_string(self.date),}

app01/utils/parse_time.py文件內容

def parse_datetime_to_string(datetime_str, flag=True):"""把datetime時間轉化成時間字符串:param datetime_str: datetime生成的時間,例子:datetime.datetime.now()或者: datetime.datetime.now() - datetime.timedelta(hours=1)       # 一個小時之前或者: datetime.datetime.now() - datetime.timedelta(days=1)        # 一天之前:return:"""# 將日期轉化為字符串 datetime => string# 在數據庫中定義字段信息時為:models.DateTimeField(auto_now_add=True)# 查詢數據庫之后,使用此方法把查詢到的時間轉換成可用的時間字符串# when_insert__range=(an_hour_time, now_time)# an_hour_time和 now_time 都是 datetime時間字符串,查詢兩個datetime時間字符串之間的數據if flag:return datetime_str.strftime('%Y-%m-%d %H:%M:%S')else:return datetime_str.strftime('%Y-%m-%d')

api_test/urls.py文件內容

from django.conf.urls import url
from django.contrib import admin
from app01 import viewsurlpatterns = [url(r'^admin/', admin.site.urls),url(r'^host/$', views.host_list),
]

api_test/app01/views.py文件內容

import jsonfrom django.http import JsonResponse
from django.views.decorators.csrf import csrf_exemptfrom .models import HostInfo@csrf_exempt
def host_list(request):# for i in range(1, 31):#     hostname = random.choice(["beijing-aws-0","shanghai-aliyun-0"]) + str(i)#     ip = "192.168.100.%d" % i#     status = random.randint(1, 2)#     host_obj = HostInfo(hostname=hostname, ip=ip, status=status)#     host_obj.save()if request.method == "GET":query = request.GET.get("query_string")status = request.GET.get("status")res_list = []host_list = HostInfo.objects.all()if query:host_list = host_list.filter(hostname__icontains=query)if status:host_list = host_list.filter(status=status)for i in host_list:res_list.append(i.to_dict())return JsonResponse({"data": res_list, "result": True}, safe=False)elif request.method == "POST":data = json.loads(request.body)try:HostInfo.objects.create(**data)res = {"status": "success"}except Exception:res = {"status": "fail"}return JsonResponse(res, safe=False)

這里前端向后端發送POST請求時,后端沒有執行csrftoken驗證,前端獲取csrftoken,后端進行csrftoken驗證不在本文示例之內

2.前端準備工作

首先新建一個項目,然后引入iView插件,配置好router

npm安裝iView

npm install iview --save
cnpm install iview --save

src/main.js文件內容

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import iView from 'iview';
import 'iview/dist/styles/iview.css';Vue.use(iView);Vue.config.productionTip = false
new Vue({router,render: h => h(App)
}).$mount('#app')

src/router.js文件內容

import Vue from 'vue'
import Router from 'vue-router'Vue.use(Router)export default new Router({routes: [{path: '/table1',component: () => import('./views/table1.vue')},{path:'/',redirect:'/table1'}]
})

src/views/table1.vue文件內容

<template><div style="padding:32px 64px"><h1>在外部進行表格的搜索,過濾,隱藏某列的操作</h1><br><br><Form inline :label-width="80"><FormItem label="主機名稱:"><Input v-model="form.searchoHostname" placeholder="請輸入" style="width:200px;"/></FormItem><FormItem label="使用狀態:"><Select v-model="form.searchHostStatus" placeholder="請選擇" style="width:200px"><Option :value="1">運行中</Option><Option :value="2">異常</Option></Select></FormItem><Button type="primary" @click="getData" style="margin-right:8px;">查詢</Button><Button @click="handleReset">重置</Button></Form><CheckboxGroup v-model="showColumns"><Checkbox :label="0">主機名稱</Checkbox><Checkbox :label="1">IP 地址</Checkbox><Checkbox :label="2">使用狀態</Checkbox><Checkbox :label="3">最后修改時間</Checkbox></CheckboxGroup><Button type="primary" icon="md-add" @click="createDialog=true" style="margin-top:20px;">新建</Button><br><br><Table :data="tableData" :columns="filterColumns" :loading="loading" size="small"></Table><Modal v-model="createDialog" title="新建主機"><Form><FormItem><Input v-model="createHostForm.hostname" placeholder="主機名稱"/></FormItem><FormItem><Input v-model="createHostForm.ip" placeholder="IP 地址"/></FormItem><FormItem><Select v-model="createHostForm.status" placeholder="使用狀態"><Option :value="1">運行中</Option><Option :value="2">異常</Option></Select></FormItem></Form><Button slot="footer" type="primary" @click="handleCreate">創建</Button></Modal></div>
</template><script>import axios from 'axios';export default {data() {return {tableData: [],loading: false,columns: [{title: "主機名稱",key: 'hostname',},{title: "IP地址",key: 'ip',},{title: "等級",key: 'status',render: (h, {row}) => {if (row.status === 1) {return h('Badge', {props: {status: 'processing',text: '運行中'}})} else if (row.status === 2) {return h('Badge', {props: {status: 'error',text: '異常'}})}}},{title: "最后修改時間",key: 'when_insert',}],form: {searchoHostname: '',searchHostStatus: '',},createDialog: false,createHostForm: {hostname: '',ip: '',status: '',},isCreate: false,showColumns: [0, 1, 2, 3],}},computed: {filterColumns() {const columns = [...this.columns];const filterColumns = [];this.showColumns.sort().forEach(item => {filterColumns.push(columns[item])});return filterColumns}},methods: {getData() {if (this.loading) return;this.loading = true;axios.get(`http://127.0.0.1:8000/host/?query_string=${this.form.searchoHostname}&status=${this.form.searchHostStatus}`).then(res => {console.log(res.data)if(res.data.result) {setTimeout(() => {this.tableData = res.data.data;this.loading = false;}, 1000)} else {this.$Message.error('請求失敗');}})},handleReset() {this.form.searchoHostname = "";this.form.searchHostStatus = "";this.getData();},handleCreate() {const hostname = this.createHostForm.hostname;const ip = this.createHostForm.ip;const status = this.createHostForm.status;if (hostname === '') {this.$Message.error("請輸入主機名稱");return;}if (ip === '') {this.$Message.error("請輸入IP地址");return;}if (status === '') {this.$Message.error("請選擇使用狀態");return;}this.isCreate = true;axios.post('http://127.0.0.1:8000/host/', this.createHostForm).then(res => {if(res.data.result) {this.createDialog=falsethis.$Message.success('添加主機成功');} else {this.$Message.error('添加主機失敗');}})}},mounted() {this.getData();}}
</script>

瀏覽器打開URL:http://localhost:8080/#/table1,頁面渲染如下

1133627-20190729205341177-572189586.png

取消選中IP地址列,則下面的table中不顯示IP地址列

1133627-20190729205349653-1619515044.png

主機名稱框中輸入內容進行搜索

1133627-20190729205357265-1984613114.png

在搜索結果中,取消最后修改時間列的顯示

1133627-20190729205423010-1320165936.png

主機狀態進行搜索

1133627-20190729205431089-583257635.png

主機名稱主機狀態進行聯合搜索

1133627-20190729205441944-1507993921.png

轉載于:https://www.cnblogs.com/renpingsheng/p/11266436.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/448082.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/448082.shtml
英文地址,請注明出處:http://en.pswp.cn/news/448082.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Jenkins自動編譯庫并上傳服務器

Jenkins自動編譯庫并上傳服務器 github地址 首先添加 git 地址&#xff1a; 再添加定時構建&#xff0c;每天夜里構建一次&#xff1a; 執行 shell 腳本進行構建 cd networklayerecho "build json x86" cmake -S . -B cmake-build-release -DCMAKE_BUILD_TYPERele…

解決:The “data“ option should be a function that returns a per-instance value in component definitions

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 只是想定義一個變量&#xff0c;方便頁面上調用 。 報錯&#xff1a; The "data" option should be a function that re…

科目三考試里面的會車,調頭,靠邊停車通過標準

科目三會車&#xff1a;減速靠道路的右側邊緣線行駛&#xff0c;速度要減到20km/h以下&#xff0c;靠右以不壓右側邊緣線為基準盡量靠右。會車結束指令發出后向左打方向回到道路中央。考點&#xff1a;1.速度要降到20km/h&#xff0c;有時考官故意刁難&#xff0c;會在直線行駛…

Esxi直通板載Sata

Esxi安裝好后&#xff0c;打開SSH。 解決方法如下&#xff1a; shell下執行&#xff1a; lspci -v | grep "Class 0106"-B 1&#xff0c;查看是否有如下顯示&#xff1a;0000:00:1f.2 SATAcontroller Mass storage controller: Intel Corporation Lynx Point AHCICon…

gdb 調試 TuMediaService

gdb 調試 TuMediaService github地址 起因 首先需要有 armgdb 環境運行 ./armgdb ./TuMediaService 進入 gdb 模式b hi3531_trcod_interface.cc:98 打斷點r 運行程序print this->vdec_config_path_ 打印關鍵值 在這里我們關注的值已經被修改&#xff0c;由于程序中沒有刻…

jackson 的注解:@JsonProperty、@JsonIgnore、@JsonFormat 用法說明

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 導包&#xff1a; <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-data…

科目三-變更車道,直線行駛和超車的考試標準

直線行駛&#xff1a;這是唯一一個可以提前操作的項目&#xff0c;當聽到“下一項考試為直線行駛......”的指令時&#xff0c;可以立即把車身擺正。放在道路的正中間&#xff0c;并踩油門&#xff0c;把速度提至30----50km/h&#xff0c;最好保持在35---40km/h&#xff0c;因為…

PyQt安裝和環境配置

PyQt安裝和環境配置 github地址 首先安裝Pycharm 新建一個空的 python 工程&#xff0c;找到 setting 安裝第三方模塊 PyQT5 , 點加號&#xff0c;先安 PyQT5 , 再安裝 pyqt5-tools &#xff0c;后面包含 qtdesinger 以上模塊都安完&#xff0c;設置擴展工具的參數找到 sett…

HZOJ 大佬(kat)

及其水水水的假期望&#xff08;然而我已經被期望嚇怕了……&#xff09;。 數據范圍及其沙雕導致丟掉5分…… 因為其實每天的期望是一樣的&#xff0c;考慮分開。 f[i][j]表示做k道題&#xff0c;難度最大為j的概率。 則f[i][j](f[i-1][j])*(j-1)*temq[j]*tem;q為前綴和&#…

F12 界面:請求響應內容 Preview 和 Response 不一致、接口返回數據和 jsp 解析到的內容不一致

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 情況描述&#xff1a; 我有一個接口只是簡單的查詢列表數據并返回給前端作一個表格展示。 接口返回的 userId 數據為&#xff1a;…

為什么新手開車起步總是熄火

最近&#xff0c;深圳市民陳小姐年前考完駕照就買了一輛新車&#xff0c;在過完年后上班的第一天&#xff0c;幾乎每次等紅綠燈的路口起步時汽車都會熄火&#xff0c;導致身后的司機非常不滿狂按車喇叭催她“別擋路”&#xff0c;陳小姐自己也急得冒汗。就像陳小姐這樣的新手很…

TDD實例

TDD實例 github地址 項目中對于 TDD 的實戰&#xff0c;依賴的是 GoogleTest 框架 我負責編碼單元對中控提供 設置編碼單元設置視頻源設置視頻輸出狀態檢測開啟通道關閉通道 這 6 個接口&#xff0c;中控通過 http 調用編碼單元接口&#xff0c;為了解耦和方便進行 TDD 測…

修改Sql server中列的屬性腳本

alter tablename alter column columnname varchar(100) not null 轉載于:https://www.cnblogs.com/pw/archive/2007/01/08/615062.html

推薦 21 個頂級的 Vue UI 庫

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1、Vuetify Star 數為 11K&#xff0c;提供了 80 多個 Vue.js 組件&#xff0c;這些組件是根據谷歌 Material Design 指南實現的。Vuet…

MSCRM日志配置

之前有很多人問我在MSCRM上日志怎么做&#xff0c;具體的如&#xff08;登錄日志&#xff0c;操作日志&#xff09;。個人認為操作日志確實比較難做&#xff08;不過我可以給一個思路可以用觸發器或者plugin來實現&#xff0c;不過比較麻煩&#xff0c;對系統壓力也比較大&…

機動車駕駛人科目三考試項目及合格標準

機動車駕駛人科目三考試項目及合格標準 &#xff08;2013年道路考試智能評判&#xff09; 科目三考試綜合評判標準 一般規定&#xff1a;道路駕駛技能滿分為100分&#xff0c;成績達到90分為合格。 道路駕駛技能通用評判 不合格情形&#xff1a;考試時出現下列情形之一的&#…

數據結構——數組

數組 github地址 數組基礎 數組最大的有點&#xff1a;快速查詢。索引快數組最好應用于 “索引有語義” 的情況但并非所有有語義的索引都適用于數組&#xff08;身份證號&#xff09;數組也可以處理 ”索引沒有語義“ 的情況 封裝數組類 數組類該具備的功能&#xff1a;增…

十分鐘入門 RocketMQ

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 本文首先引出消息中間件通常需要解決哪些問題&#xff0c;在解決這些問題當中會遇到什么困難&#xff0c;Apache RocketMQ作為阿里開源的…

高智商孩子14個獨有的特點

每一位家長都希望自己的孩子具有高智商&#xff0c;但據專家分析孩子的智商一種是與生俱來的&#xff0c;另一種是在2歲之前還可以提高的&#xff0c;一起來看看怎樣才能提高孩子的智商? 智商高的孩子都具有哪些特點? 提高孩子智商的方法 1、改變兒童的飲食習慣。 提高孩…

Onvif2.6.1命名空間前綴對照

Onvif2.6.1命名空間前綴對照 tds http://www.onvif.org/ver10/device/wsdl tev http://www.onvif.org/ver10/events/wsdl tls http://www.onvif.org/ver10/display/wsdl tmd http://www.onvif.org/ver10/deviceIO/wsdl timg http://www.onvif.org/ver20/imaging/wsdl trt…