Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

javascript - (Tabulator) How can I remove unwanted newline from string when pasting?

I'm using Tabulator >> link
How to remove unwanted newline in string when pasting the data copied from Excel sheet?

This is my data in Excel sheet
enter image description here

And this is the current result
enter image description here

I would like the result to show like this
enter image description here

This is my current code

<div id="example-table"></div>
<script>
    var table = new Tabulator("#example-table", {
        height:205,
        layout:"fitColumns", 
        columns:[ 
            {title:"A", field:"A"},
            {title:"B", field:"B"},
            {title:"C", field:"C"},
            {title:"D", field:"D"},
        ],
        clipboard:"paste", 
    });
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have achieved it by adding custom manipulation to remove unwanted newlines when pasting data into the table. Here's the code

var table = new Tabulator("#example-table", {
        height:205,
        layout:"fitData",
        columns:col_list,
        clipboard:true,
        clipboardPasteAction:"replace",
        clipboardPasteParser:function(clipboard){
            clipboard = JSON.stringify(clipboard);
            clipboard = clipboard.replace(/\n/g,"");
            clipboard = clipboard.replace(/\r/g,"
");

            var data = clipboard.split('
').map(function(ln){
                return ln.split("\t");
            });
            data.splice(-1,1)
           
            var new_data = [];
            $.each( data, function( index,val ) {
                var obj = {};
                for(var i=0; i<73; i++){
                    var col = col_list[i].title;
                    obj[col] = val[i];
                }
                new_data.push(obj);
            });
            table.addData(new_data);
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...