0

I want to display first 10 characters afterword ... till .pdf or xlx or .docx match. Each result has link . when user click on perticular link it will redirect to matching file and file will be download. could anyone help me.

  function createRowMultiresult(jobjects) {

        var $div = $('<div class="chat Bot"></div>');
        var $div2 = $('<div class="user-photo"><img src="{% static "Robot.jpg" %}" /></div>');
        $div.append($div2);
        var $tbl = $('<table style="width:100%;"></table>');

        if (jobjects.length>1){
            var $tr = $('<tr><td style="padding:5px;">Multiple results Found for your query. Please search with specific keyword</td></tr>');
            $tbl.append($tr);
        }

        for(var x=0; x<jobjects.length;x++){

            var currentobj = jobjects[x];
            if (currentobj.ans != null){
                if (currentobj.ans.indexOf("/AA") != -1){
                    var $tr = $('<tr><td style="padding:5px;"><a href="https://' + currentobj.ans +'" target="_blank" >Click Here for User Guide</a></td></tr>');
                    $tbl.append($tr);
                }
                else{
                    var $tr = $('<tr><td style="padding:5px;">' +(x+1)+'.'+ currentobj.ans.replace(/[^\w\s]/gi, "<br/>") +'</td> </tr>');
                    $tbl.append($tr);
                    if (currentobj.Pic.length>7){
                        var $tr = $("<tr><td style='text-align:center;'><img class='productpic' src='{% static '/Pictures/' %}" + currentobj.Pic +"' /></td></tr>");
                        $tbl.append($tr);
                    }
                }
            }
            else if(currentobj.filename != null){
                    alert("Got there");
                    var $tr = $('<tr><td style="padding:5px;"><a href="https://' + currentobj.ans + '" target="_blank" >' + currentobj.filename.slice(0,10)+"...."+ +'</a></td> </tr>');
                    $tbl.append($tr);
            }
        }

        var $par = $('<p class="chat-message"></p>');
        $par.append($tbl);

        $div.append($par);
        $chatlog.append($div);

    }

Each result has link.

Sample actual result:

  1. insert_data_data_data.pdf
  2. BE_guage_data_data.xlsx
  3. BE_guage_data_data.docx

Expected output and when user click it will download :

  1. insert_data....pdf
  2. BE_guage.....xlsx
  3. BE_guage.....docx

3 Answers 3

1

You can simply count character length and replace '...' after some point.

var displayName = ( currentobj.ans.length > 10 ) > ( currentobj.ans.substring( 0, 10 ) + '...' ) : currentobj.ans;

Now you can use this displayName variable for display.

in .substring() function first parameter is the starting point, and the second one is the endpoint.

6
  • I have another question as when user click on perticular file it will redirect to location and download. If you see the code their is https://....
    – harsh
    Commented Feb 13, 2019 at 8:48
  • So what do you what? do you want to download a file without redirecting? Commented Feb 13, 2019 at 8:51
  • Now when I click on particular file it will redirect to link but does not download because link contains 4 folders each folder contains different file format .how I can connect that link or any method to direct download. if condition properly work . I have problem with elseif condition.
    – harsh
    Commented Feb 13, 2019 at 8:54
  • then you have to add that folder path in to link ( as I know ). like this; if you want to download the pdf file and if it's in 'pdf' folder : http//<url>/pdf/<file name> Commented Feb 13, 2019 at 9:02
  • results comes with any format how I can link them ..suppose first link is related to pdf , second link is related to docx and third is related to xlsx . I want to download second then how I can link it.
    – harsh
    Commented Feb 13, 2019 at 9:07
0

According to the samples and the expected outputs, the simplest way is by using the replace function i.e.

result_str.replace(/_data_data\./, '...')
0

You can use a regex with capture groups. $n is the nth group.

var regex = /(\w{10})\w+(\.\w+)/;
var str = "insert_data_data_data.pdf";

console.log(str.replace(regex, "$1...$2"));

1
  • @harsh You can use the above way to display the name and have the original name in the href.
    – Alex G
    Commented Feb 13, 2019 at 9:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.