首页 > 其他分享 >autosize插件和自动增删行

autosize插件和自动增删行

时间:2024-07-22 11:51:39浏览次数:14  
标签:function el 插件 autosize var style 增删 ta

http://www.jacklmoore.com/autosize/

https://github.com/jackmoore/autosize?tab=readme-ov-file

列表空值自动删行,末行输入,底部自动增行,不超过10行,textarea文本域自动高度没有滚动条

<div id="app" v-cloak>
    <table style="width:700px;">
        <tr v-for="(item,index) in list" :key="index">
            <td style="width:40%;">
                <textarea type="text" class="textaa" v-model="item.Q.XMNR" v-on:input="ltextFn($event,item,index)" v-on:focus="zenghFn"></textarea>
            </td>
            <td style="width:20%;text-align: center;">--</td>
            <td style="width:40%;">
                <textarea type="text" class="textaa" v-model="item.H.XMNR" v-on:input="rtextFn($event,item,index)" v-on:focus="zenghFn"></textarea>
            </td>
        </tr>
    </table>
</div>
<!-- <script src="http://www.h5al.cn/js/autosize.min.js"></script> -->
<script src="http://www.h5al.cn/js/vue2716.js"></script>
<script src="http://www.h5al.cn/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
var vm = new Vue({
    el:"#app",
    data:{
        list:[]
    },
    created(){
        this.getData()
    },
    methods:{
        getData(){
            var self = this
            var ary = [
                {
                    Q:{XMNR: '抓改革、促发展,归根到底就是为了让人民过上更好的日子。',LX: 1,DDID : 1},
                    H:{XMNR: '十八大以来,为核心的坚持以人民为中心推进改革,',LX: 2,DDID : 2}
                },
                {
                    Q:{XMNR: '始终把人民利益摆在至高无上的地位,抓住人民最关心最直接最现实的利益问题推进重点领域改革。',LX: 1,DDID : 3},
                    H:{XMNR: '二十届三中全会审议通过的《中共中央关于进一步全面深化改革、推进中国式现代化的决定》,',LX: 2,DDID : 4}
                },
                {
                    Q:{XMNR: '坚持人民至上,从人民整体利益、根本利益、长远利益出发谋划和推进改革,',LX: 1,DDID : 5},
                    H:{XMNR: '充分彰显了我们宗旨、初心使命,充分体现了新时代思想的世界观和方法论。',LX: 2,DDID : 6}
                }
            ]
            if(ary.length!=0){
                $.each(ary,function(index,item){
                    item.uid = index+1
                })
                self.list = ary
            }else{
                self.list = [{
                    Q:{DDID:'',LX:1,XMNR:''},
                    H:{DDID:'',LX:2,XMNR:''},
                    uid:1,
                }]
            }     
            this.zenghFn()
        },
        ltextFn(event,item,ind){
            var self = this
            var curval = event.target.value
            var num = item.uid;
            if(curval === '' && (item.H.XMNR === '' || item.H.XMNR === null)){
                if(self.list.length == 1){
                    // alert("该行不能删除!!");
                    return false;
                }else{
                    for(var i=0;i < self.list.length;i++){
                        if(self.list[i].uid == num){
                            self.list.splice(i,1);
                            setTimeout(function(){
                                var ta = document.querySelectorAll('textarea');
                                $.each(ta,function(index,item){
                                    autosize.update(item);
                                })
                            },500)
                            break;
                        }
                    }
                }
            }else{
                this.zenghFn()
            }
            // console.log(self.list);
        },
        rtextFn(event,item,ind){
            var self = this
            var curval = event.target.value
            var num = item.uid;
            if((item.Q.XMNR === '' || item.Q.XMNR === null) && curval === ''){
                if(self.list.length == 1){
                    // alert("该行不能删除!!");
                    return false;
                }else{
                    for(var i=0;i < self.list.length;i++){
                        if(self.list[i].uid == num){
                            self.list.splice(i,1);
                            setTimeout(function(){
                                var ta = document.querySelectorAll('textarea');
                                $.each(ta,function(index,item){
                                    autosize.update(item);
                                })
                            },500)
                            break;
                        }
                    }
                }
            }else{
                this.zenghFn()
            }
            // console.log(self.list);
        },
        zenghFn(){
            var self = this
            var xobj = {}
            if(self.list.length < 10){
                var obj = self.list[self.list.length-1]
                if(obj.Q.XMNR != '' || obj.H.XMNR != ''){
                    xobj.Q = {}
                    xobj.H = {}
                    xobj.Q.XMNR = ''
                    xobj.Q.LX = 1
                    xobj.Q.DDID = ''
                    xobj.H.XMNR = ''
                    xobj.H.LX = 2
                    xobj.H.DDID = ''
                    xobj.uid = self.list.length + 1
                    self.list.push(xobj)
                    setTimeout(function(){
                        autosize(document.querySelectorAll('textarea'));
                    },500)
                }
            }
            // console.log(self.list);
        },
    }
})

;(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
        typeof define === 'function' && define.amd ? define(factory) :
            (global = global || self, global.autosize = factory());
}(this, (function () {
    var assignedElements = new Map();
    function assign(ta) {
        if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || assignedElements.has(ta)) return;
        var previousHeight = null;
        function cacheScrollTops(el) {
            var arr = [];
            while (el && el.parentNode && el.parentNode instanceof Element) {
                if (el.parentNode.scrollTop) {
                    arr.push([el.parentNode, el.parentNode.scrollTop]);
                }
                el = el.parentNode;
            }
            return function () {
                return arr.forEach(function (_ref) {
                    var node = _ref[0],
                        scrollTop = _ref[1];
                    node.style.scrollBehavior = 'auto';
                    node.scrollTop = scrollTop;
                    node.style.scrollBehavior = null;
                });
            };
        }
        var computed = window.getComputedStyle(ta);
        function setHeight(_ref2) {
            var _ref2$restoreTextAlig = _ref2.restoreTextAlign,
                restoreTextAlign = _ref2$restoreTextAlig === void 0 ? null : _ref2$restoreTextAlig,
                _ref2$testForHeightRe = _ref2.testForHeightReduction,
                testForHeightReduction = _ref2$testForHeightRe === void 0 ? true : _ref2$testForHeightRe;
            var initialOverflowY = computed.overflowY;
            if (ta.scrollHeight === 0) {
                // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM.
                return;
            } // disallow vertical resizing
            if (computed.resize === 'vertical') {
                ta.style.resize = 'none';
            } else if (computed.resize === 'both') {
                ta.style.resize = 'horizontal';
            }
            var restoreScrollTops; // remove inline height style to accurately measure situations where the textarea should shrink
            // however, skip this step if the new value appends to the previous value, as textarea height should only have grown
            if (testForHeightReduction) {
                // ensure the scrollTop values of parent elements are not modified as a consequence of shrinking the textarea height
                restoreScrollTops = cacheScrollTops(ta);
                ta.style.height = '';
            }
            var newHeight;
            if (computed.boxSizing === 'content-box') {
                newHeight = ta.scrollHeight - (parseFloat(computed.paddingTop) + parseFloat(computed.paddingBottom));
            } else {
                newHeight = ta.scrollHeight + parseFloat(computed.borderTopWidth) + parseFloat(computed.borderBottomWidth);
            }
            if (computed.maxHeight !== 'none' && newHeight > parseFloat(computed.maxHeight)) {
                if (computed.overflowY === 'hidden') {
                    ta.style.overflow = 'scroll';
                }
                newHeight = parseFloat(computed.maxHeight);
            } else if (computed.overflowY !== 'hidden') {
                ta.style.overflow = 'hidden';
            }
            ta.style.height = newHeight + 'px';
            if (restoreTextAlign) {
                ta.style.textAlign = restoreTextAlign;
            }
            if (restoreScrollTops) {
                restoreScrollTops();
            }
            if (previousHeight !== newHeight) {
                ta.dispatchEvent(new Event('autosize:resized', {
                    bubbles: true
                }));
                previousHeight = newHeight;
            }
            if (initialOverflowY !== computed.overflow && !restoreTextAlign) {
                var textAlign = computed.textAlign;
                if (computed.overflow === 'hidden') {
                    // Webkit fails to reflow text after overflow is hidden,
                    // even if hiding overflow would allow text to fit more compactly.
                    // The following is intended to force the necessary text reflow.
                    ta.style.textAlign = textAlign === 'start' ? 'end' : 'start';
                }
                setHeight({
                    restoreTextAlign: textAlign,
                    testForHeightReduction: true
                });
            }
        }
        function fullSetHeight() {
            setHeight({
                testForHeightReduction: true,
                restoreTextAlign: null
            });
        }
        var handleInput = function () {
            var previousValue = ta.value;
            return function () {
                setHeight({
                    // if previousValue is '', check for height shrinkage because the placeholder may be taking up space instead
                    // if new value is merely appending to previous value, skip checking for height deduction
                    testForHeightReduction: previousValue === '' || !ta.value.startsWith(previousValue),
                    restoreTextAlign: null
                });
                previousValue = ta.value;
            };
        }();
        var destroy = function (style) {
            ta.removeEventListener('autosize:destroy', destroy);
            ta.removeEventListener('autosize:update', fullSetHeight);
            ta.removeEventListener('input', handleInput);
            window.removeEventListener('resize', fullSetHeight); // future todo: consider replacing with ResizeObserver
            Object.keys(style).forEach(function (key) {
                return ta.style[key] = style[key];
            });
            assignedElements["delete"](ta);
        }.bind(ta, {
            height: ta.style.height,
            resize: ta.style.resize,
            textAlign: ta.style.textAlign,
            overflowY: ta.style.overflowY,
            overflowX: ta.style.overflowX,
            wordWrap: ta.style.wordWrap
        });
        ta.addEventListener('autosize:destroy', destroy);
        ta.addEventListener('autosize:update', fullSetHeight);
        ta.addEventListener('input', handleInput);
        window.addEventListener('resize', fullSetHeight); // future todo: consider replacing with ResizeObserver
        ta.style.overflowX = 'hidden';
        ta.style.wordWrap = 'break-word';
        assignedElements.set(ta, {
            destroy: destroy,
            update: fullSetHeight
        });
        fullSetHeight();
    }
    function destroy(ta) {
        var methods = assignedElements.get(ta);

        if (methods) {
            methods.destroy();
        }
    }
    function update(ta) {
        var methods = assignedElements.get(ta);

        if (methods) {
            methods.update();
        }
    }
    var autosize = null; // Do nothing in Node.js environment
    if (typeof window === 'undefined') {
        autosize = function autosize(el) {
            return el;
        };
        autosize.destroy = function (el) {
            return el;
        };
        autosize.update = function (el) {
            return el;
        };
    } else {
        autosize = function autosize(el, options) {
            if (el) {
                Array.prototype.forEach.call(el.length ? el : [el], function (x) {
                    return assign(x);
                });
            }
            return el;
        };
        autosize.destroy = function (el) {
            if (el) {
                Array.prototype.forEach.call(el.length ? el : [el], destroy);
            }
            return el;
        };
        autosize.update = function (el) {
            if (el) {
                Array.prototype.forEach.call(el.length ? el : [el], update);
            }
            return el;
        };
    }
    var autosize$1 = autosize;
    return autosize$1;
})));

setTimeout(function(){
    autosize(document.querySelectorAll('textarea'));
},500)
</script>

 

标签:function,el,插件,autosize,var,style,增删,ta
From: https://www.cnblogs.com/liufeiran/p/18315764

相关文章

  • 在K8S中,优先优选哪个CNI插件?为何使用该插件?
    在Kubernetes(K8s)中,选择哪个CNI(ContainerNetworkInterface)插件并没有绝对的“最优”选择,因为不同的插件适用于不同的场景和需求。以下是一些常见的CNI插件及其特点,以及选择它们时可能考虑的因素:1.Flannel特点:最常用的K8s网络插件之一。使用虚拟网络技术(如VXLAN、UDP和Host-......
  • MiniAuth 一个轻量 ASP.NET Core Identity Web 后台管理中间插件
    MiniAuth一个轻量ASP.NETCoreIdentityWeb后台管理中间插件「一行代码」为「新、旧项目」添加Identity系统跟用户、权限管理网页后台系统开箱即用,避免打掉重写或是严重耦合情况Github:https://github.com/mini-software/MiniAuth,Gitee:https://gitee.com/shps9510......
  • vue3 ts 项目增加eslint插件实现命令行报错提示和vscode 报错提示,eslint 最新版本9.x
    快速开始安装eslintyarnaddeslint-D然后运行初始化eslintnpxeslint--init接着上面命令会自动生成一个新文件eslint.config.jseslint.config.jsimportglobalsfrom"globals";importpluginJsfrom"@eslint/js";importtseslintfrom"typescript-eslint......
  • ComfyUI进阶:Comfyroll插件 (六)
    ComfyUI进阶:Comfyroll插件(六)前言:学习ComfyUI是一场持久战,而Comfyroll是一款功能强大的自定义节点集合,专为ComfyUI用户打造,旨在提供更加丰富和专业的图像生成与编辑工具。借助这些节点,用户可以在静态图像的精细调整和动态动画的复杂构建方面进行深入探索。Comfyroll的节点设......
  • Day44.数据的增删改查
    1.数据的增删改查_新增单条数据和多条数据#?1.增#?insertintot1values(1,'wanye');#?insertintot1values(1,'wanye'),(2,'egon'),(3,'zc');2.数据的增删改查_查询表所有数据和查询表中指定字段数据#?2.查#?select*fromt1;......
  • Day44.表的增删改查
    1.表的增删改查_查询当前所在库和切换库2.表的增删改查_创建表、查询所有表、查询单表信息3.表的增删改查_表的修改4.表的增删改查_表的删除5.表的增删改查_跨库利用绝对路径创建表6.表的增删改查_总结 ......
  • xlsx 前端插件 导出 elment表格示例
    安装yarnaddxlsxyarnaddfile-savergit文档:https://github.com/rockboom/SheetJS-docs-zh-CNnpm地址:https://www.npmjs.com/package/xlsx代码<template> <divclass='box'> pageC <el-buttontype="primary"size="defa......
  • vscode注释插件koroFileHeader使用, vue 文件注释插件
    使用文档https://github.com/OBKoro1/koro1FileHeader/wiki/安装和快速上手git地址https://github.com/OBKoro1/koro1FileHeader安装测试搜索setting.json用户输入如下配置//头部注释"fileheader.customMade":{//Author字段是文件的创建者可以在specialO......
  • IDEA的APIPost接口测试插件详解
    APIPOST官方网址一、安装APIPost插件打开IntelliJIDEA:启动您的IntelliJIDEA开发环境。导航到插件设置:在Windows或Linux上,点击File>Settings。在macOS上,点击IntelliJIDEA>Preferences。搜索并安装APIPost插件:在设置窗口中,选择Plugins。点击Marketplace标签......
  • chrome 插件开发
    chrome插件介绍 Chrome插件(ChromeExtension)是一种可以扩展浏览器功能的小程序,它们可以修改和增强浏览器的功能,提供更好的用户体验。常见的用途和功能1.修改网页内容Chrome插件可以访问和修改当前网页的内容。例如:广告拦截:移除网页上的广告。内容高亮:高亮显示特定的......